BlueGit

ict rusty-teams

chore(app): write panics with location and backtrace to crash.log

A panic on the UI thread aborts on Windows (0xc0000409) and left no trace.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Daniel Däschle · pushed by metis

Authored 2026-10-08 · 7 weeks ago

Hash
e756946f58e970b608fc6125ea1141b1f2f940c4
Parent
b41a05ad
2 files changed +43 −0
crates/app/src/crash_log.rs added +41 −0
@@ -0,0 +1,41 @@
1 use std::backtrace::Backtrace;
2 use std::fs::OpenOptions;
3 use std::io::Write;
4 use std::panic::PanicHookInfo;
5 use std::path::PathBuf;
6
7 pub const CRASH_LOG_FILE: &str = "crash.log";
8
9 pub fn install(path: PathBuf) {
10 let previous = std::panic::take_hook();
11 std::panic::set_hook(Box::new(move |info| {
12 if let Ok(mut file) = OpenOptions::new().create(true).append(true).open(&path) {
13 let _ = file.write_all(entry(info, &Backtrace::force_capture().to_string()).as_bytes());
14 }
15 previous(info);
16 }));
17 }
18
19 fn entry(info: &PanicHookInfo<'_>, backtrace: &str) -> String {
20 let location = info
21 .location()
22 .map(|location| format!("{}:{}:{}", location.file(), location.line(), location.column()))
23 .unwrap_or_default();
24 let thread = std::thread::current();
25 format!(
26 "=== {} build {} thread {}\npanic at {location}: {}\n{backtrace}\n",
27 chrono::Utc::now().to_rfc3339(),
28 env!("TEAMS_BUILD_VERSION"),
29 thread.name().unwrap_or("unnamed"),
30 message(info),
31 )
32 }
33
34 fn message(info: &PanicHookInfo<'_>) -> String {
35 let payload = info.payload();
36 payload
37 .downcast_ref::<&str>()
38 .map(|text| (*text).to_owned())
39 .or_else(|| payload.downcast_ref::<String>().cloned())
40 .unwrap_or_else(|| "non-text panic payload".to_owned())
41 }
@@ -0,0 +1,41 @@
1use std::backtrace::Backtrace;
2use std::fs::OpenOptions;
3use std::io::Write;
4use std::panic::PanicHookInfo;
5use std::path::PathBuf;
6
7pub const CRASH_LOG_FILE: &str = "crash.log";
8
9pub fn install(path: PathBuf) {
10 let previous = std::panic::take_hook();
11 std::panic::set_hook(Box::new(move |info| {
12 if let Ok(mut file) = OpenOptions::new().create(true).append(true).open(&path) {
13 let _ = file.write_all(entry(info, &Backtrace::force_capture().to_string()).as_bytes());
14 }
15 previous(info);
16 }));
17}
18
19fn entry(info: &PanicHookInfo<'_>, backtrace: &str) -> String {
20 let location = info
21 .location()
22 .map(|location| format!("{}:{}:{}", location.file(), location.line(), location.column()))
23 .unwrap_or_default();
24 let thread = std::thread::current();
25 format!(
26 "=== {} build {} thread {}\npanic at {location}: {}\n{backtrace}\n",
27 chrono::Utc::now().to_rfc3339(),
28 env!("TEAMS_BUILD_VERSION"),
29 thread.name().unwrap_or("unnamed"),
30 message(info),
31 )
32}
33
34fn message(info: &PanicHookInfo<'_>) -> String {
35 let payload = info.payload();
36 payload
37 .downcast_ref::<&str>()
38 .map(|text| (*text).to_owned())
39 .or_else(|| payload.downcast_ref::<String>().cloned())
40 .unwrap_or_else(|| "non-text panic payload".to_owned())
41}
crates/app/src/main.rs +2 −0
@@ -10,6 +10,7 @@ mod avatar_image;
10 10 mod backend;
11 11 mod card_actions;
12 12 mod chat_actions;
13 mod crash_log;
13 14 mod card_state;
14 15 mod data;
15 16 mod demo;
@@ -135,6 +136,7 @@ fn transport(endpoint: Option<&str>, database: Option<&std::path::Path>) -> Arc<
135 136 }
136 137
137 138 fn main() {
139 crash_log::install(data_path(crash_log::CRASH_LOG_FILE));
138 140 let arguments = parse_arguments();
139 141 #[cfg(windows)]
140 142 updater::clean_up_old_binary();
@@ -10,6 +10,7 @@ mod avatar_image;
10mod backend;
10mod backend;
11mod card_actions;
11mod card_actions;
12mod chat_actions;
12mod chat_actions;
13mod crash_log;
13mod card_state;
14mod card_state;
14mod data;
15mod data;
15mod demo;
16mod demo;
@@ -135,6 +136,7 @@ fn transport(endpoint: Option<&str>, database: Option<&std::path::Path>) -> Arc<
135}
136}
136
137
137fn main() {
138fn main() {
139 crash_log::install(data_path(crash_log::CRASH_LOG_FILE));
138 let arguments = parse_arguments();
140 let arguments = parse_arguments();
139 #[cfg(windows)]
141 #[cfg(windows)]
140 updater::clean_up_old_binary();
142 updater::clean_up_old_binary();