diff --git a/color-eyre/src/config.rs b/color-eyre/src/config.rs index f8f19b4..a913ead 100644 --- a/color-eyre/src/config.rs +++ b/color-eyre/src/config.rs @@ -219,9 +219,9 @@ impl<'a> fmt::Display for StyledFrame<'a> { )?; let v = if std::thread::panicking() { - panic_verbosity() + panic_verbosity(None) } else { - lib_verbosity() + lib_verbosity(None) }; // Maybe print source. @@ -386,6 +386,7 @@ impl Frame { pub struct HookBuilder { filters: Vec>, capture_span_trace_by_default: bool, + verbosity: Option, display_env_section: bool, #[cfg(feature = "track-caller")] display_location_section: bool, @@ -429,6 +430,7 @@ impl HookBuilder { HookBuilder { filters: vec![], capture_span_trace_by_default: false, + verbosity: None, display_env_section: true, #[cfg(feature = "track-caller")] display_location_section: true, @@ -616,6 +618,12 @@ impl HookBuilder { self } + /// Configures the verbosity to use. Overrides environment variables. + pub fn verbosity(mut self, verbosity: Verbosity) -> Self { + self.verbosity = Some(verbosity); + self + } + /// Configures the enviroment varible info section and whether or not it is displayed pub fn display_env_section(mut self, cond: bool) -> Self { self.display_env_section = cond; @@ -696,6 +704,7 @@ impl HookBuilder { section: self.panic_section, #[cfg(feature = "capture-spantrace")] capture_span_trace_by_default: self.capture_span_trace_by_default, + verbosity: self.verbosity, display_env_section: self.display_env_section, panic_message: self .panic_message @@ -713,6 +722,7 @@ impl HookBuilder { filters: panic_hook.filters.clone(), #[cfg(feature = "capture-spantrace")] capture_span_trace_by_default: self.capture_span_trace_by_default, + verbosity: self.verbosity, display_env_section: self.display_env_section, #[cfg(feature = "track-caller")] display_location_section: self.display_location_section, @@ -831,7 +841,7 @@ pub struct PanicReport<'a> { fn print_panic_info(report: &PanicReport<'_>, f: &mut fmt::Formatter<'_>) -> fmt::Result { report.hook.panic_message.display(report.panic_info, f)?; - let v = panic_verbosity(); + let v = panic_verbosity(None); let capture_bt = v != Verbosity::Minimal; let mut separated = f.header("\n\n"); @@ -913,6 +923,7 @@ pub struct PanicHook { theme: Theme, #[cfg(feature = "capture-spantrace")] capture_span_trace_by_default: bool, + verbosity: Option, display_env_section: bool, #[cfg(feature = "issue-url")] issue_url: Option, @@ -961,7 +972,7 @@ impl PanicHook { &'a self, panic_info: &'a std::panic::PanicInfo<'_>, ) -> PanicReport<'a> { - let v = panic_verbosity(); + let v = panic_verbosity(self.verbosity); let capture_bt = v != Verbosity::Minimal; #[cfg(feature = "capture-spantrace")] @@ -992,6 +1003,7 @@ pub struct EyreHook { filters: Arc<[Box]>, #[cfg(feature = "capture-spantrace")] capture_span_trace_by_default: bool, + verbosity: Option, display_env_section: bool, #[cfg(feature = "track-caller")] display_location_section: bool, @@ -1014,10 +1026,12 @@ type HookFunc = Box< impl EyreHook { #[allow(unused_variables)] pub(crate) fn default(&self, error: &(dyn std::error::Error + 'static)) -> crate::Handler { - let backtrace = if lib_verbosity() != Verbosity::Minimal { - Some(backtrace::Backtrace::new()) - } else { - None + let backtrace = { + if lib_verbosity(self.verbosity) != Verbosity::Minimal { + Some(backtrace::Backtrace::new()) + } else { + None + } }; #[cfg(feature = "capture-spantrace")] @@ -1156,14 +1170,25 @@ impl fmt::Display for BacktraceFormatter<'_> { } } +/// Verbosity of error and panic messages. +/// +/// For previews or more information see the [crate documentation](crate#multiple-report-format-verbosity-levels) #[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy)] -pub(crate) enum Verbosity { +pub enum Verbosity { + /// Minimal verbosity that does not include a `Backtrace`. Minimal, + /// The "short format" which additionally captures a `Backtrace`. Medium, + /// The full verbose format which will include context of the source files where the error + /// originated from, assuming it can find them on the disk. Full, } -pub(crate) fn panic_verbosity() -> Verbosity { +pub(crate) fn panic_verbosity(verbosity: Option) -> Verbosity { + if let Some(v) = verbosity { + return v; + } + match env::var("RUST_BACKTRACE") { Ok(s) if s == "full" => Verbosity::Full, Ok(s) if s != "0" => Verbosity::Medium, @@ -1171,7 +1196,11 @@ pub(crate) fn panic_verbosity() -> Verbosity { } } -pub(crate) fn lib_verbosity() -> Verbosity { +pub(crate) fn lib_verbosity(verbosity: Option) -> Verbosity { + if let Some(v) = verbosity { + return v; + } + match env::var("RUST_LIB_BACKTRACE").or_else(|_| env::var("RUST_BACKTRACE")) { Ok(s) if s == "full" => Verbosity::Full, Ok(s) if s != "0" => Verbosity::Medium, diff --git a/color-eyre/src/writers.rs b/color-eyre/src/writers.rs index ce828af..b1bbd3d 100644 --- a/color-eyre/src/writers.rs +++ b/color-eyre/src/writers.rs @@ -189,9 +189,9 @@ pub(crate) struct EnvSection<'a> { impl fmt::Display for EnvSection<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let v = if std::thread::panicking() { - panic_verbosity() + panic_verbosity(None) } else { - lib_verbosity() + lib_verbosity(None) }; write!(f, "{}", BacktraceOmited(!self.bt_captured))?;