refact: rust backtrace logs (#13467)

Signed-off-by: fufesou <linlong1266@gmail.com>
This commit is contained in:
fufesou
2025-11-10 15:43:46 +08:00
committed by GitHub
parent 2d7c6ef21f
commit 934d6c3987
4 changed files with 7 additions and 47 deletions

View File

@@ -115,50 +115,6 @@ pub fn global_init() -> bool {
crate::server::wayland::init();
}
}
// Install panic hook to log backtrace on all platforms
std::env::set_var("RUST_BACKTRACE", "1");
std::panic::set_hook(Box::new(|info| {
let thread = std::thread::current().name().unwrap_or("unnamed");
let location = info
.location()
.map(|l| format!("{}:{}:{}", l.file(), l.line(), l.column()))
.unwrap_or_else(|| "unknown".into());
let msg = if let Some(s) = info.payload().downcast_ref::<&str>() {
*s
} else if let Some(s) = info.payload().downcast_ref::<String>() {
s.as_str()
} else {
"Box<Any>"
};
let bt = std::backtrace::Backtrace::force_capture();
let out = format!("thread '{}' panicked at '{}', {}\nBacktrace:\n{bt:?}", thread, msg, location);
eprintln!("{}", out);
log::error!("{}", out);
}));
// Native crash handlers
#[cfg(unix)]
unsafe {
extern "C" fn crash_signal_handler(sig: libc::c_int) {
let bt = std::backtrace::Backtrace::force_capture();
eprintln!("native crash signal {}\nBacktrace:\n{bt:?}", sig);
log::error!("native crash signal {}\nBacktrace:\n{bt:?}", sig);
}
for &s in &[libc::SIGSEGV, libc::SIGABRT, libc::SIGILL, libc::SIGFPE, libc::SIGBUS] {
libc::signal(s, crash_signal_handler as usize);
}
}
#[cfg(windows)]
unsafe {
use winapi::um::{errhandlingapi::SetUnhandledExceptionFilter, minwinbase::EXCEPTION_POINTERS};
extern "system" fn unhandled_exception_filter(_: *mut EXCEPTION_POINTERS) -> i32 {
let bt = std::backtrace::Backtrace::force_capture();
eprintln!("native unhandled exception\nBacktrace:\n{bt:?}");
log::error!("native unhandled exception\nBacktrace:\n{bt:?}");
// EXCEPTION_EXECUTE_HANDLER = 1
1
}
SetUnhandledExceptionFilter(Some(unhandled_exception_filter));
}
true
}

View File

@@ -29,6 +29,9 @@ macro_rules! my_println{
/// If it returns [`Some`], then the process will continue, and flutter gui will be started.
#[cfg(not(any(target_os = "android", target_os = "ios")))]
pub fn core_main() -> Option<Vec<String>> {
if !crate::common::global_init() {
return None;
}
crate::load_custom_client();
#[cfg(windows)]
if !crate::platform::windows::bootstrap() {

View File

@@ -70,6 +70,10 @@ fn initialize(app_dir: &str, custom_client_config: &str) {
init_from_env(Env::default().filter_or(DEFAULT_FILTER_ENV, "debug"));
crate::common::test_nat_type();
}
#[cfg(any(target_os = "android", target_os = "ios"))]
{
let _ = crate::common::global_init();
}
#[cfg(not(any(target_os = "android", target_os = "ios")))]
{
// core_main's init_log does not work for flutter since it is only applied to its load_library in main.c

View File

@@ -23,9 +23,6 @@ fn main() {
feature = "flutter"
)))]
fn main() {
if !common::global_init() {
return;
}
#[cfg(all(windows, not(feature = "inline")))]
unsafe {
winapi::um::shellscalingapi::SetProcessDpiAwareness(2);