From a71dcafb255bbee7c22cdb94340a4082c8bc33c0 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 11 Dec 2018 15:27:23 +0100 Subject: [PATCH 1/2] add Display impl forwarding to Debug; remark on the Debug/Display impls --- src/capture.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/capture.rs b/src/capture.rs index 55e18ddb9..c296687e1 100644 --- a/src/capture.rs +++ b/src/capture.rs @@ -10,6 +10,9 @@ use types::c_void; /// /// This structure can be used to capture a backtrace at various points in a /// program and later used to inspect what the backtrace was at that time. +/// +/// `Backtrace` supports pretty-printing of backtraces by implementing +/// `Display` and `Debug` (which do the same thing). #[derive(Clone)] #[cfg_attr(feature = "serialize-rustc", derive(RustcDecodable, RustcEncodable))] #[cfg_attr(feature = "serialize-serde", derive(Deserialize, Serialize))] @@ -264,6 +267,12 @@ impl fmt::Debug for Backtrace { } } +impl fmt::Display for Backtrace { + fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { + fmt::Debug::fmt(self, fmt) + } +} + impl Default for Backtrace { fn default() -> Backtrace { Backtrace::new() From 8381403bce759fd7198f1fb37558c828af196f00 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Tue, 11 Dec 2018 15:44:21 +0100 Subject: [PATCH 2/2] print backtraces more like libstd does --- src/capture.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/capture.rs b/src/capture.rs index c296687e1..133b5fc36 100644 --- a/src/capture.rs +++ b/src/capture.rs @@ -233,32 +233,37 @@ impl fmt::Debug for Backtrace { for (idx, frame) in iter.enumerate() { let ip = frame.ip(); - write!(fmt, "\n{:4}: {:2$?}", idx, ip, hex_width)?; + write!(fmt, "\n{:4}: ", idx)?; let symbols = match frame.symbols { Some(ref s) => s, None => { - write!(fmt, " - ")?; + write!(fmt, " ({:?})", ip)?; continue } }; if symbols.len() == 0 { - write!(fmt, " - ")?; + write!(fmt, " ({:?})", ip)?; + continue; } for (idx, symbol) in symbols.iter().enumerate() { if idx != 0 { - write!(fmt, "\n {:1$}", "", hex_width)?; + write!(fmt, "\n ")?; } if let Some(name) = symbol.name() { - write!(fmt, " - {}", name)?; + write!(fmt, "{}", name)?; } else { - write!(fmt, " - ")?; + write!(fmt, "")?; + } + + if idx == 0 { + write!(fmt, " ({:?})", ip)?; } if let (Some(file), Some(line)) = (symbol.filename(), symbol.lineno()) { - write!(fmt, "\n {:3$}at {}:{}", "", file.display(), line, hex_width)?; + write!(fmt, "\n at {}:{}", file.display(), line)?; } } }