Skip to content

Instantly share code, notes, and snippets.

@DoumanAsh
Created September 29, 2016 20:33
Show Gist options
  • Save DoumanAsh/0a5ebc13e4a2413b8dced05874705056 to your computer and use it in GitHub Desktop.
Save DoumanAsh/0a5ebc13e4a2413b8dced05874705056 to your computer and use it in GitHub Desktop.
Custom hook example
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