ソースを参照

更完美的堆栈跟踪

skyfffire 11 ヶ月 前
コミット
9200fc66ff
2 ファイル変更15 行追加3 行削除
  1. 1 0
      Cargo.toml
  2. 14 3
      src/main.rs

+ 1 - 0
Cargo.toml

@@ -23,6 +23,7 @@ serde_json = "1.0.105"
 rust_decimal = { version = "1.32.0", features = ["maths"] }
 rust_decimal_macros = "1.32.0"
 actix-cors = "0.6"
+backtrace = "0.3" # 堆栈跟踪
 
 [workspace]
 members=[

+ 14 - 3
src/main.rs

@@ -7,6 +7,7 @@ use std::str::FromStr;
 use std::sync::Arc;
 use std::sync::atomic::{AtomicBool, Ordering};
 use std::time::Duration;
+use backtrace::Backtrace;
 use tokio::sync::Mutex;
 use tracing::{error, info, warn};
 use tracing_appender_timezone::non_blocking::WorkerGuard;
@@ -137,9 +138,19 @@ async fn main() {
     let account_name_clone = params.account_name.clone();
     let panic_running = running.clone();
     std::panic::set_hook(Box::new(move |panic_info| {
-        let msg = format!("account={}, type=panic, msg={:?}, location={:?}",
-                          account_name_clone, panic_info.to_string(), panic_info.location());
-        warn!("{}", msg);
+        let msg = format!(
+            "account={}, type=panic, msg={:?}, location={:?}",
+            account_name_clone,
+            panic_info.to_string(),
+            panic_info.location()
+        );
+
+        // 生成并格式化完整的堆栈跟踪
+        let backtrace = Backtrace::new();
+        let stack_trace = format!("{:?}", backtrace);
+
+        // 一并打印堆栈跟踪
+        warn!("{}\nStack Trace:\n{}", msg, stack_trace);
         panic_running.store(false, Ordering::Relaxed);
     }));