From f7fac8eed50bfc46c0e057e3f321828049b157cd Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Fri, 1 May 2020 13:18:33 -0700 Subject: [PATCH 1/4] Add support for RUST_LIB_BACKTRACE variable --- src/lib.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 00d4e7a..ea33e3b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,6 +36,7 @@ //! [medium](Verbosity::Medium) and `RUST_BACKTRACE=full` to //! [full](Verbosity::Full) verbosity levels. +use std::env; use std::fs::File; use std::io::{BufRead, BufReader, ErrorKind}; use std::panic::PanicInfo; @@ -73,13 +74,19 @@ pub enum Verbosity { impl Verbosity { /// Get the verbosity level from the `RUST_BACKTRACE` env variable. pub fn from_env() -> Self { - match std::env::var("RUST_BACKTRACE") { - Ok(ref x) if x == "full" => Verbosity::Full, - Ok(_) => Verbosity::Medium, - Err(_) => Verbosity::Minimal, + match read_env() { + Some(ref x) if x == "full" => Verbosity::Full, + Some(_) => Verbosity::Medium, + None => Verbosity::Minimal, } } } + +fn read_env() -> Option { + env::var("RUST_LIB_BACKTRACE") + .or_else(|_| env::var("RUST_BACKTRACE")) + .ok() +} // ============================================================================================== // // [Panic handler and install logic] // // ============================================================================================== // From ac1fdf39bf808d0bf566f0964656d704bce0d20a Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Wed, 6 May 2020 10:23:16 -0700 Subject: [PATCH 2/4] add lib_from_env --- src/lib.rs | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ea33e3b..b0e5f41 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -80,13 +80,25 @@ impl Verbosity { None => Verbosity::Minimal, } } + + pub fn lib_from_env() -> Self { + match read_lib_env() { + Some(ref x) if x == "full" => Verbosity::Full, + Some(_) => Verbosity::Medium, + None => Verbosity::Minimal, + } + } } -fn read_env() -> Option { +fn read_lib_env() -> Option { env::var("RUST_LIB_BACKTRACE") .or_else(|_| env::var("RUST_BACKTRACE")) .ok() } + +fn read_env() -> Option { + env::var("RUST_BACKTRACE").ok() +} // ============================================================================================== // // [Panic handler and install logic] // // ============================================================================================== // @@ -399,7 +411,9 @@ pub type Settings = BacktracePrinter; pub struct BacktracePrinter { message: String, verbosity: Verbosity, + lib_verbosity: Verbosity, strip_function_hash: bool, + is_panic_handler: bool, colors: ColorScheme, } @@ -407,9 +421,11 @@ impl Default for BacktracePrinter { fn default() -> Self { Self { verbosity: Verbosity::from_env(), + lib_verbosity: Verbosity::lib_from_env(), message: "The application panicked (crashed).".to_owned(), strip_function_hash: false, colors: ColorScheme::classic(), + is_panic_handler: false, } } } @@ -439,12 +455,20 @@ impl BacktracePrinter { /// Controls the verbosity level. /// - /// Defaults to `Verbosity::get_env()`. + /// Defaults to `Verbosity::from_env()`. pub fn verbosity(mut self, v: Verbosity) -> Self { self.verbosity = v; self } + /// Controls the lib verbosity level. + /// + /// Defaults to `Verbosity::lib_from_env()`. + pub fn lib_verbosity(mut self, v: Verbosity) -> Self { + self.lib_verbosity = v; + self + } + /// Controls whether the hash part of functions is printed stripped. /// /// Defaults to `false`. @@ -472,6 +496,7 @@ impl BacktracePrinter { self, out: impl WriteColor + Sync + Send + 'static, ) -> Box) + 'static + Sync + Send> { + self.is_panic_handler = true; let out_stream_mutex = Mutex::new(out); Box::new(move |pi| { let mut lock = out_stream_mutex.lock().unwrap(); @@ -604,15 +629,15 @@ impl BacktracePrinter { } // Print some info on how to increase verbosity. - if self.verbosity == Verbosity::Minimal { + if self.current_verbosity() == Verbosity::Minimal { write!(out, "\nBacktrace omitted. Run with ")?; out.set_color(&self.colors.env_var)?; write!(out, "RUST_BACKTRACE=1")?; out.reset()?; writeln!(out, " environment variable to display it.")?; } - if self.verbosity <= Verbosity::Medium { - if self.verbosity == Verbosity::Medium { + if self.current_verbosity <= Verbosity::Medium { + if self.current_verbosity == Verbosity::Medium { // If exactly medium, no newline was printed before. writeln!(out)?; } @@ -624,12 +649,20 @@ impl BacktracePrinter { writeln!(out, " to include source snippets.")?; } - if self.verbosity >= Verbosity::Medium { + if self.current_verbosity >= Verbosity::Medium { self.print_trace(&backtrace::Backtrace::new(), out)?; } Ok(()) } + + fn current_verbosity(&self) -> Verbosity { + if self.is_panic_handler { + self.verbosity + } else { + self.lib_verbosity + } + } } // ============================================================================================== // From 991381970a224e34f6b3e4792aa1264a60e06823 Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Wed, 6 May 2020 10:25:49 -0700 Subject: [PATCH 3/4] actually try compiling lol --- src/lib.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7ff9f47..31f34e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -545,7 +545,7 @@ impl BacktracePrinter { /// /// This can be used if you want to combine the handler with other handlers. pub fn into_panic_handler( - self, + mut self, out: impl WriteColor + Sync + Send + 'static, ) -> Box) + 'static + Sync + Send> { self.is_panic_handler = true; @@ -692,8 +692,8 @@ impl BacktracePrinter { out.reset()?; writeln!(out, " environment variable to display it.")?; } - if self.current_verbosity <= Verbosity::Medium { - if self.current_verbosity == Verbosity::Medium { + if self.current_verbosity() <= Verbosity::Medium { + if self.current_verbosity() == Verbosity::Medium { // If exactly medium, no newline was printed before. writeln!(out)?; } @@ -705,7 +705,7 @@ impl BacktracePrinter { writeln!(out, " to include source snippets.")?; } - if self.current_verbosity >= Verbosity::Medium { + if self.current_verbosity() >= Verbosity::Medium { self.print_trace(&backtrace::Backtrace::new(), out)?; } From 16853eb9901c3947f320192dd231ab8778a3838f Mon Sep 17 00:00:00 2001 From: Jane Lusby Date: Wed, 6 May 2020 10:28:56 -0700 Subject: [PATCH 4/4] deduplicate code --- src/lib.rs | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 31f34e6..1e6c546 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,15 +74,19 @@ pub enum Verbosity { impl Verbosity { /// Get the verbosity level from the `RUST_BACKTRACE` env variable. pub fn from_env() -> Self { - match read_env() { - Some(ref x) if x == "full" => Verbosity::Full, - Some(_) => Verbosity::Medium, - None => Verbosity::Minimal, - } + Self::convert_env(env::var("RUST_BACKTRACE").ok()) } pub fn lib_from_env() -> Self { - match read_lib_env() { + Self::convert_env( + env::var("RUST_LIB_BACKTRACE") + .or_else(|_| env::var("RUST_BACKTRACE")) + .ok(), + ) + } + + fn convert_env(env: Option) -> Self { + match env { Some(ref x) if x == "full" => Verbosity::Full, Some(_) => Verbosity::Medium, None => Verbosity::Minimal, @@ -90,15 +94,6 @@ impl Verbosity { } } -fn read_lib_env() -> Option { - env::var("RUST_LIB_BACKTRACE") - .or_else(|_| env::var("RUST_BACKTRACE")) - .ok() -} - -fn read_env() -> Option { - env::var("RUST_BACKTRACE").ok() -} // ============================================================================================== // // [Panic handler and install logic] // // ============================================================================================== //