BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / diagnostics.rs

diagnostics.rs

diagnostics.rs 1.1 KiB Download Raw
1use std::fs::OpenOptions;
2use std::io::Write;
3use std::path::PathBuf;
4use std::sync::OnceLock;
5
6pub const DIAGNOSTICS_FILE: &str = "diagnostics.log";
7const MAX_BYTES: u64 = 1024 * 1024;
8
9static PATH: OnceLock<PathBuf> = OnceLock::new();
10
11pub fn install(path: PathBuf) {
12 let _ = PATH.set(path);
13}
14
15pub fn directory() -> Option<PathBuf> {
16 PATH.get().and_then(|path| path.parent()).map(PathBuf::from)
17}
18
19pub fn record(message: impl AsRef<str>) {
20 let message = message.as_ref();
21 eprintln!("{message}");
22 let Some(path) = PATH.get() else {
23 return;
24 };
25 let full = std::fs::metadata(path).is_ok_and(|metadata| metadata.len() > MAX_BYTES);
26 let file = OpenOptions::new()
27 .create(true)
28 .write(true)
29 .append(!full)
30 .truncate(full)
31 .open(path);
32 if let Ok(mut file) = file {
33 let _ = writeln!(file, "{} {message}", line_prefix());
34 }
35}
36
37fn line_prefix() -> String {
38 format!(
39 "{} {}",
40 chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ"),
41 env!("TEAMS_BUILD_VERSION")
42 )
43}