@@ -200,6 +200,7 @@ pub fn is_class_runtime(is_tool: bool) -> bool {
200
200
// ----------------------------------------------------------------------------------------------------------------------------------------------
201
201
// Panic handling
202
202
203
+ #[ cfg( debug_assertions) ]
203
204
#[ derive( Debug ) ]
204
205
struct GodotPanicInfo {
205
206
line : u32 ,
@@ -328,12 +329,15 @@ where
328
329
F : FnOnce ( ) -> R + std:: panic:: UnwindSafe ,
329
330
S : std:: fmt:: Display ,
330
331
{
332
+ #[ cfg( debug_assertions) ]
331
333
let info: Arc < Mutex < Option < GodotPanicInfo > > > = Arc :: new ( Mutex :: new ( None ) ) ;
332
334
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 = {
336
338
let info = info. clone ( ) ;
339
+ let prev_hook = std:: panic:: take_hook ( ) ;
340
+
337
341
std:: panic:: set_hook ( Box :: new ( move |panic_info| {
338
342
if let Some ( location) = panic_info. location ( ) {
339
343
* info. lock ( ) . unwrap ( ) = Some ( GodotPanicInfo {
@@ -345,30 +349,38 @@ where
345
349
eprintln ! ( "panic occurred, but can't get location information" ) ;
346
350
}
347
351
} ) ) ;
348
- }
349
352
350
- // Run code that should panic, restore hook
353
+ prev_hook
354
+ } ;
355
+
356
+ // Run code that should panic, restore hook.
351
357
let panic = std:: panic:: catch_unwind ( code) ;
358
+
359
+ // Restore the previous panic hook if in Debug mode.
360
+ #[ cfg( debug_assertions) ]
352
361
std:: panic:: set_hook ( prev_hook) ;
353
362
354
363
match panic {
355
364
Ok ( result) => Ok ( result) ,
356
365
Err ( err) => {
357
366
// 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.
359
368
flush_stdout ( ) ;
360
369
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
+ }
372
384
}
373
385
374
386
let msg = extract_panic_message ( err) ;
0 commit comments