Created
September 29, 2016 20:33
-
-
Save DoumanAsh/0a5ebc13e4a2413b8dced05874705056 to your computer and use it in GitHub Desktop.
Custom hook example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro_rules! format_payload { | |
($payload:expr, $($p_type:ty),+) => {{ | |
$( | |
if let Some(result) = $payload.downcast_ref::<$p_type>() { | |
format!("{}", result) | |
} | |
)else+ | |
else { | |
format!("{:?}", $payload) | |
} | |
}}; | |
} | |
fn main() { | |
std::panic::set_hook(Box::new(|info| { | |
let payload = info.payload(); | |
let msg = format!("{}{}", | |
info.location() | |
.map(|loc| format!("{}:{} - ", loc.file(), loc.line())) | |
.unwrap_or("".to_string()), | |
format_payload!(payload, String, &'static str)); | |
println!("Fatal error: {}", msg); | |
})); | |
panic!("lolka"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment