BlueGit

ict rusty-teams

Files

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

diagnostics.rs

diagnostics.rs 3.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
11const WEBRTC_AUDIO_MARKERS: [&str; 5] = [
12 "AdmProxy",
13 "audio_device",
14 "core_audio",
15 "WebRtcVoiceEngine",
16 "webrtc_voice_engine",
17];
18const WEBRTC_SKIPPED: [&str; 1] = ["audio_device_buffer"];
19const ENDPOINT_DUMP_LINES: std::ops::Range<u32> = 4040..4170;
20
21pub fn install(path: PathBuf) {
22 let _ = PATH.set(path);
23 if log::set_logger(&WebrtcAudioLog).is_ok() {
24 log::set_max_level(log::LevelFilter::Debug);
25 }
26}
27
28struct WebrtcAudioLog;
29
30impl log::Log for WebrtcAudioLog {
31 fn enabled(&self, metadata: &log::Metadata<'_>) -> bool {
32 metadata.target() == "libwebrtc"
33 }
34
35 fn log(&self, entry: &log::Record<'_>) {
36 if !self.enabled(entry.metadata()) {
37 return;
38 }
39 let text = entry.args().to_string();
40 if keeps_webrtc_line(&text) {
41 record(format!("webrtc: {text}"));
42 }
43 }
44
45 fn flush(&self) {}
46}
47
48fn keeps_webrtc_line(text: &str) -> bool {
49 WEBRTC_AUDIO_MARKERS
50 .iter()
51 .any(|marker| text.contains(marker))
52 && !WEBRTC_SKIPPED.iter().any(|skipped| text.contains(skipped))
53 && !is_endpoint_dump(text)
54}
55
56fn is_endpoint_dump(text: &str) -> bool {
57 text.split_once("audio_device_core_win.cc:")
58 .and_then(|(_, rest)| rest.split_once(')'))
59 .and_then(|(line, _)| line.parse::<u32>().ok())
60 .is_some_and(|line| ENDPOINT_DUMP_LINES.contains(&line))
61}
62
63pub fn directory() -> Option<PathBuf> {
64 PATH.get().and_then(|path| path.parent()).map(PathBuf::from)
65}
66
67pub fn record(message: impl AsRef<str>) {
68 let message = message.as_ref();
69 eprintln!("{message}");
70 let Some(path) = PATH.get() else {
71 return;
72 };
73 let full = std::fs::metadata(path).is_ok_and(|metadata| metadata.len() > MAX_BYTES);
74 let file = OpenOptions::new()
75 .create(true)
76 .write(true)
77 .append(!full)
78 .truncate(full)
79 .open(path);
80 if let Ok(mut file) = file {
81 let _ = writeln!(file, "{} {message}", line_prefix());
82 }
83}
84
85fn line_prefix() -> String {
86 format!(
87 "{} {}",
88 chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ"),
89 env!("TEAMS_BUILD_VERSION")
90 )
91}
92
93#[cfg(test)]
94mod tests {
95 use super::keeps_webrtc_line;
96
97 #[test]
98 fn audio_lines_are_kept_and_endpoint_dumps_and_noise_are_skipped() {
99 assert!(keeps_webrtc_line("(audio_device_impl.cc:123): Init"));
100 assert!(keeps_webrtc_line(
101 "(webrtc_voice_engine.cc:10): WebRtcVoiceEngine::Init"
102 ));
103 assert!(!keeps_webrtc_line(
104 "(audio_device_core_win.cc:4090): endpoint 3"
105 ));
106 assert!(keeps_webrtc_line(
107 "(audio_device_core_win.cc:412): AvRevertMmThreadCharacteristics() is OK"
108 ));
109 assert!(!keeps_webrtc_line(
110 "(audio_device_buffer.cc:450): [REC : 10000msec, 48kHz] callbacks: 1000"
111 ));
112 assert!(!keeps_webrtc_line("(peer_connection.cc:55): something"));
113 }
114}