Skip to content

Rust library to show manually-maintained context information on panic

Notifications You must be signed in to change notification settings

kriomant/panic-context

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Rust library to show manually-maintained context information on panic

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages