From 4789131859df762dcfedc77304bfd007a3e33230 Mon Sep 17 00:00:00 2001 From: Marcello Date: Tue, 17 Oct 2023 13:25:25 +0200 Subject: [PATCH] added message when exiting (#4069) * added message when exiting * use general macro print_start_and_end * few changes from rebase * cargo fmt * addressed comments * added comment about panic handling for the node --- engine/src/main.rs | 6 +++- state-chain/node/src/command.rs | 13 ++++--- utilities/src/with_std/logging.rs | 57 +++++++++++++++++++++++++++++-- 3 files changed, 68 insertions(+), 8 deletions(-) diff --git a/engine/src/main.rs b/engine/src/main.rs index 0a962aac6c..f50af09d2e 100644 --- a/engine/src/main.rs +++ b/engine/src/main.rs @@ -87,8 +87,12 @@ async fn main() -> anyhow::Result<()> { // Note: the greeting should only be printed in normal mode (i.e. not for short-lived commands // like `--version`), so we execute it only after the settings have been parsed. - utilities::print_starting!(); + utilities::print_start_and_end!(async run_main(settings)); + Ok(()) +} + +async fn run_main(settings: Settings) -> anyhow::Result<()> { task_scope(|scope| { async move { let mut start_logger_server_fn = Some(utilities::logging::init_json_logger(settings.logging.clone()).await); diff --git a/state-chain/node/src/command.rs b/state-chain/node/src/command.rs index ef373fabc5..f10af89201 100644 --- a/state-chain/node/src/command.rs +++ b/state-chain/node/src/command.rs @@ -8,6 +8,7 @@ use frame_benchmarking_cli::{BenchmarkCmd, ExtrinsicFactory, SUBSTRATE_REFERENCE use sc_cli::SubstrateCli; use sc_service::PartialComponents; use state_chain_runtime::Block; +use std::panic::{catch_unwind, AssertUnwindSafe}; #[cfg(feature = "try-runtime")] use try_runtime_cli::block_building_info::timestamp_with_aura_info; @@ -203,11 +204,13 @@ pub fn run() -> sc_cli::Result<()> { You can enable it with `--features try-runtime`." .into()), None => { - utilities::print_starting!(); - let runner = cli.create_runner(&cli.run)?; - runner.run_node_until_exit(|config| async move { - service::new_full(config).map_err(sc_cli::Error::Service) - }) + utilities::print_start_and_end!({ + let runner = cli.create_runner(&cli.run)?; + runner.run_node_until_exit(|config| async move { + service::new_full(config).map_err(sc_cli::Error::Service) + }) + }); + Ok(()) }, } } diff --git a/utilities/src/with_std/logging.rs b/utilities/src/with_std/logging.rs index 57c13513fb..14a61e0aa6 100644 --- a/utilities/src/with_std/logging.rs +++ b/utilities/src/with_std/logging.rs @@ -10,8 +10,8 @@ pub struct LoggingSettings { } #[macro_export] -macro_rules! print_starting { - () => { +macro_rules! print_start_and_end { + (async $e:expr) => { println!( "Starting {} v{} ({})", env!("CARGO_PKG_NAME"), @@ -28,6 +28,59 @@ macro_rules! print_starting { ╚═════╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝╚═╝ ╚═══╝╚═╝ ╚══════╝╚═╝╚═╝ " ); + + match std::panic::AssertUnwindSafe($e).catch_unwind().await { + Ok(result) => match result { + Ok(_) => {}, + Err(error) => { + println!("Exiting {} due to error: {error:?}", env!("CARGO_PKG_NAME")); + }, + }, + Err(panic) => { + println!( + "Exiting {} due to panic: {:#?}", + env!("CARGO_PKG_NAME"), + panic.downcast_ref::<&str>() + ); + }, + }; + () + }; + ($e:expr) => { + println!( + "Starting {} v{} ({})", + env!("CARGO_PKG_NAME"), + env!("CARGO_PKG_VERSION"), + utilities::internal_lazy_format!(if let Some(repository_link) = utilities::repository_link() => ("CI Build: \"{}\"", repository_link) else => ("Non-CI Build")) + ); + println!( + " + ██████╗██╗ ██╗ █████╗ ██╗███╗ ██╗███████╗██╗ ██╗██████╗ + ██╔════╝██║ ██║██╔══██╗██║████╗ ██║██╔════╝██║ ██║██╔══██╗ + ██║ ███████║███████║██║██╔██╗ ██║█████╗ ██║ ██║██████╔╝ + ██║ ██╔══██║██╔══██║██║██║╚██╗██║██╔══╝ ██║ ██║██╔═══╝ + ╚██████╗██║ ██║██║ ██║██║██║ ╚████║██║ ███████╗██║██║ + ╚═════╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝╚═╝ ╚═══╝╚═╝ ╚══════╝╚═╝╚═╝ + " + ); + + match catch_unwind(AssertUnwindSafe(|| $e)) { + Ok(result) => match result { + Ok(_) => {}, + Err(error) => { + println!("Exiting {} due to error: {error:?}", env!("CARGO_PKG_NAME")); + }, + }, + Err(panic) => { + // We'll never catch a panic since we use sp-panic-handler which set up a panic hook and abort the process + println!( + "Exiting {} due to panic: {:#?}", + env!("CARGO_PKG_NAME"), + panic.downcast_ref::<&str>() + ); + }, + }; + } }