Hello. I don't know is it right place to post, or I should add comment to RFC-2504 or create new RFC instead?
I've been looking through std::backtrace::Backtrace and noticed that it is configured using env variable and content of the variable is cached for obvious reasons (performance).
impl Backtrace {
/// Returns whether backtrace captures are enabled through environment
/// variables.
fn enabled() -> bool {
// Cache the result of reading the environment variables to make
// backtrace captures speedy, because otherwise reading environment
// variables every time can be somewhat slow.
static ENABLED: AtomicUsize = AtomicUsize::new(0);
match ENABLED.load(SeqCst) {
0 => {}
1 => return false,
_ => return true,
}
let enabled = match env::var("RUST_LIB_BACKTRACE") {
Ok(s) => s != "0",
Err(_) => match env::var("RUST_BACKTRACE") {
Ok(s) => s != "0",
Err(_) => false,
},
};
ENABLED.store(enabled as usize + 1, SeqCst);
enabled
}
But I have one use case and request for change. I would like to be able to switch backtrace in runtime.
When it could be needed? Production use on the server, when sys-admin detected incorrect behavior or developer want to detect why Error was returned. So it would be really useful to change backtrace behaviour of error at run-time.
- notice error in logs
- call private API (or debug button if UI app) and receive backtraces till disabled.
So the application user won't have the overhead of backtrace until it's required.
What I propose
Add setter method: Backtrace::set_enabled()
and make ENABLED: AtomicUsize
non static inside function but just private.
The default behavior is still to check env-variable, but it could be overridden by the programmer.
P.S. do I understand correctly that RUST_BACKTRACE would affect backtraces of panics and it's possible to set RUST_BACKTRACE=1 RUST_LIB=BACKTRACE=0. meaning that panics would print backtrace and Errors won't have backtrace?