forked from kaspanet/rusty-kaspa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write panic errors to logs (kaspanet#445) (kaspanet#446)
* Write panic errors to logs (kaspanet#445) * Remove the comment as it doesn't need to be addressed Reason: Waiting here will block the thread, and as the thread is locked and we wait for the logger system to be initiated that would wait for the thread to be released itself, making the waiting pointless * Add column to the log message for the panic error * Move panic setup after the logger init in all places * Add the thread name to the panic error * default hook invoke position + minor style changes
- Loading branch information
Showing
6 changed files
with
45 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,31 @@ | ||
use std::{panic, process}; | ||
use kaspa_core::error; | ||
use std::{panic, process, thread}; | ||
|
||
/// Configures the panic hook to exit the program on every panic | ||
pub fn configure_panic() { | ||
let default_hook = panic::take_hook(); | ||
panic::set_hook(Box::new(move |panic_info| { | ||
// Invoke the default hook and exit the process | ||
// Get the panic location details | ||
let (file, line, column) = match panic_info.location() { | ||
Some(location) => (location.file(), location.line(), location.column()), | ||
None => ("unknown", 0, 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 => "Box<dyn Any>", | ||
}, | ||
}; | ||
// Get the thread name | ||
let current_thread = thread::current(); | ||
let thread_name = current_thread.name().unwrap_or("<unnamed>"); | ||
// Log the panic | ||
error!("thread '{}' panicked at {}:{}:{}: {}", thread_name, file, line, column, message); | ||
// Invoke the default hook as well, since it might include additional info such as the full backtrace | ||
default_hook(panic_info); | ||
println!("Exiting..."); | ||
// TODO: setup a wait time and fold the log system properly | ||
process::exit(1); | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters