Skip to content

Commit

Permalink
added message when exiting (#4069)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
marcellorigotti authored Oct 17, 2023
1 parent bee7e7f commit 4789131
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
6 changes: 5 additions & 1 deletion engine/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
13 changes: 8 additions & 5 deletions state-chain/node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(())
},
}
}
57 changes: 55 additions & 2 deletions utilities/src/with_std/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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>()
);
},
};

}
}

Expand Down

0 comments on commit 4789131

Please sign in to comment.