16 releases

0.6.1 Oct 22, 2023
0.6.0 Jul 30, 2023
0.5.1 Apr 25, 2021
0.5.0 Nov 21, 2020
0.2.2 Jun 29, 2019

#15 in Value formatting

Download history 9392/week @ 2024-08-18 7209/week @ 2024-08-25 8976/week @ 2024-09-01 8087/week @ 2024-09-08 7544/week @ 2024-09-15 10256/week @ 2024-09-22 7795/week @ 2024-09-29 7498/week @ 2024-10-06 6354/week @ 2024-10-13 8525/week @ 2024-10-20 7042/week @ 2024-10-27 8960/week @ 2024-11-03 9132/week @ 2024-11-10 7788/week @ 2024-11-17 7451/week @ 2024-11-24 8996/week @ 2024-12-01

34,192 downloads per month
Used in 102 crates (72 directly)

MIT/Apache

34KB
569 lines

color-backtrace

Crates.io docs.rs MIT licensed Apache 2.0 licensed

A Rust library that makes panics a little less painful by nicely colorizing them and printing the relevant source snippets.

[dependencies]
color-backtrace = { version = "0.6" }

To enable it, simply place this code somewhere in your app initialization code:

color_backtrace::install();

If you want to customize some settings, you can instead do:

use color_backtrace::{default_output_stream, BacktracePrinter};
BacktracePrinter::new().message("Custom message!").install(default_output_stream());

Features

  • Colorize backtraces to be easier on the eyes
  • Show source snippets if source files are found on disk
  • Print frames of application code vs dependencies in different color
  • Hide all the frames after the panic was already initiated
  • Hide language runtime initialization frames

Usage in tests

Unfortunately, defining custom init functions run before tests are started is currently not supported in Rust. Since initializing color-backtrace in each and every test is tedious even when wrapping it into a function, I recommended using the ctor crate for this.

Somewhere, preferably in your crate's main module, put the following code:

#[cfg(test)]
mod tests {
    use ctor::ctor;

    #[ctor]
    fn init_color_backtrace() {
        color_backtrace::install();
    }
}

You can also do this outside of a #[cfg(test)] section, in which case the panic handler is installed for both test and regular runs.

Screenshot

Screenshot

Dependencies

~2–10MB
~110K SLoC