4 releases

0.2.0 Jun 7, 2020
0.1.2 Jun 7, 2020
0.1.1 Jun 7, 2020
0.1.0 Jun 7, 2020

#823 in Debugging

Download history 25/week @ 2024-07-26 3/week @ 2024-08-02 7/week @ 2024-09-20 8/week @ 2024-09-27 1628/week @ 2024-10-04 3928/week @ 2024-10-11 2452/week @ 2024-10-18 2717/week @ 2024-10-25 3974/week @ 2024-11-01

13,490 downloads per month
Used in 31 crates (2 directly)

MIT/Apache

10KB
130 lines

econtext: fast and simple error context on panics.

Calling an econtext! macro adds a scope to a thread-local linked list. If there is a panic!() while the scope is active, the data/message provided to the econtext! macro will be printed.

This thus provides an opt-in stack trace with optional data (e.g. the values of function arguments).

This can be very useful, for instance:

  • To print what data was being worked on when an error occurred
  • To provide something similar to a stack trace where a real stack trace is not available (e.g. in some WASM contexts)
  • To print a shorter and more readable stack trace for when the real stack trace is too long and winding.

The overhead of calling an excontext macro is around 15ns on a 2020 MacBook Pro.

Example

use econtext::*;

fn main() {
	econtext::add_panic_hook(); // Ensures econtext is printed on panic
	econtext!("While running"); // Print a message if there is a panic
	run();
}

fn run() {
	econtext_function!(); // Print function name (`run`) if there is a panic
	process("filename.txt");
}

fn process(filename: &str) {
	econtext_function_data!(filename.to_owned()); // Print function name and filename if there is a panic
	for i in 0..10 {
		econtext_data!("i", i); // Print loop index if there is a panic
		assert!(i != 4, "Intentional panic");
	}
}

On error, something like this is printed:

ERROR CONTEXT:
  my_module src/main.rs:17: i 4
  my_module src/main.rs:15: main::process "filename.txt"
  my_module src/main.rs:10: main::run
  my_module src/main.rs:5: While running

No runtime deps