Add panic_hook to gui
This commit is contained in:
parent
5d5792197d
commit
9ccb22b2ac
@ -294,6 +294,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
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<dyn Error>> {
|
||||
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::<String>().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> {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user