Skip to content

Commit

Permalink
fix: panic messages now log to file
Browse files Browse the repository at this point in the history
  • Loading branch information
TgZ39 committed Jan 30, 2025
1 parent 27fa336 commit 9044448
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions wizardrs-client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const MAX_LOGS: usize = 20;

#[tokio::main]
async fn main() -> Result<()> {
setup_panic_hook();
setup_logger()?;
debug!("started logger");

Expand Down Expand Up @@ -181,3 +182,12 @@ fn setup_logger() -> Result<()> {

Ok(())
}

fn setup_panic_hook() {
std::panic::set_hook(Box::new(|info| {
let location = info.location().map(|loc| format!("{}:{}:{}", loc.file(), loc.line(), loc.column())).unwrap_or("unknown".to_string());
let message = info.payload().downcast_ref::<&str>().map_or_else(|| "unknown panic".to_string(), |s| s.to_string());

error!("Panic occurred: {} at {}", message, location);
}));
}

0 comments on commit 9044448

Please sign in to comment.