mirror of
https://github.com/eyre-rs/eyre.git
synced 2025-10-03 07:46:18 +00:00
update to match color-backtrace
This commit is contained in:
parent
c2ac4508ae
commit
20b490cd17
@ -10,6 +10,7 @@ edition = "2018"
|
|||||||
console = "0.10.0"
|
console = "0.10.0"
|
||||||
tracing-error = "0.1.2"
|
tracing-error = "0.1.2"
|
||||||
tracing-core = "0.1.10"
|
tracing-core = "0.1.10"
|
||||||
|
bat = "0.15.0"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
tracing-subscriber = "0.2.5"
|
tracing-subscriber = "0.2.5"
|
||||||
|
62
src/lib.rs
62
src/lib.rs
@ -1,7 +1,9 @@
|
|||||||
use console::style;
|
use console::style;
|
||||||
|
use std::env;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{BufRead, BufReader, ErrorKind};
|
use std::io::{BufRead, BufReader, ErrorKind};
|
||||||
|
use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
|
||||||
use tracing_error::SpanTrace;
|
use tracing_error::SpanTrace;
|
||||||
|
|
||||||
pub fn colorize(span_trace: &SpanTrace) -> impl fmt::Display + '_ {
|
pub fn colorize(span_trace: &SpanTrace) -> impl fmt::Display + '_ {
|
||||||
@ -29,26 +31,25 @@ struct Frame<'a> {
|
|||||||
fields: &'a str,
|
fields: &'a str,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Defines how verbose the backtrace is supposed to be.
|
fn enabled() -> bool {
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
// Cache the result of reading the environment variables to make
|
||||||
enum Verbosity {
|
// backtrace captures speedy, because otherwise reading environment
|
||||||
/// Print a small message including the panic payload and the panic location.
|
// variables every time can be somewhat slow.
|
||||||
Minimal,
|
static ENABLED: AtomicUsize = AtomicUsize::new(0);
|
||||||
/// Everything in `Minimal` and additionally print a backtrace.
|
match ENABLED.load(SeqCst) {
|
||||||
Medium,
|
0 => {}
|
||||||
/// Everything in `Medium` plus source snippets for all backtrace locations.
|
1 => return false,
|
||||||
Full,
|
_ => return true,
|
||||||
}
|
|
||||||
|
|
||||||
impl Verbosity {
|
|
||||||
/// Get the verbosity level from the `RUST_LIB_BACKTRACE` env variable.
|
|
||||||
fn from_env() -> Self {
|
|
||||||
match std::env::var("RUST_LIB_BACKTRACE") {
|
|
||||||
Ok(ref x) if x == "full" => Verbosity::Full,
|
|
||||||
Ok(_) => Verbosity::Medium,
|
|
||||||
Err(_) => Verbosity::Minimal,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
let enabled = match env::var("RUST_LIB_BACKTRACE") {
|
||||||
|
Ok(s) => s != "0",
|
||||||
|
Err(_) => match env::var("RUST_BACKTRACE") {
|
||||||
|
Ok(s) => s != "0",
|
||||||
|
Err(_) => false,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
ENABLED.store(enabled as usize + 1, SeqCst);
|
||||||
|
return enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Frame<'_> {
|
impl Frame<'_> {
|
||||||
@ -62,7 +63,7 @@ impl Frame<'_> {
|
|||||||
fn print_header(&self, i: u32, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn print_header(&self, i: u32, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"{:>4}: {}",
|
"{:>2}: {}",
|
||||||
i,
|
i,
|
||||||
style(format_args!(
|
style(format_args!(
|
||||||
"{}::{}",
|
"{}::{}",
|
||||||
@ -70,13 +71,12 @@ impl Frame<'_> {
|
|||||||
self.metadata.name()
|
self.metadata.name()
|
||||||
))
|
))
|
||||||
.red()
|
.red()
|
||||||
.dim()
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn print_fields(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn print_fields(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
if !self.fields.is_empty() {
|
if !self.fields.is_empty() {
|
||||||
write!(f, " with {}", style(self.fields).bold())?;
|
write!(f, " with {}", style(self.fields).cyan())?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -88,9 +88,9 @@ impl Frame<'_> {
|
|||||||
.metadata
|
.metadata
|
||||||
.line()
|
.line()
|
||||||
.map_or("<unknown line>".to_owned(), |x| x.to_string());
|
.map_or("<unknown line>".to_owned(), |x| x.to_string());
|
||||||
write!(f, "\n at {}:{}", file, lineno)?;
|
write!(f, "\n at {}:{}", file, lineno)?;
|
||||||
} else {
|
} else {
|
||||||
write!(f, "\n at <unknown source file>")?;
|
write!(f, "\n at <unknown source file>")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -118,10 +118,10 @@ impl Frame<'_> {
|
|||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"\n{}",
|
"\n{}",
|
||||||
style(format_args!("{:>10} > {}", cur_line_no, line.unwrap())).bold()
|
style(format_args!("{:>8} > {}", cur_line_no, line.unwrap())).bold()
|
||||||
)?;
|
)?;
|
||||||
} else {
|
} else {
|
||||||
write!(f, "\n{:>10} │ {}", cur_line_no, line.unwrap())?;
|
write!(f, "\n{:>8} │ {}", cur_line_no, line.unwrap())?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,9 +134,7 @@ impl fmt::Display for ColorSpanTrace<'_> {
|
|||||||
let mut err = Ok(());
|
let mut err = Ok(());
|
||||||
let mut span = 0;
|
let mut span = 0;
|
||||||
|
|
||||||
let verbosity = Verbosity::from_env();
|
writeln!(f, "{:━^80}\n", " SPANTRACE ")?;
|
||||||
|
|
||||||
writeln!(f, "{:━^80}", " SPANTRACE ")?;
|
|
||||||
self.span_trace.with_spans(|metadata, fields| {
|
self.span_trace.with_spans(|metadata, fields| {
|
||||||
let frame = Frame { metadata, fields };
|
let frame = Frame { metadata, fields };
|
||||||
|
|
||||||
@ -146,10 +144,8 @@ impl fmt::Display for ColorSpanTrace<'_> {
|
|||||||
|
|
||||||
try_bool!(frame.print(span, f), err);
|
try_bool!(frame.print(span, f), err);
|
||||||
|
|
||||||
match verbosity {
|
if enabled() {
|
||||||
Verbosity::Full => try_bool!(frame.print_source_if_avail(f), err),
|
try_bool!(frame.print_source_if_avail(f), err);
|
||||||
Verbosity::Medium => {}
|
|
||||||
Verbosity::Minimal => {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
span += 1;
|
span += 1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user