Macro custom_print::define_init_panic_hook [−][src]
macro_rules! define_init_panic_hook { ( $(#[$extern_meta:meta])* $vis:vis fn $name:ident(), $($args:tt)* ) => { ... }; ( $($args:tt)* ) => { ... }; }
Expand description
Defines init_panic_hook
function that set panic hook with the specified writer.
The first argument specify function name in the format fn FUNC_NAME()
and can be omitted to use the default name init_panic_hook
.
use once_cell::sync::Lazy; use std::sync::Mutex; static MESSAGE: Lazy<Mutex<String>> = Lazy::new(Mutex::default); fn main() { use std::panic::{catch_unwind, take_hook}; fn write(value: &str) { let mut chunks = MESSAGE.lock().unwrap(); *chunks += value; } custom_print::define_init_panic_hook!(concat, write); init_panic_hook(); let result = catch_unwind(|| assert_eq!("foo", "bar")); let _ = take_hook(); assert!(result.is_err()); let message = MESSAGE.lock().unwrap(); assert!(message.contains("panicked")); assert!(message.contains("assertion failed")); assert!(message.contains("\"foo\"")); assert!(message.contains("\"bar\"")); }