ic_kit/
setup.rs

1use crate::ic;
2use std::panic;
3
4static mut DONE: bool = false;
5
6pub fn setup() {
7    unsafe {
8        if DONE {
9            return;
10        }
11        DONE = true;
12    }
13
14    set_panic_hook()
15}
16
17/// Sets a custom panic hook, uses debug.trace
18pub fn set_panic_hook() {
19    panic::set_hook(Box::new(|info| {
20        let file = info.location().unwrap().file();
21        let line = info.location().unwrap().line();
22        let col = info.location().unwrap().column();
23
24        let msg = match info.payload().downcast_ref::<&'static str>() {
25            Some(s) => *s,
26            None => match info.payload().downcast_ref::<String>() {
27                Some(s) => &s[..],
28                None => "Box<Any>",
29            },
30        };
31
32        let err_info = format!("Panicked at '{}', {}:{}:{}", msg, file, line, col);
33        ic::print(&err_info);
34        ic::trap(&err_info);
35    }));
36}