chore: refactor backtrace style in panic

This commit is contained in:
Konippi 2024-08-02 23:58:05 +09:00
parent 2cec7a85ed
commit a798e0f488

View File

@ -463,11 +463,10 @@ static SHOULD_CAPTURE: AtomicU8 = AtomicU8::new(0);
/// environment variable; see the details in [`get_backtrace_style`]. /// environment variable; see the details in [`get_backtrace_style`].
#[unstable(feature = "panic_backtrace_config", issue = "93346")] #[unstable(feature = "panic_backtrace_config", issue = "93346")]
pub fn set_backtrace_style(style: BacktraceStyle) { pub fn set_backtrace_style(style: BacktraceStyle) {
if !cfg!(feature = "backtrace") { if cfg!(feature = "backtrace") {
// If the `backtrace` feature of this crate isn't enabled, skip setting. // If the `backtrace` feature of this crate is enabled, set the backtrace style.
return; SHOULD_CAPTURE.store(style.as_u8(), Ordering::Release);
} }
SHOULD_CAPTURE.store(style.as_u8(), Ordering::Release);
} }
/// Checks whether the standard library's panic hook will capture and print a /// Checks whether the standard library's panic hook will capture and print a
@ -503,21 +502,13 @@ pub fn get_backtrace_style() -> Option<BacktraceStyle> {
return Some(style); return Some(style);
} }
let format = crate::env::var_os("RUST_BACKTRACE") let format = match crate::env::var_os("RUST_BACKTRACE") {
.map(|x| { Some(x) if &x == "0" => BacktraceStyle::Off,
if &x == "0" { Some(x) if &x == "full" => BacktraceStyle::Full,
BacktraceStyle::Off Some(_) => BacktraceStyle::Short,
} else if &x == "full" { None if crate::sys::FULL_BACKTRACE_DEFAULT => BacktraceStyle::Full,
BacktraceStyle::Full None => BacktraceStyle::Off,
} else { };
BacktraceStyle::Short
}
})
.unwrap_or(if crate::sys::FULL_BACKTRACE_DEFAULT {
BacktraceStyle::Full
} else {
BacktraceStyle::Off
});
set_backtrace_style(format); set_backtrace_style(format);
Some(format) Some(format)
} }