From 226514ef508f1f18cc5b84a3c7a71e5e06a24725 Mon Sep 17 00:00:00 2001 From: Liam Aharon Date: Mon, 8 Apr 2024 17:55:21 +1000 Subject: [PATCH] Simplify some logging (#83) --- core/src/commands/on_runtime_upgrade.rs | 73 +++++++------------------ core/src/lib.rs | 1 + core/src/misc.rs | 39 +++++++++++++ 3 files changed, 60 insertions(+), 53 deletions(-) create mode 100644 core/src/misc.rs diff --git a/core/src/commands/on_runtime_upgrade.rs b/core/src/commands/on_runtime_upgrade.rs index 38086994e89..50f17823936 100644 --- a/core/src/commands/on_runtime_upgrade.rs +++ b/core/src/commands/on_runtime_upgrade.rs @@ -19,7 +19,7 @@ use std::{collections::BTreeMap, fmt::Debug, str::FromStr}; use bytesize::ByteSize; use frame_try_runtime::UpgradeCheckSelect; -use paris::formatter::colorize_string; +use log::Level; use parity_scale_codec::Encode; use sc_executor::sp_wasm_interface::HostFunctions; use sp_core::{hexdisplay::HexDisplay, Hasher}; @@ -27,7 +27,7 @@ use sp_runtime::traits::{Block as BlockT, HashingFor, NumberFor}; use sp_state_machine::{CompactProof, OverlayedChanges, StorageProof}; use crate::{ - build_executor, + build_executor, misc, state::{RuntimeChecks, State}, state_machine_call_with_proof, RefTimeInfo, SharedParams, LOG_TARGET, }; @@ -106,24 +106,13 @@ where } // Run `TryRuntime_on_runtime_upgrade` with the given checks. - log::info!( - "{}", - colorize_string( - "-------------------------------------------------------------------\n\n" - ) - ); - log::info!( - "{}", - colorize_string(format!( - "🔬 Running TryRuntime_on_runtime_upgrade with checks: {:?}\n\n", + misc::basti_log( + Level::Info, + format!( + "🔬 Running TryRuntime_on_runtime_upgrade with checks: {:?}", command.checks - )) - ); - log::info!( - "{}", - colorize_string( - "-------------------------------------------------------------------" ) + .as_str(), ); // Save the overlayed changes from the first run, so we can use them later for idempotency // checks. @@ -146,17 +135,9 @@ where let (proof, ref_time_results) = match command.checks { UpgradeCheckSelect::None => (proof, ref_time_results), _ => { - log::info!( - "{}", - colorize_string("-------------------------------------------------------------------\n\n") - ); - log::info!( - "{}", - colorize_string("🔬 TryRuntime_on_runtime_upgrade succeeded! Running it again without checks for weight measurements.\n\n"), - ); - log::info!( - "{}", - colorize_string("-------------------------------------------------------------------") + misc::basti_log( + Level::Info, + "🔬 TryRuntime_on_runtime_upgrade succeeded! Running it again without checks for weight measurements.", ); let (proof, encoded_result) = state_machine_call_with_proof::( &ext, @@ -179,17 +160,13 @@ where true } false => { - log::info!( - "{}", - colorize_string("-------------------------------------------------------------------\n\n") - ); - log::info!( - "{}", - colorize_string(format!("🔬 Running TryRuntime_on_runtime_upgrade again to check idempotency: {:?}\n\n", command.checks)), - ); - log::info!( - "{}", - colorize_string("-------------------------------------------------------------------") + misc::basti_log( + Level::Info, + format!( + "🔬 Running TryRuntime_on_runtime_upgrade again to check idempotency: {:?}", + command.checks + ) + .as_str(), ); let (oc_pre_root, _) = overlayed_changes.storage_root(&ext.backend, ext.state_version); overlayed_changes.start_transaction(); @@ -267,19 +244,9 @@ where }; if !weight_ok || !idempotency_ok { - log::error!( - "{}", - colorize_string("-------------------------------------------------------------------\n\n") - ); - log::error!( - "{}", - colorize_string("❌ Issues detected, exiting non-zero. See logs.\n\n"), - ); - log::error!( - "{}", - colorize_string( - "-------------------------------------------------------------------" - ) + misc::basti_log( + Level::Error, + "❌ Issues detected, exiting non-zero. See logs.", ); std::process::exit(1); } diff --git a/core/src/lib.rs b/core/src/lib.rs index f43ecabbda8..03558d996a7 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -42,6 +42,7 @@ use crate::shared_parameters::SharedParams; pub mod commands; pub mod inherent_provider; +mod misc; mod parse; pub mod shared_parameters; pub mod state; diff --git a/core/src/misc.rs b/core/src/misc.rs new file mode 100644 index 00000000000..e7f705e40ca --- /dev/null +++ b/core/src/misc.rs @@ -0,0 +1,39 @@ +use log::{log, Level}; +use paris::formatter::colorize_string; + +fn level_to_color(level: Level) -> &'static str { + match level { + Level::Info => "blue", + Level::Warn => "yellow", + Level::Error => "red", + _ => "white", + } +} + +/// A BIG log that's very difficult to miss. +pub fn basti_log(level: Level, message: &str) { + let color = level_to_color(level); + log!( + level, + "{}", + colorize_string(format!( + "<{}>{}\n\n", + &color, + "-".repeat(message.len()) + )) + ); + log!( + level, + "{}", + colorize_string(format!("<{}>{}\n\n", &color, message)) + ); + log!( + level, + "{}", + colorize_string(format!( + "<{}>{}\n\n", + &color, + "-".repeat(message.len()) + )) + ); +}