Skip to content

Commit

Permalink
Merge pull request #81 from jfrimmel/better-error-output
Browse files Browse the repository at this point in the history
Display additional ("aux") information and stacks
  • Loading branch information
jfrimmel authored Sep 17, 2024
2 parents a24bd4a + fa29054 commit fd17d94
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,6 @@ fn display_errors(errors: &[valgrind::xml::Error]) {
} else {
display_generic_error(error);
}
let mut info = Some("Info".cyan().bold());
error.stack_trace[0]
.frames
.iter()
.for_each(|frame| eprintln!("{:>12} at {}", info.take().unwrap_or_default(), frame));
}

let total: usize = errors.iter().map(|error| error.resources.bytes).sum();
Expand All @@ -59,6 +54,9 @@ fn display_leak(error: &valgrind::xml::Error) {
error.resources.blocks,
if error.resources.blocks == 1 { "" } else { "s" }
);

let stack = &error.stack_trace[0]; // always available
display_stack_trace("stack trace (user code at the bottom)", stack);
}

/// Nicely format a non-memory-leak error.
Expand All @@ -72,6 +70,29 @@ fn display_generic_error(error: &valgrind::xml::Error) {
.map(String::as_str)
.unwrap_or("unknown"),
);

let stack = &error.stack_trace[0]; // always available
display_stack_trace("main stack trace (user code at the bottom)", stack);
error
.stack_trace
.iter()
.skip(1)
.enumerate()
.map(|(index, stack)| (error.auxiliary_info.get(index), stack))
.for_each(|(msg, stack)| {
display_stack_trace(
msg.map_or_else(|| "additional stack trace", String::as_str),
stack,
)
})
}

fn display_stack_trace(msg: &str, stack: &valgrind::xml::Stack) {
eprintln!("{:>12} {}", "Info".cyan().bold(), msg);
stack
.frames
.iter()
.for_each(|frame| eprintln!(" at {}", frame));
}

fn main() {
Expand Down

0 comments on commit fd17d94

Please sign in to comment.