Skip to content

Commit

Permalink
Write panic errors to logs (kaspanet#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
gvbgduh committed Apr 4, 2024
1 parent 6fd7029 commit 4b5f6d6
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions core/src/panic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use kaspa_core::error;
use std::{panic, process};

/// Configures the panic hook to exit the program on every panic
Expand All @@ -8,6 +9,23 @@ pub fn configure_panic() {
default_hook(panic_info);
println!("Exiting...");
// TODO: setup a wait time and fold the log system properly

// Get the panic location and message
let (file, line) = match panic_info.location() {
Some(location) => (location.file(), location.line()),
None => ("unknown", 0),
};

let message = match panic_info.payload().downcast_ref::<&str>() {
Some(s) => *s,
None => match panic_info.payload().downcast_ref::<String>() {
Some(s) => &s[..],
None => "unknown",
},
};
// Log the panic
error!("Panic at {}:{}: {}", file, line, message);

process::exit(1);
}));
}

0 comments on commit 4b5f6d6

Please sign in to comment.