Macro custom_print::define_panic_hook [−][src]
macro_rules! define_panic_hook { ( $(#[$extern_meta:meta])* $vis:vis fn $name:ident(...), $($args:tt)* ) => { ... }; ( $($args:tt)* ) => { ... }; }
Expand description
Defines panic_hook function that can be used as panic hook that uses the specified writer.
The first argument specify function name in the format fn FUNC_NAME(...)
and can be omitted to use the default name panic_hook.
Examples
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_panic_hook!(concat, write); std::panic::set_hook(Box::new(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\"")); }