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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
/// 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 /// #[cfg_attr(feature = "alloc", doc = "```rust")] #[cfg_attr(not(feature = "alloc"), doc = "```rust,compile_fail")] /// 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\"")); /// } /// ``` #[macro_export] macro_rules! define_panic_hook { ( $(#[$extern_meta:meta])* $vis:vis fn $name:ident(...), $($args:tt)* ) => { $(#[$extern_meta])* $vis fn $name(info: &::std::panic::PanicInfo<'_>) { ::core::writeln!($crate::define_writer!($($args)*), "{}", info) .expect("failed writing panic info"); } }; ( $($args:tt)* ) => { $crate::define_panic_hook!(fn panic_hook(...), $($args)*); }; } /// 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`. /// #[cfg_attr(feature = "alloc", doc = "```rust")] #[cfg_attr(not(feature = "alloc"), doc = "```rust,compile_fail")] /// 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\"")); /// } /// ``` #[macro_export] macro_rules! define_init_panic_hook { ( $(#[$extern_meta:meta])* $vis:vis fn $name:ident(), $($args:tt)* ) => { $(#[$extern_meta])* $vis fn $name() { ::std::panic::set_hook(::std::boxed::Box::new( |info: &::std::panic::PanicInfo<'_>| { ::core::writeln!($crate::define_writer!($($args)*), "{}", info) .expect("failed writing panic info"); } )) } }; ( $($args:tt)* ) => { $crate::define_init_panic_hook!(fn init_panic_hook(), $($args)*); }; } /// Sets `panic_hook` that uses the specified writer. /// /// # Examples /// #[cfg_attr(feature = "alloc", doc = "```rust")] #[cfg_attr(not(feature = "alloc"), doc = "```rust,compile_fail")] /// 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\"")); /// } /// ``` #[macro_export] macro_rules! init_panic_hook { ( $($args:tt)* ) => { ::std::panic::set_hook(::std::boxed::Box::new( |info: &::std::panic::PanicInfo<'_>| { ::core::writeln!($crate::define_writer!($($args)*), "{}", info) .expect("failed writing panic info"); } )) }; }