BlueGit

ict rusty-teams

Files

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

diagnostics.rs

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