Skip to content

Commit 608d694

Browse files
erdemgokselerdemgoksel
authored andcommitted
Improve crash logging in Windows service
Refactors the Windows service entrypoint to add a dedicated crash log writer and ensure key lifecycle events are logged to a persistent file. Removes panic catching in favor of direct logging, and enhances error reporting for better diagnostics.
1 parent 8cdeef9 commit 608d694

File tree

1 file changed

+36
-49
lines changed

1 file changed

+36
-49
lines changed

src/bin/windows_service.rs

Lines changed: 36 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,40 +20,42 @@ use beeper_auotmations::logging::log_to_file;
2020

2121
define_windows_service!(ffi_service_main, service_main);
2222

23+
fn write_crash_log(msg: &str) {
24+
let log_path = std::env::var("PROGRAMDATA")
25+
.unwrap_or_else(|_| "C:\\ProgramData".to_string())
26+
+ "\\BeeperAutomations\\service_crash.log";
27+
28+
let timestamp = chrono::Local::now().format("%Y-%m-%d %H:%M:%S");
29+
30+
if let Ok(mut f) = std::fs::OpenOptions::new()
31+
.create(true)
32+
.write(true)
33+
.append(true)
34+
.open(&log_path)
35+
{
36+
use std::io::Write;
37+
let _ = writeln!(f, "[{}] {}", timestamp, msg);
38+
}
39+
}
40+
2341
fn service_main(_arguments: Vec<OsString>) {
42+
write_crash_log("service_main() called");
43+
2444
// Initialize tracing for Windows service mode BEFORE any other logging
25-
match std::panic::catch_unwind(|| {
26-
beeper_auotmations::logging::init_logging(true);
27-
log_to_file("Windows service wrapper started");
28-
}) {
29-
Ok(_) => {},
30-
Err(e) => {
31-
// If we can't even initialize logging, try to write to a simple text file
32-
let error_msg = format!("PANIC during initialization: {:?}", e);
33-
let log_path = std::env::var("PROGRAMDATA").unwrap_or_else(|_| "C:\\ProgramData".to_string())
34-
+ "\\BeeperAutomations\\service_crash.log";
35-
36-
if let Ok(mut f) = std::fs::OpenOptions::new()
37-
.create(true)
38-
.write(true)
39-
.append(true)
40-
.open(&log_path)
41-
{
42-
use std::io::Write;
43-
let _ = writeln!(f, "{}", error_msg);
44-
}
45-
return;
46-
}
47-
}
45+
beeper_auotmations::logging::init_logging(true);
46+
log_to_file("Windows service wrapper started");
4847

49-
log_to_file("About to call run_service()");
48+
write_crash_log("Logging initialized, about to call run_service()");
5049

5150
if let Err(e) = run_service() {
52-
log_to_file(&format!("Service error: {}", e));
51+
let error_msg = format!("Service error: {}", e);
52+
log_to_file(&error_msg);
5353
log_to_file(&format!("Error details: {:?}", e));
54+
write_crash_log(&error_msg);
5455
}
5556

5657
log_to_file("Windows service wrapper exiting");
58+
write_crash_log("Windows service wrapper exiting");
5759
}
5860

5961
fn run_service() -> windows_service::Result<()> {
@@ -142,34 +144,19 @@ fn run_service() -> windows_service::Result<()> {
142144
log_to_file("About to call beeper_auotmations::run_service_with_shutdown()");
143145

144146
// Run the service and wait for shutdown signal
145-
let result = std::panic::catch_unwind(move || {
146-
runtime.block_on(async {
147-
tokio::select! {
148-
result = beeper_auotmations::run_service_with_shutdown(shutdown_rx) => {
149-
if let Err(e) = result {
150-
log_to_file(&format!("Service error: {}", e));
151-
log_to_file(&format!("Error details: {:?}", e));
152-
}
153-
log_to_file("run_service_with_shutdown() RETURNED");
147+
let result = runtime.block_on(async {
148+
tokio::select! {
149+
result = beeper_auotmations::run_service_with_shutdown(shutdown_rx) => {
150+
if let Err(e) = result {
151+
log_to_file(&format!("Service error: {}", e));
152+
log_to_file(&format!("Error details: {:?}", e));
154153
}
154+
log_to_file("run_service_with_shutdown() RETURNED");
155155
}
156-
})
156+
}
157157
});
158158

159-
match result {
160-
Ok(_) => {
161-
log_to_file("Service block_on completed successfully");
162-
}
163-
Err(panic_info) => {
164-
let error_msg = format!("PANIC in service: {:?}", panic_info);
165-
log_to_file(&error_msg);
166-
if let Some(s) = panic_info.downcast_ref::<&str>() {
167-
log_to_file(&format!("Panic message: {}", s));
168-
} else if let Some(s) = panic_info.downcast_ref::<String>() {
169-
log_to_file(&format!("Panic message: {}", s));
170-
}
171-
}
172-
}
159+
log_to_file(&format!("Service block_on completed with result: {:?}", result));
173160

174161
log_to_file("Service loop exited, initiating shutdown");
175162

0 commit comments

Comments
 (0)