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