fn default_hook()

in sgx_tstd/src/panicking.rs [250:307]


fn default_hook(info: &PanicInfo<'_>) {
    // If this is a double panic, make sure that we print a backtrace
    // for this panic. Otherwise only print it if logging is enabled.
    #[cfg(feature = "backtrace")]
    let backtrace = if panic_count::get_count() >= 2 {
        BacktraceStyle::full()
    } else {
        crate::panic::get_backtrace_style()
    };

    // The current implementation always returns `Some`.
    let location = info.location().unwrap();

    let msg = match info.payload().downcast_ref::<&'static str>() {
        Some(s) => *s,
        None => match info.payload().downcast_ref::<String>() {
            Some(s) => &s[..],
            None => "Box<dyn Any>",
        },
    };
    let thread = thread_info::current_thread();
    let name = thread.as_ref().and_then(|t| t.name()).unwrap_or("<unnamed>");

    let write = |err: &mut dyn crate::io::Write| {
        let _ = writeln!(err, "thread '{name}' panicked at '{msg}', {location}");

        #[cfg(feature = "backtrace")]
        {
            static FIRST_PANIC: AtomicBool = AtomicBool::new(true);

            match backtrace {
                Some(BacktraceStyle::Short) => {
                    drop(backtrace::print(err, crate::sys::backtrace::PrintFmt::Short))
                }
                Some(BacktraceStyle::Full) => {
                    drop(backtrace::print(err, crate::sys::backtrace::PrintFmt::Full))
                }
                Some(BacktraceStyle::Off) => {
                    if FIRST_PANIC.swap(false, Ordering::SeqCst) {
                        let _ = writeln!(
                            err,
                            "note: call backtrace::enable_backtrace with 'PrintFormat::Short/Full' for a backtrace."
                        );
                    }
                }
                // If backtraces aren't supported, do nothing.
                None => {}
            }
        }
    };

    if let Some(local) = set_output_capture(None) {
        write(&mut *local.lock().unwrap_or_else(|e| e.into_inner()));
        set_output_capture(Some(local));
    } else if let Some(mut out) = panic_output() {
        write(&mut out);
    }
}