From 2db5ca4fa73a9c487d073b24a44384d36eb3ad62 Mon Sep 17 00:00:00 2001 From: Dragos Daian Date: Mon, 9 Sep 2024 16:36:39 +0200 Subject: [PATCH] Add extra logging Update private.rs Update private.rs Update private.rs Update private.rs Update Cargo.toml Update private.rs Update private.rs Update private.rs --- godot-core/src/private.rs | 46 ++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/godot-core/src/private.rs b/godot-core/src/private.rs index 27fe2c99f..bc70ce33b 100644 --- a/godot-core/src/private.rs +++ b/godot-core/src/private.rs @@ -200,6 +200,7 @@ pub fn is_class_runtime(is_tool: bool) -> bool { // ---------------------------------------------------------------------------------------------------------------------------------------------- // Panic handling +#[cfg(debug_assertions)] #[derive(Debug)] struct GodotPanicInfo { line: u32, @@ -328,12 +329,15 @@ where F: FnOnce() -> R + std::panic::UnwindSafe, S: std::fmt::Display, { + #[cfg(debug_assertions)] let info: Arc>> = Arc::new(Mutex::new(None)); - // Back up previous hook, set new one - let prev_hook = std::panic::take_hook(); - { + // Back up previous hook, set new one. + #[cfg(debug_assertions)] + let prev_hook = { let info = info.clone(); + let prev_hook = std::panic::take_hook(); + std::panic::set_hook(Box::new(move |panic_info| { if let Some(location) = panic_info.location() { *info.lock().unwrap() = Some(GodotPanicInfo { @@ -345,30 +349,38 @@ where eprintln!("panic occurred, but can't get location information"); } })); - } - // Run code that should panic, restore hook + prev_hook + }; + + // Run code that should panic, restore hook. let panic = std::panic::catch_unwind(code); + + // Restore the previous panic hook if in Debug mode. + #[cfg(debug_assertions)] std::panic::set_hook(prev_hook); match panic { Ok(result) => Ok(result), Err(err) => { // Flush, to make sure previous Rust output (e.g. test announcement, or debug prints during app) have been printed - // TODO write custom panic handler and move this there, before panic backtrace printing + // TODO write custom panic handler and move this there, before panic backtrace printing. flush_stdout(); - let guard = info.lock().unwrap(); - let info = guard.as_ref().expect("no panic info available"); - - if print { - godot_error!( - "Rust function panicked at {}:{}.\n Context: {}", - info.file, - info.line, - error_context() - ); - //eprintln!("Backtrace:\n{}", info.backtrace); + // Handle panic info only in Debug mode. + #[cfg(debug_assertions)] + { + let guard = info.lock().unwrap(); + let info = guard.as_ref().expect("no panic info available"); + if print { + godot_error!( + "Rust function panicked at {}:{}.\n Context: {}", + info.file, + info.line, + error_context() + ); + //eprintln!("Backtrace:\n{}", info.backtrace); + } } let msg = extract_panic_message(err);