steam_user/utils/
debug.rs1use std::{
2 env, fs,
3 path::Path,
4 time::{SystemTime, UNIX_EPOCH},
5};
6
7pub fn dump_html(context: &str, html: &str) {
18 if env::var("STEAM_USER_DEBUG_HTML").is_err() {
19 return;
20 }
21
22 let timestamp = SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();
23
24 let filename = format!("debug_html_{}_{}.html", context, timestamp);
25 let path = Path::new(&filename);
26
27 let dump_dir = Path::new("debug_dumps");
33 let final_path = match fs::create_dir_all(dump_dir) {
34 Ok(_) => dump_dir.join(&filename),
35 Err(_) => path.to_path_buf(),
36 };
37
38 if let Err(e) = fs::write(&final_path, html) {
39 tracing::error!("Failed to dump debug HTML to {:?}: {}", final_path, e);
40 } else {
41 tracing::debug!("Debug HTML dumped to {:?}", final_path);
42 }
43}
44
45#[cfg(test)]
46mod tests {
47 use std::fs;
48
49 use super::*;
50
51 #[test]
52 #[serial_test::serial]
53 fn test_dump_html_creates_file() {
54 unsafe {
58 env::set_var("STEAM_USER_DEBUG_HTML", "1");
59 }
60
61 let context = "test_context";
62 let html = "<html><body>Test</body></html>";
63
64 dump_html(context, html);
65
66 let dump_dir = Path::new("debug_dumps");
68 let exists = if dump_dir.exists() {
69 fs::read_dir(dump_dir).unwrap().any(|entry| {
70 let entry = entry.unwrap();
71 let name = entry.file_name().into_string().unwrap();
72 name.starts_with("debug_html_test_context")
73 })
74 } else {
75 fs::read_dir(".").unwrap().any(|entry| {
76 let entry = entry.unwrap();
77 let name = entry.file_name().into_string().unwrap();
78 name.starts_with("debug_html_test_context")
79 })
80 };
81
82 assert!(exists, "Debug HTML file was not created");
83
84 unsafe {
86 env::remove_var("STEAM_USER_DEBUG_HTML");
87 }
88
89 if dump_dir.exists() {
90 for entry in fs::read_dir(dump_dir).unwrap() {
91 let entry = entry.unwrap();
92 let path = entry.path();
93 let name = entry.file_name().into_string().unwrap();
94 if name.starts_with("debug_html_test_context") {
95 let _ = fs::remove_file(path);
96 }
97 }
98 }
99 }
100}