add couple of debug utils

This commit is contained in:
Aleksey Kladov 2019-06-04 00:25:43 +03:00
parent 6aa8d8b99d
commit 5264711b5d
3 changed files with 36 additions and 4 deletions

View file

@ -225,6 +225,36 @@ fn print(lvl: usize, msgs: &[Message], out: &mut impl Write, longer_than: Durati
}
}
/// Prints backtrace to stderr, useful for debugging.
pub fn print_backtrace() {
let bt = backtrace::Backtrace::new();
eprintln!("{:?}", bt);
}
thread_local!(static IN_SCOPE: RefCell<bool> = RefCell::new(false));
/// Allows to check if the current code is withing some dynamic scope, can be
/// useful during debugging to figure out why a function is called.
pub struct Scope {
_hidden: (),
}
impl Scope {
pub fn enter() -> Scope {
IN_SCOPE.with(|slot| *slot.borrow_mut() = true);
Scope { _hidden: () }
}
pub fn is_active() -> bool {
IN_SCOPE.with(|slot| *slot.borrow())
}
}
impl Drop for Scope {
fn drop(&mut self) {
IN_SCOPE.with(|slot| *slot.borrow_mut() = false);
}
}
#[cfg(test)]
mod tests {
use super::*;