From 934d6c3987d4159d5733b777f0cbe8d8c3fe10f2 Mon Sep 17 00:00:00 2001 From: fufesou <13586388+fufesou@users.noreply.github.com> Date: Mon, 10 Nov 2025 15:43:46 +0800 Subject: [PATCH] refact: rust backtrace logs (#13467) Signed-off-by: fufesou --- src/common.rs | 44 -------------------------------------------- src/core_main.rs | 3 +++ src/flutter_ffi.rs | 4 ++++ src/main.rs | 3 --- 4 files changed, 7 insertions(+), 47 deletions(-) diff --git a/src/common.rs b/src/common.rs index 5d6d2ba7d..4ac3b6cd9 100644 --- a/src/common.rs +++ b/src/common.rs @@ -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::() { - s.as_str() - } else { - "Box" - }; - 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 } diff --git a/src/core_main.rs b/src/core_main.rs index 7347c1895..ecef5a45a 100644 --- a/src/core_main.rs +++ b/src/core_main.rs @@ -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> { + if !crate::common::global_init() { + return None; + } crate::load_custom_client(); #[cfg(windows)] if !crate::platform::windows::bootstrap() { diff --git a/src/flutter_ffi.rs b/src/flutter_ffi.rs index 15ffd52b8..ce9954b14 100644 --- a/src/flutter_ffi.rs +++ b/src/flutter_ffi.rs @@ -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 diff --git a/src/main.rs b/src/main.rs index 274d7735c..9bc90a8fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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);