panic-context

Crates.iopanic-context
lib.rspanic-context
version0.1.0
sourcesrc
created_at2017-07-07 13:23:25.164694
updated_at2017-07-07 13:23:25.164694
descriptionShow manually-maintained context information on panic
homepagehttps://crates.io/crates/panic-context
repositoryhttps://github.com/kriomant/panic-context.git
max_upload_size
id22358
size11,296
Mikhail Trishchenkov (kriomant)

documentation

README

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.
Commit count: 1

cargo fmt