From 981b3c79eeadcb5948a77f7b7f0d980d6daf3993 Mon Sep 17 00:00:00 2001 From: Sergei Pepyakin <sergei@parity.io> Date: Thu, 12 Sep 2019 17:52:26 +0200 Subject: [PATCH] Print version when panic (#3608) * Print version when panic * Fix tests. --- substrate/core/cli/src/lib.rs | 6 +++--- .../core/client/src/light/call_executor.rs | 2 +- substrate/core/panic-handler/src/lib.rs | 17 ++++++++++++----- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/substrate/core/cli/src/lib.rs b/substrate/core/cli/src/lib.rs index 6e9955ca1a4..1fa4cabd632 100644 --- a/substrate/core/cli/src/lib.rs +++ b/substrate/core/cli/src/lib.rs @@ -77,7 +77,7 @@ const NODE_KEY_ED25519_FILE: &str = "secret_ed25519"; /// Executable version. Used to pass version information from the root crate. pub struct VersionInfo { - /// Implemtation name. + /// Implementaiton name. pub name: &'static str, /// Implementation version. pub version: &'static str, @@ -191,13 +191,13 @@ where I: IntoIterator, <I as IntoIterator>::Item: Into<std::ffi::OsString> + Clone, { - panic_handler::set(version.support_url); - let full_version = service::config::full_version_from_strs( version.version, version.commit ); + panic_handler::set(version.support_url, &full_version); + let matches = CoreParams::<CC, RP>::clap() .name(version.executable_name) .author(version.author) diff --git a/substrate/core/client/src/light/call_executor.rs b/substrate/core/client/src/light/call_executor.rs index a06b48a6411..2c9c1f29957 100644 --- a/substrate/core/client/src/light/call_executor.rs +++ b/substrate/core/client/src/light/call_executor.rs @@ -494,7 +494,7 @@ mod tests { execute_with_proof_failure(&remote_client, 2, "Core_version"); // check that proof check doesn't panic even if proof is incorrect AND panic handler is set - panic_handler::set("TEST"); + panic_handler::set("TEST", "1.2.3"); execute_with_proof_failure(&remote_client, 2, "Core_version"); } diff --git a/substrate/core/panic-handler/src/lib.rs b/substrate/core/panic-handler/src/lib.rs index 2c04700e969..1df05120c11 100644 --- a/substrate/core/panic-handler/src/lib.rs +++ b/substrate/core/panic-handler/src/lib.rs @@ -51,8 +51,13 @@ enum OnPanic { /// /// The `bug_url` parameter is an invitation for users to visit that URL to submit a bug report /// in the case where a panic happens. -pub fn set(bug_url: &'static str) { - panic::set_hook(Box::new(move |c| panic_hook(c, bug_url))); +pub fn set(bug_url: &'static str, version: &str) { + panic::set_hook(Box::new({ + let version = version.to_string(); + move |c| { + panic_hook(c, bug_url, &version) + } + })); } macro_rules! ABOUT_PANIC { @@ -124,7 +129,7 @@ impl Drop for AbortGuard { } /// Function being called when a panic happens. -fn panic_hook(info: &PanicInfo, report_url: &'static str) { +fn panic_hook(info: &PanicInfo, report_url: &'static str, version: &str) { let location = info.location(); let file = location.as_ref().map(|l| l.file()).unwrap_or("<unknown>"); let line = location.as_ref().map(|l| l.line()).unwrap_or(0); @@ -147,6 +152,8 @@ fn panic_hook(info: &PanicInfo, report_url: &'static str) { let _ = writeln!(stderr, ""); let _ = writeln!(stderr, "===================="); let _ = writeln!(stderr, ""); + let _ = writeln!(stderr, "Version: {}", version); + let _ = writeln!(stderr, ""); let _ = writeln!(stderr, "{:?}", backtrace); let _ = writeln!(stderr, ""); let _ = writeln!( @@ -169,14 +176,14 @@ mod tests { #[test] fn does_not_abort() { - set("test"); + set("test", "1.2.3"); let _guard = AbortGuard::force_unwind(); ::std::panic::catch_unwind(|| panic!()).ok(); } #[test] fn does_not_abort_after_never_abort() { - set("test"); + set("test", "1.2.3"); let _guard = AbortGuard::never_abort(); let _guard = AbortGuard::force_abort(); std::panic::catch_unwind(|| panic!()).ok(); -- GitLab