Add panic logging

This commit is contained in:
biroder 2024-03-25 16:06:47 +00:00
parent 08f086bfc4
commit ca5361cc58
2 changed files with 18 additions and 2 deletions

View File

@ -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"]

View File

@ -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;