From 1ca7b8a930034cbaed6947d9343abf5b1e9977a7 Mon Sep 17 00:00:00 2001 From: TheTollingBell Date: Tue, 13 Aug 2024 18:08:24 -0400 Subject: [PATCH 1/3] add support to specify verbosity in HookBuilder --- color-eyre/src/config.rs | 36 ++++++++++++++++++++++++++---------- color-eyre/src/writers.rs | 4 ++-- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/color-eyre/src/config.rs b/color-eyre/src/config.rs index f8f19b4..86e013c 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, @@ -696,6 +698,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 +716,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 +835,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 +917,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 +966,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 +997,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 +1020,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")] @@ -1163,7 +1171,11 @@ pub(crate) enum Verbosity { 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 +1183,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))?; From 18b28e6ae16db49c03c2a36fbd7fbccf40da78db Mon Sep 17 00:00:00 2001 From: TheTollingBell Date: Tue, 13 Aug 2024 18:23:51 -0400 Subject: [PATCH 2/3] actually allow modifying Verbosity in HookBuilder Made the Verbosity public and added documentation Added verbosity method to HookBuilder --- color-eyre/src/config.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/color-eyre/src/config.rs b/color-eyre/src/config.rs index 86e013c..d99f314 100644 --- a/color-eyre/src/config.rs +++ b/color-eyre/src/config.rs @@ -618,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; @@ -1164,10 +1170,17 @@ 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, } From fd8f3d22eee5a660951761d75b8d58c66f597d79 Mon Sep 17 00:00:00 2001 From: TheTollingBell Date: Tue, 13 Aug 2024 18:25:12 -0400 Subject: [PATCH 3/3] ran rustfmt --- color-eyre/src/config.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/color-eyre/src/config.rs b/color-eyre/src/config.rs index d99f314..a913ead 100644 --- a/color-eyre/src/config.rs +++ b/color-eyre/src/config.rs @@ -1186,7 +1186,7 @@ pub enum Verbosity { pub(crate) fn panic_verbosity(verbosity: Option) -> Verbosity { if let Some(v) = verbosity { - return v + return v; } match env::var("RUST_BACKTRACE") { @@ -1198,7 +1198,7 @@ pub(crate) fn panic_verbosity(verbosity: Option) -> Verbosity { pub(crate) fn lib_verbosity(verbosity: Option) -> Verbosity { if let Some(v) = verbosity { - return v + return v; } match env::var("RUST_LIB_BACKTRACE").or_else(|_| env::var("RUST_BACKTRACE")) {