#backtrace #stack-trace #pretty #color

comfy-color-backtrace

Comfy fork of Colorful panic backtraces

1 unstable release

0.6.0 Sep 24, 2023

#178 in Value formatting

Download history 99/week @ 2024-07-22 96/week @ 2024-07-29 66/week @ 2024-08-05 90/week @ 2024-08-12 84/week @ 2024-08-19 86/week @ 2024-08-26 92/week @ 2024-09-02 70/week @ 2024-09-09 63/week @ 2024-09-16 93/week @ 2024-09-23 73/week @ 2024-09-30 10/week @ 2024-10-07 65/week @ 2024-10-14 53/week @ 2024-10-21 64/week @ 2024-10-28 65/week @ 2024-11-04

247 downloads per month
Used in 4 crates (via comfy-core)

MIT/Apache

34KB
571 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.5" }

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

Reducing transitive dependencies

In order to reduce transitive dependencies, you can disable the default enabled gimli-symbolize feature by adding a default-features = false clause to your Cargo.toml dependency entry, e.g.:

[dependencies]
color-backtrace = { version = "0.5", default-features = false }

This will reduce dependencies from ~50 → ~10. However, you'll pay for it with inaccurate source info on macOS and Linux

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–11MB
~113K SLoC