Skip to content

Commit 2db5ca4

Browse files
committed
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
1 parent acae2b0 commit 2db5ca4

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

godot-core/src/private.rs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ pub fn is_class_runtime(is_tool: bool) -> bool {
200200
// ----------------------------------------------------------------------------------------------------------------------------------------------
201201
// Panic handling
202202

203+
#[cfg(debug_assertions)]
203204
#[derive(Debug)]
204205
struct GodotPanicInfo {
205206
line: u32,
@@ -328,12 +329,15 @@ where
328329
F: FnOnce() -> R + std::panic::UnwindSafe,
329330
S: std::fmt::Display,
330331
{
332+
#[cfg(debug_assertions)]
331333
let info: Arc<Mutex<Option<GodotPanicInfo>>> = Arc::new(Mutex::new(None));
332334

333-
// Back up previous hook, set new one
334-
let prev_hook = std::panic::take_hook();
335-
{
335+
// Back up previous hook, set new one.
336+
#[cfg(debug_assertions)]
337+
let prev_hook = {
336338
let info = info.clone();
339+
let prev_hook = std::panic::take_hook();
340+
337341
std::panic::set_hook(Box::new(move |panic_info| {
338342
if let Some(location) = panic_info.location() {
339343
*info.lock().unwrap() = Some(GodotPanicInfo {
@@ -345,30 +349,38 @@ where
345349
eprintln!("panic occurred, but can't get location information");
346350
}
347351
}));
348-
}
349352

350-
// Run code that should panic, restore hook
353+
prev_hook
354+
};
355+
356+
// Run code that should panic, restore hook.
351357
let panic = std::panic::catch_unwind(code);
358+
359+
// Restore the previous panic hook if in Debug mode.
360+
#[cfg(debug_assertions)]
352361
std::panic::set_hook(prev_hook);
353362

354363
match panic {
355364
Ok(result) => Ok(result),
356365
Err(err) => {
357366
// Flush, to make sure previous Rust output (e.g. test announcement, or debug prints during app) have been printed
358-
// TODO write custom panic handler and move this there, before panic backtrace printing
367+
// TODO write custom panic handler and move this there, before panic backtrace printing.
359368
flush_stdout();
360369

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);
370+
// Handle panic info only in Debug mode.
371+
#[cfg(debug_assertions)]
372+
{
373+
let guard = info.lock().unwrap();
374+
let info = guard.as_ref().expect("no panic info available");
375+
if print {
376+
godot_error!(
377+
"Rust function panicked at {}:{}.\n Context: {}",
378+
info.file,
379+
info.line,
380+
error_context()
381+
);
382+
//eprintln!("Backtrace:\n{}", info.backtrace);
383+
}
372384
}
373385

374386
let msg = extract_panic_message(err);

0 commit comments

Comments
 (0)