From 9ccb22b2ac943f500ae1940f0279525e0e277637 Mon Sep 17 00:00:00 2001 From: edouard Date: Thu, 9 Feb 2023 11:15:08 +0100 Subject: [PATCH] Add panic_hook to gui --- gui/src/main.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/gui/src/main.rs b/gui/src/main.rs index 66296d4c..5f67a7cf 100644 --- a/gui/src/main.rs +++ b/gui/src/main.rs @@ -294,6 +294,7 @@ fn main() -> Result<(), Box> { log::LevelFilter::Info }; setup_logger(level)?; + setup_panic_hook(); let mut settings = Settings::with_flags(config); settings.exit_on_close_request = false; @@ -304,6 +305,36 @@ fn main() -> Result<(), Box> { Ok(()) } +// A panic in any thread should stop the main thread, and print the panic. +fn setup_panic_hook() { + std::panic::set_hook(Box::new(move |panic_info| { + let file = panic_info + .location() + .map(|l| l.file()) + .unwrap_or_else(|| "'unknown'"); + let line = panic_info + .location() + .map(|l| l.line().to_string()) + .unwrap_or_else(|| "'unknown'".to_string()); + + let bt = backtrace::Backtrace::new(); + let info = panic_info + .payload() + .downcast_ref::<&str>() + .map(|s| s.to_string()) + .or_else(|| panic_info.payload().downcast_ref::().cloned()); + log::error!( + "panic occurred at line {} of file {}: {:?}\n{:?}", + line, + file, + info, + bt + ); + + std::process::exit(1); + })); +} + // This creates the log file automagically if it doesn't exist, and logs on stdout // if None is given pub fn setup_logger(log_level: log::LevelFilter) -> Result<(), fern::InitError> {