panic-context 0.1.0

Show manually-maintained context information on panic
Documentation
  • Coverage
  • 44.44%
    4 out of 9 items documented3 out of 8 items with examples
  • Size
  • Source code size: 15.16 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.64 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 11s Average build duration of successful builds.
  • all releases: 11s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Homepage
  • kriomant/panic-context
    5 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • kriomant

Panic context

This library allows to print manually-maintained messages on panic.

When your program panics, it prints backtrace. However, if panic occurs inside loop, it is not clear which iteration was the cause. It is possible to use log, but printing slows down execution considerably and you get lots of entries, while only last ones are required.

Panic context lets you set value which is remembered, but not printed anywhere until panic occurs. It is also automatically forgotten at the end of scope.

Example

#[macro_use] extern crate panic_context;

use panic_context::panic_context;

static ITEMS: &[&str] = &["foo", "bar", "yo", "nope"];

fn get_len(item: &str) -> usize { item.len() }
fn calc_sig(item: &str) -> &str { &item[3..] }

fn main() {
    let step = panic_context("step: ");

    step.update("calculate lengths");
    for item in ITEMS {
        panic_context!("item: {}", item);
        get_len(item);
    }

    step.update("calculate signatures");
    for item in ITEMS {
        panic_context!("item: {}", item);
        calc_sig(item);
    }

    panic!("boom!");
}

When this code panics inside calc_sig, you will see:

Panic context:
step: calculate signatures
item: yo
thread 'main' panicked at '...', src/libcore/str/mod.rs:2162
note: Run with `RUST_BACKTRACE=1` for a backtrace.