• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /// Initialize our panic handling hook to optionally log panics
2 #[cfg(feature = "log_panics")]
ensure_setup()3 pub fn ensure_setup() {
4     use std::sync::Once;
5     static INIT_BACKTRACES: Once = Once::new();
6     INIT_BACKTRACES.call_once(move || {
7         #[cfg(all(feature = "log_backtraces", not(target_os = "android")))]
8         {
9             std::env::set_var("RUST_BACKTRACE", "1");
10         }
11         // Turn on a panic hook which logs both backtraces and the panic
12         // "Location" (file/line). We do both in case we've been stripped,
13         // ).
14         std::panic::set_hook(Box::new(move |panic_info| {
15             let (file, line) = if let Some(loc) = panic_info.location() {
16                 (loc.file(), loc.line())
17             } else {
18                 // Apparently this won't happen but rust has reserved the
19                 // ability to start returning None from location in some cases
20                 // in the future.
21                 ("<unknown>", 0)
22             };
23             log::error!("### Rust `panic!` hit at file '{file}', line {line}");
24             #[cfg(all(feature = "log_backtraces", not(target_os = "android")))]
25             {
26                 log::error!("  Complete stack trace:\n{:?}", backtrace::Backtrace::new());
27             }
28         }));
29     });
30 }
31 
32 /// Initialize our panic handling hook to optionally log panics
33 #[cfg(not(feature = "log_panics"))]
ensure_setup()34 pub fn ensure_setup() {}
35