Skip to content

Commit

Permalink
Merge pull request #133 from RalfJung/printing
Browse files Browse the repository at this point in the history
Print backtraces more like libstd does
  • Loading branch information
alexcrichton committed Dec 11, 2018
2 parents 5284115 + 8381403 commit f881a82
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down Expand Up @@ -230,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, " - <unresolved>")?;
write!(fmt, "<unresolved> ({:?})", ip)?;
continue
}
};
if symbols.len() == 0 {
write!(fmt, " - <no info>")?;
write!(fmt, "<no info> ({:?})", 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, " - <unknown>")?;
write!(fmt, "<unknown>")?;
}

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)?;
}
}
}
Expand All @@ -264,6 +272,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()
Expand Down

0 comments on commit f881a82

Please sign in to comment.