mirror of https://github.com/zkat/miette.git
feat(panic): improved backtrace handling
Fixes: https://github.com/zkat/miette/issues/92
This commit is contained in:
parent
0b445dc2b4
commit
b0453215f6
16
Cargo.toml
16
Cargo.toml
|
|
@ -26,6 +26,7 @@ supports-color = { version = "2.0.0", optional = true }
|
||||||
supports-unicode = { version = "2.0.0", optional = true }
|
supports-unicode = { version = "2.0.0", optional = true }
|
||||||
backtrace = { version = "0.3.61", optional = true }
|
backtrace = { version = "0.3.61", optional = true }
|
||||||
terminal_size = { version = "0.1.17", optional = true }
|
terminal_size = { version = "0.1.17", optional = true }
|
||||||
|
backtrace-ext = { version = "0.2.1", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
semver = "1.0.4"
|
semver = "1.0.4"
|
||||||
|
|
@ -41,19 +42,8 @@ lazy_static = "1.4"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = []
|
default = []
|
||||||
fancy-no-backtrace = [
|
fancy-no-backtrace = ["owo-colors", "is-terminal", "textwrap", "terminal_size", "supports-hyperlinks", "supports-color", "supports-unicode"]
|
||||||
"owo-colors",
|
fancy = ["fancy-no-backtrace", "backtrace", "backtrace-ext"]
|
||||||
"is-terminal",
|
|
||||||
"textwrap",
|
|
||||||
"terminal_size",
|
|
||||||
"supports-hyperlinks",
|
|
||||||
"supports-color",
|
|
||||||
"supports-unicode",
|
|
||||||
]
|
|
||||||
fancy = [
|
|
||||||
"fancy-no-backtrace",
|
|
||||||
"backtrace",
|
|
||||||
]
|
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
members = ["miette-derive"]
|
members = ["miette-derive"]
|
||||||
|
|
|
||||||
25
src/panic.rs
25
src/panic.rs
|
|
@ -1,6 +1,3 @@
|
||||||
#![cfg(feature = "fancy")]
|
|
||||||
use std::fmt::Write;
|
|
||||||
|
|
||||||
use backtrace::Backtrace;
|
use backtrace::Backtrace;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
|
|
@ -29,20 +26,22 @@ pub fn set_panic_hook() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Error, Diagnostic)]
|
#[derive(Debug, Error, Diagnostic)]
|
||||||
#[error("{0}{}", self.maybe_collect_backtrace())]
|
#[error("{0}{}", Panic::backtrace())]
|
||||||
#[diagnostic(help("set the `RUST_BACKTRACE=1` environment variable to display a backtrace."))]
|
#[diagnostic(help("set the `RUST_BACKTRACE=1` environment variable to display a backtrace."))]
|
||||||
struct Panic(String);
|
struct Panic(String);
|
||||||
|
|
||||||
impl Panic {
|
impl Panic {
|
||||||
fn maybe_collect_backtrace(&self) -> String {
|
fn backtrace() -> String {
|
||||||
|
use std::fmt::Write;
|
||||||
if let Ok(var) = std::env::var("RUST_BACKTRACE") {
|
if let Ok(var) = std::env::var("RUST_BACKTRACE") {
|
||||||
if !var.is_empty() && var != "0" {
|
if !var.is_empty() && var != "0" {
|
||||||
// This is all taken from human-panic: https://github.com/rust-cli/human-panic/blob/master/src/report.rs#L55-L107
|
|
||||||
const HEX_WIDTH: usize = std::mem::size_of::<usize>() + 2;
|
const HEX_WIDTH: usize = std::mem::size_of::<usize>() + 2;
|
||||||
//Padding for next lines after frame's address
|
// Padding for next lines after frame's address
|
||||||
const NEXT_SYMBOL_PADDING: usize = HEX_WIDTH + 6;
|
const NEXT_SYMBOL_PADDING: usize = HEX_WIDTH + 6;
|
||||||
let mut backtrace = String::new();
|
let mut backtrace = String::new();
|
||||||
for (idx, frame) in Backtrace::new().frames().iter().skip(26).enumerate() {
|
let trace = Backtrace::new();
|
||||||
|
let frames = backtrace_ext::short_frames_strict(&trace).enumerate();
|
||||||
|
for (idx, (frame, sub_frames)) in frames {
|
||||||
let ip = frame.ip();
|
let ip = frame.ip();
|
||||||
let _ = write!(backtrace, "\n{:4}: {:2$?}", idx, ip, HEX_WIDTH);
|
let _ = write!(backtrace, "\n{:4}: {:2$?}", idx, ip, HEX_WIDTH);
|
||||||
|
|
||||||
|
|
@ -52,10 +51,10 @@ impl Panic {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (idx, symbol) in symbols.iter().enumerate() {
|
for (idx, symbol) in symbols[sub_frames].iter().enumerate() {
|
||||||
//Print symbols from this address,
|
// Print symbols from this address,
|
||||||
//if there are several addresses
|
// if there are several addresses
|
||||||
//we need to put it on next line
|
// we need to put it on next line
|
||||||
if idx != 0 {
|
if idx != 0 {
|
||||||
let _ = write!(backtrace, "\n{:1$}", "", NEXT_SYMBOL_PADDING);
|
let _ = write!(backtrace, "\n{:1$}", "", NEXT_SYMBOL_PADDING);
|
||||||
}
|
}
|
||||||
|
|
@ -66,7 +65,7 @@ impl Panic {
|
||||||
let _ = write!(backtrace, " - <unknown>");
|
let _ = write!(backtrace, " - <unknown>");
|
||||||
}
|
}
|
||||||
|
|
||||||
//See if there is debug information with file name and line
|
// See if there is debug information with file name and line
|
||||||
if let (Some(file), Some(line)) = (symbol.filename(), symbol.lineno()) {
|
if let (Some(file), Some(line)) = (symbol.filename(), symbol.lineno()) {
|
||||||
let _ = write!(
|
let _ = write!(
|
||||||
backtrace,
|
backtrace,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue