mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 06:41:48 +00:00
add couple of debug utils
This commit is contained in:
parent
6aa8d8b99d
commit
5264711b5d
3 changed files with 36 additions and 4 deletions
|
@ -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::*;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue