1use regex::Regex;
2
3use super::severity::detect_severity_from_text;
4use crate::models::log_entry::{LogEntry, LogFormat, Severity};
5use std::sync::OnceLock;
6
7fn dism_prefix_re() -> &'static Regex {
8 static CELL: OnceLock<Regex> = OnceLock::new();
9 CELL.get_or_init(|| Regex::new(r"^\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2},").unwrap())
10}
11
12fn dism_header_re() -> &'static Regex {
13 static CELL: OnceLock<Regex> = OnceLock::new();
14 CELL.get_or_init(|| {
15 Regex::new(
16 r"^(\d{4})-(\d{2})-(\d{2})\s+(\d{2}):(\d{2}):(\d{2}),\s+(Info|Warning|Error|Perf|Verbose|Debug)\s+([A-Za-z][A-Za-z0-9_.-]{1,31})\s+(.*)$",
17 )
18 .unwrap()
19})
20}
21
22fn dism_relaxed_header_re() -> &'static Regex {
23 static CELL: OnceLock<Regex> = OnceLock::new();
24 CELL.get_or_init(|| {
25 Regex::new(
26 r"^(\d{4})-(\d{2})-(\d{2})\s+(\d{2}):(\d{2}):(\d{2}),\s+([A-Za-z][A-Za-z0-9_-]{1,31})\s+([A-Za-z][A-Za-z0-9_.-]{1,31})\s+(.*)$",
27 )
28 .unwrap()
29})
30}
31
32struct PendingEntry {
33 entry: LogEntry,
34 start_line: u32,
35}
36
37pub fn matches_dism_record(line: &str) -> bool {
38 let Some(caps) = dism_header_re().captures(line) else {
39 return false;
40 };
41
42 caps.get(8)
43 .map(|m| m.as_str().to_ascii_uppercase().starts_with("DISM"))
44 .unwrap_or(false)
45}
46
47pub fn parse_lines(lines: &[&str], file_path: &str) -> (Vec<LogEntry>, u32) {
48 let mut entries = Vec::new();
49 let mut parse_errors = 0;
50 let mut next_id = 0;
51 let mut pending: Option<PendingEntry> = None;
52
53 for (index, line) in lines.iter().enumerate() {
54 let line_number = (index + 1) as u32;
55
56 if let Some(entry) = parse_header(line, file_path) {
57 flush_pending(&mut entries, &mut pending, &mut next_id);
58 pending = Some(PendingEntry {
59 entry,
60 start_line: line_number,
61 });
62 continue;
63 }
64
65 if dism_prefix_re().is_match(line) {
66 flush_pending(&mut entries, &mut pending, &mut next_id);
67 entries.push(fallback_entry(next_id, line_number, line, file_path));
68 next_id += 1;
69 parse_errors += 1;
70 continue;
71 }
72
73 let trimmed_end = line.trim_end();
74 if trimmed_end.is_empty() {
75 continue;
76 }
77
78 if let Some(pending_entry) = pending.as_mut() {
79 if !pending_entry.entry.message.is_empty() {
80 pending_entry.entry.message.push('\n');
81 }
82 pending_entry.entry.message.push_str(trimmed_end);
83 } else {
84 entries.push(fallback_entry(next_id, line_number, trimmed_end, file_path));
85 next_id += 1;
86 parse_errors += 1;
87 }
88 }
89
90 flush_pending(&mut entries, &mut pending, &mut next_id);
91
92 (entries, parse_errors)
93}
94
95fn parse_header(line: &str, file_path: &str) -> Option<LogEntry> {
96 if let Some(caps) = dism_header_re().captures(line) {
100 return build_entry_from_caps(&caps, file_path);
101 }
102
103 let caps = dism_relaxed_header_re().captures(line)?;
106 build_entry_from_caps(&caps, file_path)
107}
108
109fn build_entry_from_caps(caps: ®ex::Captures<'_>, file_path: &str) -> Option<LogEntry> {
110 let component = caps.get(8)?.as_str().to_string();
111
112 let year: i32 = caps.get(1)?.as_str().parse().ok()?;
113 let month: u32 = caps.get(2)?.as_str().parse().ok()?;
114 let day: u32 = caps.get(3)?.as_str().parse().ok()?;
115 let hour: u32 = caps.get(4)?.as_str().parse().ok()?;
116 let minute: u32 = caps.get(5)?.as_str().parse().ok()?;
117 let second: u32 = caps.get(6)?.as_str().parse().ok()?;
118 let level = caps.get(7)?.as_str();
119 let message = caps
120 .get(9)
121 .map(|m| m.as_str())
122 .unwrap_or("")
123 .trim_end()
124 .to_string();
125
126 let timestamp = chrono::NaiveDate::from_ymd_opt(year, month, day)
127 .and_then(|date| date.and_hms_opt(hour, minute, second))
128 .map(|dt| dt.and_utc().timestamp_millis());
129
130 Some(LogEntry {
131 id: 0,
132 line_number: 0,
133 message,
134 component: Some(component),
135 timestamp,
136 timestamp_display: Some(format!(
137 "{:04}-{:02}-{:02} {:02}:{:02}:{:02}.000",
138 year, month, day, hour, minute, second
139 )),
140 severity: severity_from_level(level, caps.get(9).map(|m| m.as_str()).unwrap_or("")),
141 thread: None,
142 thread_display: None,
143 source_file: None,
144 format: LogFormat::Timestamped,
145 file_path: file_path.to_string(),
146 timezone_offset: None,
147 error_code_spans: Vec::new(),
148 ip_address: None,
149 host_name: None,
150 mac_address: None,
151 result_code: None,
152 gle_code: None,
153 setup_phase: None,
154 operation_name: None,
155 http_method: None,
156 uri_stem: None,
157 uri_query: None,
158 status_code: None,
159 sub_status: None,
160 time_taken_ms: None,
161 client_ip: None,
162 server_ip: None,
163 user_agent: None,
164 server_port: None,
165 username: None,
166 win32_status: None,
167 query_name: None,
168 query_type: None,
169 response_code: None,
170 dns_direction: None,
171 dns_protocol: None,
172 source_ip: None,
173 dns_flags: None,
174 dns_event_id: None,
175 zone_name: None,
176 entry_kind: None,
177 whatif: None,
178 section_name: None,
179 section_color: None,
180 iteration: None,
181 tags: None,
182 })
183}
184
185fn severity_from_level(level: &str, message: &str) -> Severity {
186 match level {
187 "Error" => Severity::Error,
188 "Warning" => Severity::Warning,
189 "Info" | "Perf" | "Verbose" | "Debug" => Severity::Info,
190 _ => detect_severity_from_text(message),
191 }
192}
193
194fn flush_pending(
195 entries: &mut Vec<LogEntry>,
196 pending: &mut Option<PendingEntry>,
197 next_id: &mut u64,
198) {
199 if let Some(mut pending_entry) = pending.take() {
200 pending_entry.entry.id = *next_id;
201 pending_entry.entry.line_number = pending_entry.start_line;
202 entries.push(pending_entry.entry);
203 *next_id += 1;
204 }
205}
206
207fn fallback_entry(id: u64, line_number: u32, line: &str, file_path: &str) -> LogEntry {
208 LogEntry {
209 id,
210 line_number,
211 message: line.trim_end().to_string(),
212 component: None,
213 timestamp: None,
214 timestamp_display: None,
215 severity: detect_severity_from_text(line),
216 thread: None,
217 thread_display: None,
218 source_file: None,
219 format: LogFormat::Timestamped,
220 file_path: file_path.to_string(),
221 timezone_offset: None,
222 error_code_spans: Vec::new(),
223 ip_address: None,
224 host_name: None,
225 mac_address: None,
226 result_code: None,
227 gle_code: None,
228 setup_phase: None,
229 operation_name: None,
230 http_method: None,
231 uri_stem: None,
232 uri_query: None,
233 status_code: None,
234 sub_status: None,
235 time_taken_ms: None,
236 client_ip: None,
237 server_ip: None,
238 user_agent: None,
239 server_port: None,
240 username: None,
241 win32_status: None,
242 query_name: None,
243 query_type: None,
244 response_code: None,
245 dns_direction: None,
246 dns_protocol: None,
247 source_ip: None,
248 dns_flags: None,
249 dns_event_id: None,
250 zone_name: None,
251 entry_kind: None,
252 whatif: None,
253 section_name: None,
254 section_color: None,
255 iteration: None,
256 tags: None,
257 }
258}
259
260#[cfg(test)]
261mod tests {
262 use super::*;
263
264 #[test]
265 fn test_matches_dism_record() {
266 assert!(matches_dism_record(
267 "2024-01-15 08:00:00, Info DISM DISM Package Manager: Processing package"
268 ));
269 assert!(!matches_dism_record(
270 "2024-01-15 08:00:00, Info CBS Exec: Processing package"
271 ));
272 }
273
274 #[test]
275 fn test_parse_lines_groups_continuations() {
276 let lines = [
277 "2024-01-15 08:00:00, Info DISM DISM Provider Store: PID=100 TID=200 loaded provider",
278 "Continuation detail",
279 "2024-01-15 08:00:05, Warning DISM DISM Package Manager: Retry required",
280 ];
281
282 let (entries, parse_errors) = parse_lines(&lines, "C:/Windows/Logs/DISM/dism.log");
283
284 assert_eq!(parse_errors, 0);
285 assert_eq!(entries.len(), 2);
286 assert_eq!(entries[0].component.as_deref(), Some("DISM"));
287 assert_eq!(
288 entries[0].message,
289 "DISM Provider Store: PID=100 TID=200 loaded provider\nContinuation detail"
290 );
291 assert_eq!(entries[1].severity, Severity::Warning);
292 }
293
294 #[test]
295 fn test_parse_lines_salvages_structural_segments_with_unexpected_levels() {
296 let lines = [
297 "orphan preamble",
298 "2024-01-15 08:00:00, Info DISM DISM Package Manager: Processing package",
299 "Continuation detail",
300 "2024-01-15 08:00:01, UnexpectedLevel DISM malformed header",
301 "2024-01-15 08:00:02, Error DISM DISM Package Manager: Failed finalizing changes",
302 ];
303
304 let (entries, parse_errors) = parse_lines(&lines, "C:/Windows/Logs/DISM/dism.log");
305
306 assert_eq!(parse_errors, 1);
307 assert_eq!(entries.len(), 4);
308 assert_eq!(entries[0].message, "orphan preamble");
309 assert_eq!(
310 entries[1].message,
311 "DISM Package Manager: Processing package\nContinuation detail"
312 );
313 assert_eq!(entries[2].message, "malformed header");
314 assert_eq!(entries[2].component.as_deref(), Some("DISM"));
315 assert_eq!(entries[3].severity, Severity::Error);
316 assert_eq!(entries[3].component.as_deref(), Some("DISM"));
317 }
318
319 #[test]
320 fn test_parse_lines_accepts_cbs_components_in_dism_log() {
321 let lines = [
324 "2024-01-15 08:00:00, Info DISM DISM Provider Store: PID=100 TID=200 loaded provider",
325 "2024-01-15 08:00:01, Info CBS Exec: Processing package",
326 "2024-01-15 08:00:02, Warning CSI [SR] Cannot repair file",
327 "2024-01-15 08:00:03, Info DPX DPX scheduled restart",
328 "2024-01-15 08:00:04, Error WCP WCP transaction commit failed",
329 ];
330
331 let (entries, parse_errors) = parse_lines(&lines, "C:/Windows/Logs/DISM/dism.log");
332
333 assert_eq!(parse_errors, 0);
334 assert_eq!(entries.len(), 5);
335 assert_eq!(entries[0].component.as_deref(), Some("DISM"));
336 assert_eq!(entries[1].component.as_deref(), Some("CBS"));
337 assert_eq!(entries[2].component.as_deref(), Some("CSI"));
338 assert_eq!(entries[2].severity, Severity::Warning);
339 assert_eq!(entries[3].component.as_deref(), Some("DPX"));
340 assert_eq!(entries[4].component.as_deref(), Some("WCP"));
341 assert_eq!(entries[4].severity, Severity::Error);
342 }
343
344 #[test]
345 fn test_parse_lines_mixed_dism_cbs_with_continuations() {
346 let lines = [
348 "2024-01-15 08:00:00, Info CBS Exec: Started servicing",
349 " Package: KB5034123",
350 " State: Staged",
351 "2024-01-15 08:00:01, Info DISM DISM Package Manager: Finalizing",
352 ];
353
354 let (entries, parse_errors) = parse_lines(&lines, "C:/Windows/Logs/DISM/dism.log");
355
356 assert_eq!(parse_errors, 0);
357 assert_eq!(entries.len(), 2);
358 assert_eq!(entries[0].component.as_deref(), Some("CBS"));
359 assert_eq!(
360 entries[0].message,
361 "Exec: Started servicing\n Package: KB5034123\n State: Staged"
362 );
363 assert_eq!(entries[1].component.as_deref(), Some("DISM"));
364 }
365}