Skip to content

Commit c1fcea0

Browse files
committed
Add extra logging
Update private.rs Update private.rs Update private.rs Update private.rs Update Cargo.toml Update private.rs
1 parent acae2b0 commit c1fcea0

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

godot-core/src/private.rs

+24-14
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,11 @@ where
331331
let info: Arc<Mutex<Option<GodotPanicInfo>>> = Arc::new(Mutex::new(None));
332332

333333
// Back up previous hook, set new one
334-
let prev_hook = std::panic::take_hook();
335-
{
334+
#[cfg(debug_assertions)]
335+
let prev_hook = {
336336
let info = info.clone();
337+
let prev_hook = std::panic::take_hook();
338+
337339
std::panic::set_hook(Box::new(move |panic_info| {
338340
if let Some(location) = panic_info.location() {
339341
*info.lock().unwrap() = Some(GodotPanicInfo {
@@ -345,10 +347,15 @@ where
345347
eprintln!("panic occurred, but can't get location information");
346348
}
347349
}));
348-
}
350+
351+
prev_hook
352+
};
349353

350354
// Run code that should panic, restore hook
351355
let panic = std::panic::catch_unwind(code);
356+
357+
// Restore the previous panic hook if in Debug mode
358+
#[cfg(debug_assertions)]
352359
std::panic::set_hook(prev_hook);
353360

354361
match panic {
@@ -358,17 +365,20 @@ where
358365
// TODO write custom panic handler and move this there, before panic backtrace printing
359366
flush_stdout();
360367

361-
let guard = info.lock().unwrap();
362-
let info = guard.as_ref().expect("no panic info available");
363-
364-
if print {
365-
godot_error!(
366-
"Rust function panicked at {}:{}.\n Context: {}",
367-
info.file,
368-
info.line,
369-
error_context()
370-
);
371-
//eprintln!("Backtrace:\n{}", info.backtrace);
368+
// Handle panic info only in Debug mode
369+
#[cfg(debug_assertions)]
370+
{
371+
let guard = info.lock().unwrap();
372+
let info = guard.as_ref().expect("no panic info available");
373+
if print {
374+
godot_error!(
375+
"Rust function panicked at {}:{}.\n Context: {}",
376+
info.file,
377+
info.line,
378+
error_context()
379+
);
380+
//eprintln!("Backtrace:\n{}", info.backtrace);
381+
}
372382
}
373383

374384
let msg = extract_panic_message(err);

0 commit comments

Comments
 (0)