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 *;
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