diff options
author | Aleksey Kladov <[email protected]> | 2020-09-29 19:08:27 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-09-29 19:20:47 +0100 |
commit | d3a2b21a8c34c7b7eea0a001a1412992e3ed2cb7 (patch) | |
tree | 0f965a835c179637f6df293cef010134f283cfea /crates/stdx/src | |
parent | e315fd9bb0e0647ab8b0e118d264d2103e271586 (diff) |
Add panic_context module for better panic messages
Diffstat (limited to 'crates/stdx/src')
-rw-r--r-- | crates/stdx/src/lib.rs | 1 | ||||
-rw-r--r-- | crates/stdx/src/panic_context.rs | 49 |
2 files changed, 50 insertions, 0 deletions
diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index 273b0f55b..011935cad 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs | |||
@@ -5,6 +5,7 @@ use std::{ | |||
5 | }; | 5 | }; |
6 | 6 | ||
7 | mod macros; | 7 | mod macros; |
8 | pub mod panic_context; | ||
8 | 9 | ||
9 | #[inline(always)] | 10 | #[inline(always)] |
10 | pub fn is_ci() -> bool { | 11 | pub fn is_ci() -> bool { |
diff --git a/crates/stdx/src/panic_context.rs b/crates/stdx/src/panic_context.rs new file mode 100644 index 000000000..fd232e0cc --- /dev/null +++ b/crates/stdx/src/panic_context.rs | |||
@@ -0,0 +1,49 @@ | |||
1 | //! A micro-crate to enhance panic messages with context info. | ||
2 | //! | ||
3 | //! FIXME: upstream to https://github.com/kriomant/panic-context ? | ||
4 | |||
5 | use std::{cell::RefCell, panic, sync::Once}; | ||
6 | |||
7 | pub fn enter(context: String) -> impl Drop { | ||
8 | static ONCE: Once = Once::new(); | ||
9 | ONCE.call_once(PanicContext::init); | ||
10 | |||
11 | with_ctx(|ctx| ctx.push(context)); | ||
12 | PanicContext { _priv: () } | ||
13 | } | ||
14 | |||
15 | #[must_use] | ||
16 | struct PanicContext { | ||
17 | _priv: (), | ||
18 | } | ||
19 | |||
20 | impl PanicContext { | ||
21 | fn init() { | ||
22 | let default_hook = panic::take_hook(); | ||
23 | let hook = move |panic_info: &panic::PanicInfo<'_>| { | ||
24 | with_ctx(|ctx| { | ||
25 | if !ctx.is_empty() { | ||
26 | eprintln!("Panic context:"); | ||
27 | for frame in ctx.iter() { | ||
28 | eprintln!("> {}\n", frame) | ||
29 | } | ||
30 | } | ||
31 | default_hook(panic_info) | ||
32 | }) | ||
33 | }; | ||
34 | panic::set_hook(Box::new(hook)) | ||
35 | } | ||
36 | } | ||
37 | |||
38 | impl Drop for PanicContext { | ||
39 | fn drop(&mut self) { | ||
40 | with_ctx(|ctx| assert!(ctx.pop().is_some())) | ||
41 | } | ||
42 | } | ||
43 | |||
44 | fn with_ctx(f: impl FnOnce(&mut Vec<String>)) { | ||
45 | thread_local! { | ||
46 | static CTX: RefCell<Vec<String>> = RefCell::new(Vec::new()); | ||
47 | } | ||
48 | CTX.with(|ctx| f(&mut *ctx.borrow_mut())) | ||
49 | } | ||