BlueGit

ict rusty-teams

Files

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

diagnostics.rs

chore: cargo fmt the workspace
metis Daniel Däschle · pushed by metis · 2 weeks ago
a6592765 History
diagnostics.rs 3.7 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; 3] = ["audio_device_buffer", "requesting data", "will write"];
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!(
71 "slow ui handler: {label} took {} ms",
72 elapsed.as_millis()
73 ));
74 }
75}
76
77pub fn record(message: impl AsRef<str>) {
78 let message = message.as_ref();
79 eprintln!("{message}");
80 let Some(path) = PATH.get() else {
81 return;
82 };
83 let full = std::fs::metadata(path).is_ok_and(|metadata| metadata.len() > MAX_BYTES);
84 let file = OpenOptions::new()
85 .create(true)
86 .write(true)
87 .append(!full)
88 .truncate(full)
89 .open(path);
90 if let Ok(mut file) = file {
91 let _ = writeln!(file, "{} {message}", line_prefix());
92 }
93}
94
95fn line_prefix() -> String {
96 format!(
97 "{} {}",
98 chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ"),
99 crate::version::build_version()
100 )
101}
102
103#[cfg(test)]
104mod tests {
105 use super::keeps_webrtc_line;
106
107 #[test]
108 fn audio_lines_are_kept_and_endpoint_dumps_and_noise_are_skipped() {
109 assert!(keeps_webrtc_line("(audio_device_impl.cc:123): Init"));
110 assert!(keeps_webrtc_line(
111 "(webrtc_voice_engine.cc:10): WebRtcVoiceEngine::Init"
112 ));
113 assert!(!keeps_webrtc_line(
114 "(audio_device_core_win.cc:4090): endpoint 3"
115 ));
116 assert!(keeps_webrtc_line(
117 "(audio_device_core_win.cc:412): AvRevertMmThreadCharacteristics() is OK"
118 ));
119 assert!(!keeps_webrtc_line(
120 "(audio_device_buffer.cc:450): [REC : 10000msec, 48kHz] callbacks: 1000"
121 ));
122 assert!(!keeps_webrtc_line(
123 "(audio_device_pulse_linux.cc:2236): requesting data"
124 ));
125 assert!(!keeps_webrtc_line(
126 "(audio_device_pulse_linux.cc:2256): will write"
127 ));
128 assert!(!keeps_webrtc_line("(peer_connection.cc:55): something"));
129 }
130}