chiark / gitweb /
backtraces: Print only when RUST_BACKTRACE=1
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 8 Aug 2021 14:00:18 +0000 (15:00 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 8 Aug 2021 14:00:18 +0000 (15:00 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
Cargo.lock
Cargo.toml
src/prelude.rs
src/reporter.rs

index 0c05f7d8d8d3d46e25c1e3b3caf2424dd9f9b269..0a7897ec95fe2e7e560e6d296ed1ca8df8b17e03 100644 (file)
@@ -426,6 +426,7 @@ dependencies = [
  "ipnet",
  "itertools",
  "lazy-regex",
+ "lazy_static",
  "log",
  "mime",
  "parking_lot",
index 1b1a3dff023b26badd24e9106a80aaa694b3a57c..8d27fef9a2fc7007cd110df9dbc4596f483db8e5 100644 (file)
@@ -38,6 +38,7 @@ itertools = "0.10"
 mime = "0.3"
 parking_lot = "0.11"
 regex = "1.5"
+lazy_static = "1.4"
 log = "0.4"
 sha2 = "0.9"
 structopt = "0.3"
index e09747e95d1fb7096cb37d7f5ff8ff6c117ec1d9..ceb57d1a4166938c2c02aca687a372bf9a8e7c18 100644 (file)
@@ -8,6 +8,7 @@ pub use std::convert::{Infallible, TryFrom, TryInto};
 pub use std::borrow::Cow;
 pub use std::cell::{RefCell, RefMut};
 pub use std::cmp::{min, max};
+pub use std::env;
 pub use std::fs;
 pub use std::fmt::{self, Debug, Display, Write as _};
 pub use std::future::Future;
@@ -34,6 +35,7 @@ pub use hyper_tls::HttpsConnector;
 pub use ipnet::IpNet;
 pub use itertools::{iproduct, Itertools};
 pub use lazy_regex::{regex_captures, regex_is_match, regex_replace_all};
+pub use lazy_static::lazy_static;
 pub use log::{trace, debug, info, warn, error};
 pub use structopt::StructOpt;
 pub use thiserror::Error;
index 49b784bf0e3bf1b582d42b81fb6ecafff334d4ad..0776dcee7d27f51bc3423d31465c9f406b36b54f 100644 (file)
@@ -117,7 +117,7 @@ use indenter::indented;
 
 #[derive(Debug)]
 struct EyreDedupHandler {
-  backtrace: Arc<parking_lot::Mutex<Backtrace>>,
+  backtrace: Option<Arc<parking_lot::Mutex<Backtrace>>>,
 }
 
 type EyreDynError<'r> = &'r (dyn std::error::Error + 'static);
@@ -161,17 +161,37 @@ impl eyre::EyreHandler for EyreDedupHandler {
       }
     }
 
-    let mut backtrace = self.backtrace.lock();
-    backtrace.resolve();
-    write!(f, "\n\nStack backtrace:\n{:?}", backtrace)?;
+    if let Some(bt) = &self.backtrace {
+      let mut bt = bt.lock();
+      bt.resolve();
+      write!(f, "\n\nStack backtrace:\n{:?}", bt)?;
+    }
   }
 }
 
 #[throws(AE)]
 pub fn dedup_eyre_setup() {
   eyre::set_hook(Box::new(|_error| {
-    let backtrace = Backtrace::new_unresolved();
-    let backtrace = Arc::new(backtrace.into());
+    lazy_static! {
+      static ref BACKTRACE: bool = {
+        match env::var("RUST_BACKTRACE") {
+          Ok(s) if s.starts_with("1") => true,
+          Ok(s) if s == "0" => false,
+          Err(env::VarError::NotPresent) => false,
+          x => {
+            eprintln!("warning: RUST_BACKTRACE not understood: {:?}", x);
+            false
+          },
+        }
+      };
+    }
+    let backtrace = if *BACKTRACE {
+      let bt = Backtrace::new_unresolved();
+      let bt = Arc::new(bt.into());
+      Some(bt)
+    } else {
+      None
+    };
     Box::new(EyreDedupHandler { backtrace })
   }))
     .context("set error handler")?;