Macro custom_print::init_panic_hook
source · macro_rules! init_panic_hook { ( $($args:tt)* ) => { ... }; }
Expand description
Sets panic_hook
that uses the specified writer.
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::init_panic_hook!(concat, write);
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\""));
}