1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use std::{cell::RefCell, panic, sync::Once};

#[macro_export]
macro_rules! panic_ctx {
    ($s:expr) => {{
        if cfg!(debug_assertions) {
            Some($crate::panic_context::new($s))
        } else {
            None
        }
    }};
}

#[cfg(not(debug_assertions))]
#[inline(always)]
pub fn new(_: String) -> () {}

#[cfg(debug_assertions)]
pub fn new(context: String) -> PanicContext {
    static ONCE: Once = Once::new();
    ONCE.call_once(PanicContext::init);

    with_ctx(|ctx| ctx.push(context));
    PanicContext { _priv: () }
}

#[must_use]
#[cfg(debug_assertions)]
pub struct PanicContext {
    _priv: (),
}

#[cfg(debug_assertions)]
impl PanicContext {
    fn init() {
        let default_hook = panic::take_hook();
        let hook = move |panic_info: &panic::PanicInfo<'_>| {
            with_ctx(|ctx| {
                if !ctx.is_empty() {
                    eprintln!("Panic context:");
                    for frame in ctx.iter() {
                        eprintln!("> {}\n", frame)
                    }
                }
                default_hook(panic_info)
            })
        };
        panic::set_hook(Box::new(hook))
    }
}

#[cfg(debug_assertions)]
impl Drop for PanicContext {
    fn drop(&mut self) {
        with_ctx(|ctx| assert!(ctx.pop().is_some()))
    }
}

#[cfg(debug_assertions)]
fn with_ctx(f: impl FnOnce(&mut Vec<String>)) {
    thread_local! {
        static CTX: RefCell<Vec<String>> = RefCell::new(Vec::new());
    }
    CTX.with(|ctx| f(&mut ctx.borrow_mut()))
}