Crates.io | panic-context |
lib.rs | panic-context |
version | 0.1.0 |
source | src |
created_at | 2017-07-07 13:23:25.164694 |
updated_at | 2017-07-07 13:23:25.164694 |
description | Show manually-maintained context information on panic |
homepage | https://crates.io/crates/panic-context |
repository | https://github.com/kriomant/panic-context.git |
max_upload_size | |
id | 22358 |
size | 11,296 |
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.
#[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.