From ca5361cc586e78815e659ba7255ec83a3aae9acc Mon Sep 17 00:00:00 2001 From: biroder <107300789+biroder@users.noreply.github.com> Date: Mon, 25 Mar 2024 16:06:47 +0000 Subject: [PATCH] Add panic logging --- Cargo.toml | 1 + src/game/mod.rs | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 18efbbd..ed50a06 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ description = "A re-implementation of Cave Story (Doukutsu Monogatari) engine" version = "0.101.0" authors = ["Alula", "dawnDus"] edition = "2021" +rust-version = "1.65" [lib] crate-type = ["lib"] diff --git a/src/game/mod.rs b/src/game/mod.rs index 13d8943..76c38e2 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -1,4 +1,6 @@ +use std::backtrace::Backtrace; use std::cell::UnsafeCell; +use std::panic::PanicInfo; use std::path::PathBuf; use std::sync::Mutex; use std::time::{Duration, Instant}; @@ -290,14 +292,27 @@ fn init_logger() -> GameResult { Ok(()) } +fn panic_hook(info: &PanicInfo<'_>) { + let backtrace = Backtrace::force_capture(); + let msg = info.payload().downcast_ref::<&str>().unwrap_or(&""); + let location = info.location(); + + if location.is_some() { + log::error!("Panic occurred in {} with message: '{msg}'\n {backtrace:#}", location.unwrap().to_string()); + } else { + log::error!("Panic occurred with message: '{msg}'\n {backtrace:#}"); + } +} + pub fn init(options: LaunchOptions) -> GameResult { let _ = init_logger(); - + std::panic::set_hook(Box::new(panic_hook)); + let mut context = Box::pin(Context::new()); let mut fs_container = FilesystemContainer::new(); fs_container.mount_fs(&mut context)?; - + if options.server_mode { log::info!("Running in server mode..."); context.headless = true;