提交 5bc5f021 authored 作者: Serhij S's avatar Serhij S

panic handler

上级 a74d0b4f
#![ doc = include_str!( concat!( env!( "CARGO_MANIFEST_DIR" ), "/", "README.md" ) ) ] #![ doc = include_str!( concat!( env!( "CARGO_MANIFEST_DIR" ), "/", "README.md" ) ) ]
use core::{fmt, num}; use core::{fmt, num};
use std::io::Write; use std::io::Write;
use std::panic::PanicInfo;
use std::{env, mem, str::FromStr, sync::Arc, time::Duration}; use std::{env, mem, str::FromStr, sync::Arc, time::Duration};
use colored::Colorize as _; use colored::Colorize as _;
...@@ -250,6 +251,22 @@ pub fn suicide(delay: Duration, warn: bool) { ...@@ -250,6 +251,22 @@ pub fn suicide(delay: Duration, warn: bool) {
}; };
} }
/// Sets panic handler to immediately kill the process and its childs with SIGKILL
pub fn setup_panic() {
std::panic::set_hook(Box::new(move |info: &PanicInfo| {
panic(info);
}));
}
fn panic(info: &PanicInfo) -> ! {
eprintln!("{}", info.to_string().red().bold());
thread_rt::suicide_myself(Duration::from_secs(0), false);
// never happens
loop {
std::thread::sleep(Duration::from_secs(1));
}
}
impl DataDeliveryPolicy for () {} impl DataDeliveryPolicy for () {}
impl DataDeliveryPolicy for usize {} impl DataDeliveryPolicy for usize {}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论