print panics via eprinln to leverage test io capture (#42)

* print panics via eprinln to leverage test io capture

* move things into a display impl to only acquire the lock once

* unfuck indentation

* use original identifier for output stream
This commit is contained in:
Jane Lusby 2020-07-26 11:32:51 -07:00 committed by GitHub
parent 1df9ec5f44
commit 318241a118
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -375,21 +375,21 @@ fn eyre_frame_filters(frames: &mut Vec<&Frame>) {
}); });
} }
struct PanicPrinter<'a>(&'a std::panic::PanicInfo<'a>);
impl fmt::Display for PanicPrinter<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
print_panic_info(self, f)
}
}
fn install_panic_hook() { fn install_panic_hook() {
std::panic::set_hook(Box::new(move |pi| { std::panic::set_hook(Box::new(|pi| eprintln!("{}", PanicPrinter(pi))))
if let Err(e) = print_panic_info(pi) {
// Panicking while handling a panic would send us into a deadlock,
// so we just print the error to stderr instead.
eprintln!("Error while printing panic: {:?}", e);
}
}))
} }
fn print_panic_info(pi: &std::panic::PanicInfo<'_>) -> std::io::Result<()> { fn print_panic_info(printer: &PanicPrinter<'_>, out: &mut fmt::Formatter<'_>) -> fmt::Result {
use std::io::Write; let pi = printer.0;
let stdout = std::io::stdout();
let mut out = stdout.lock();
writeln!(out, "{}", Red.paint("The application panicked (crashed)."))?; writeln!(out, "{}", Red.paint("The application panicked (crashed)."))?;
// Print panic message. // Print panic message.
@ -418,19 +418,16 @@ fn print_panic_info(pi: &std::panic::PanicInfo<'_>) -> std::io::Result<()> {
// Print some info on how to increase verbosity. // Print some info on how to increase verbosity.
if v == Verbosity::Minimal { if v == Verbosity::Minimal {
write!(out, "\nBacktrace omitted.\n\nRun with ")?; write!(out, "\nBacktrace omitted.\n\nRun with ")?;
// out.set_color(&self.colors.env_var)?;
write!(out, "RUST_BACKTRACE=1")?; write!(out, "RUST_BACKTRACE=1")?;
writeln!(out, " environment variable to display it.")?; writeln!(out, " environment variable to display it.")?;
} else { } else {
// This text only makes sense if frames are displayed. // This text only makes sense if frames are displayed.
write!(out, "\nRun with ")?; write!(out, "\nRun with ")?;
// out.set_color(&self.colors.env_var)?;
write!(out, "COLORBT_SHOW_HIDDEN=1")?; write!(out, "COLORBT_SHOW_HIDDEN=1")?;
writeln!(out, " environment variable to disable frame filtering.")?; writeln!(out, " environment variable to disable frame filtering.")?;
} }
if v <= Verbosity::Medium { if v <= Verbosity::Medium {
write!(out, "Run with ")?; write!(out, "Run with ")?;
// out.set_color(&self.colors.env_var)?;
write!(out, "RUST_BACKTRACE=full")?; write!(out, "RUST_BACKTRACE=full")?;
writeln!(out, " to include source snippets.")?; writeln!(out, " to include source snippets.")?;
} }