diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_prof/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/ra_prof/src/lib.rs | 30 |
2 files changed, 31 insertions, 0 deletions
diff --git a/crates/ra_prof/Cargo.toml b/crates/ra_prof/Cargo.toml index efcce3d65..d55af18f5 100644 --- a/crates/ra_prof/Cargo.toml +++ b/crates/ra_prof/Cargo.toml | |||
@@ -8,3 +8,4 @@ publish = false | |||
8 | [dependencies] | 8 | [dependencies] |
9 | once_cell = "0.2.0" | 9 | once_cell = "0.2.0" |
10 | itertools = "0.8.0" | 10 | itertools = "0.8.0" |
11 | backtrace = "0.3.28" | ||
diff --git a/crates/ra_prof/src/lib.rs b/crates/ra_prof/src/lib.rs index e681b8e0b..6ec24d86d 100644 --- a/crates/ra_prof/src/lib.rs +++ b/crates/ra_prof/src/lib.rs | |||
@@ -225,6 +225,36 @@ fn print(lvl: usize, msgs: &[Message], out: &mut impl Write, longer_than: Durati | |||
225 | } | 225 | } |
226 | } | 226 | } |
227 | 227 | ||
228 | /// Prints backtrace to stderr, useful for debugging. | ||
229 | pub fn print_backtrace() { | ||
230 | let bt = backtrace::Backtrace::new(); | ||
231 | eprintln!("{:?}", bt); | ||
232 | } | ||
233 | |||
234 | thread_local!(static IN_SCOPE: RefCell<bool> = RefCell::new(false)); | ||
235 | |||
236 | /// Allows to check if the current code is withing some dynamic scope, can be | ||
237 | /// useful during debugging to figure out why a function is called. | ||
238 | pub struct Scope { | ||
239 | _hidden: (), | ||
240 | } | ||
241 | |||
242 | impl Scope { | ||
243 | pub fn enter() -> Scope { | ||
244 | IN_SCOPE.with(|slot| *slot.borrow_mut() = true); | ||
245 | Scope { _hidden: () } | ||
246 | } | ||
247 | pub fn is_active() -> bool { | ||
248 | IN_SCOPE.with(|slot| *slot.borrow()) | ||
249 | } | ||
250 | } | ||
251 | |||
252 | impl Drop for Scope { | ||
253 | fn drop(&mut self) { | ||
254 | IN_SCOPE.with(|slot| *slot.borrow_mut() = false); | ||
255 | } | ||
256 | } | ||
257 | |||
228 | #[cfg(test)] | 258 | #[cfg(test)] |
229 | mod tests { | 259 | mod tests { |
230 | use super::*; | 260 | use super::*; |