Skip to main content

cmtraceopen_parser/parser/
panther.rs

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 panther_prefix_re() -> &'static Regex {
8    static CELL: OnceLock<Regex> = OnceLock::new();
9    CELL.get_or_init(|| {
10        Regex::new(r"^\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2},")
11            .expect("panther_prefix_re: date-time prefix pattern must compile")
12    })
13}
14
15/// Matches a bracketed executable tag at the start of the message, e.g. `[SetupPlatform.exe]`.
16fn exe_tag_re() -> &'static Regex {
17    static CELL: OnceLock<Regex> = OnceLock::new();
18    CELL.get_or_init(|| {
19        Regex::new(r"^\[([^\]]+\.[Ee][Xx][Ee])\]\s*")
20            .expect("exe_tag_re: bracketed executable tag pattern must compile")
21    })
22}
23
24/// Matches C++ class::method patterns, e.g. `CSetupManager::GetWuIdFromRegistry(13192):`.
25fn class_method_re() -> &'static Regex {
26    static CELL: OnceLock<Regex> = OnceLock::new();
27    CELL.get_or_init(|| {
28        Regex::new(r"^([A-Z][A-Za-z0-9_]*(?:::[A-Za-z_]\w*)+)(?:\(\d+\))?:\s*")
29            .expect("class_method_re: C++ class::method pattern must compile")
30    })
31}
32
33/// Matches DISM-style thread IDs embedded in the message, e.g. `PID=1452 TID=776`.
34fn tid_re() -> &'static Regex {
35    static CELL: OnceLock<Regex> = OnceLock::new();
36    CELL.get_or_init(|| {
37        Regex::new(r"TID=(\d+)").expect("tid_re: DISM thread ID pattern must compile")
38    })
39}
40
41/// Matches a primary result/error/status code, e.g.:
42///   `Result = 0x80070490`, `Error: 0x80070002`, `Status: 0xC000000F`
43fn result_code_re() -> &'static Regex {
44    static CELL: OnceLock<Regex> = OnceLock::new();
45    CELL.get_or_init(|| {
46        Regex::new(r"(?:Result\s*=|Error:|Status:)\s*(0x[0-9A-Fa-f]+)")
47            .expect("result_code_re: result/error/status hex code pattern must compile")
48    })
49}
50
51/// Matches a GetLastError annotation, e.g. `[gle=0x00000002]`.
52fn gle_re() -> &'static Regex {
53    static CELL: OnceLock<Regex> = OnceLock::new();
54    CELL.get_or_init(|| {
55        Regex::new(r"\[gle=(0x[0-9A-Fa-f]+)\]")
56            .expect("gle_re: GetLastError code pattern must compile")
57    })
58}
59
60/// Matches the current setup phase, e.g. `CurrentSetupPhase [SetupPhaseInstall]`.
61fn setup_phase_re() -> &'static Regex {
62    static CELL: OnceLock<Regex> = OnceLock::new();
63    CELL.get_or_init(|| {
64        Regex::new(r"CurrentSetupPhase\s+\[([A-Za-z0-9]+)\]")
65            .expect("setup_phase_re: setup phase pattern must compile")
66    })
67}
68
69/// Matches an operation being executed or completed, e.g.:
70///   `Executing operation: Apply Drivers`
71///   `Operation completed successfully: Apply Drivers`
72fn operation_re() -> &'static Regex {
73    static CELL: OnceLock<Regex> = OnceLock::new();
74    CELL.get_or_init(|| {
75        Regex::new(r"(?:Executing operation|Operation completed successfully):\s*(.+)")
76            .expect("operation_re: operation execution/completion pattern must compile")
77    })
78}
79
80fn panther_header_re() -> &'static Regex {
81    static CELL: OnceLock<Regex> = OnceLock::new();
82    CELL.get_or_init(|| {
83    Regex::new(
84        r"^(\d{4})-(\d{2})-(\d{2})\s+(\d{2}):(\d{2}):(\d{2}),\s+(Info|Warning|Error|Fatal Error|Perf)\s+(?:(\[0x[0-9A-Fa-f]+\])\s+)?(?:([A-Z][A-Z0-9_.-]{1,31})\s+)?(.*)$",
85    )
86    .expect("panther_header_re: strict Panther log header pattern must compile")
87})
88}
89
90fn panther_relaxed_header_re() -> &'static Regex {
91    static CELL: OnceLock<Regex> = OnceLock::new();
92    CELL.get_or_init(|| {
93    Regex::new(
94        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+(?:(\[0x[0-9A-Fa-f]+\])\s+)?(?:([A-Z][A-Z0-9_.-]{1,31})\s+)?(.*)$",
95    )
96    .expect("panther_relaxed_header_re: relaxed Panther log header pattern must compile")
97})
98}
99
100struct PendingEntry {
101    entry: LogEntry,
102    start_line: u32,
103}
104
105pub fn matches_panther_record(line: &str) -> bool {
106    panther_header_re().is_match(line)
107}
108
109pub fn parse_lines(lines: &[&str], file_path: &str) -> (Vec<LogEntry>, u32) {
110    let mut entries = Vec::new();
111    let mut parse_errors = 0;
112    let mut next_id = 0;
113    let mut pending: Option<PendingEntry> = None;
114
115    for (index, line) in lines.iter().enumerate() {
116        let line_number = (index + 1) as u32;
117
118        if let Some(entry) = parse_header(line, file_path) {
119            flush_pending(&mut entries, &mut pending, &mut next_id);
120            pending = Some(PendingEntry {
121                entry,
122                start_line: line_number,
123            });
124            continue;
125        }
126
127        if panther_prefix_re().is_match(line) {
128            flush_pending(&mut entries, &mut pending, &mut next_id);
129            entries.push(fallback_entry(next_id, line_number, line, file_path));
130            next_id += 1;
131            parse_errors += 1;
132            continue;
133        }
134
135        let trimmed_end = line.trim_end();
136        if trimmed_end.is_empty() {
137            continue;
138        }
139
140        if let Some(pending_entry) = pending.as_mut() {
141            if !pending_entry.entry.message.is_empty() {
142                pending_entry.entry.message.push('\n');
143            }
144            pending_entry.entry.message.push_str(trimmed_end);
145        } else {
146            entries.push(fallback_entry(next_id, line_number, trimmed_end, file_path));
147            next_id += 1;
148            parse_errors += 1;
149        }
150    }
151
152    flush_pending(&mut entries, &mut pending, &mut next_id);
153
154    (entries, parse_errors)
155}
156
157fn parse_header(line: &str, file_path: &str) -> Option<LogEntry> {
158    if let Some(caps) = panther_header_re().captures(line) {
159        return build_entry_from_caps(&caps, file_path);
160    }
161
162    let caps = panther_relaxed_header_re().captures(line)?;
163    build_entry_from_caps(&caps, file_path)
164}
165
166fn build_entry_from_caps(caps: &regex::Captures<'_>, file_path: &str) -> Option<LogEntry> {
167    let year: i32 = caps.get(1)?.as_str().parse().ok()?;
168    let month: u32 = caps.get(2)?.as_str().parse().ok()?;
169    let day: u32 = caps.get(3)?.as_str().parse().ok()?;
170    let hour: u32 = caps.get(4)?.as_str().parse().ok()?;
171    let minute: u32 = caps.get(5)?.as_str().parse().ok()?;
172    let second: u32 = caps.get(6)?.as_str().parse().ok()?;
173    let level = caps.get(7)?.as_str();
174    let code = caps.get(8).map(|m| m.as_str());
175    let component = caps.get(9).map(|m| m.as_str().to_string());
176    let raw_message = caps.get(10).map(|m| m.as_str()).unwrap_or("").trim_end();
177    let message = match code {
178        Some(code) if raw_message.is_empty() => code.to_string(),
179        Some(code) => format!("{} {}", code, raw_message),
180        None => raw_message.to_string(),
181    };
182
183    let timestamp = chrono::NaiveDate::from_ymd_opt(year, month, day)
184        .and_then(|date| date.and_hms_opt(hour, minute, second))
185        .map(|dt| dt.and_utc().timestamp_millis());
186
187    let mut entry = LogEntry {
188        id: 0,
189        line_number: 0,
190        message,
191        component,
192        timestamp,
193        timestamp_display: Some(format!(
194            "{:04}-{:02}-{:02} {:02}:{:02}:{:02}.000",
195            year, month, day, hour, minute, second
196        )),
197        severity: severity_from_level(level, raw_message),
198        thread: None,
199        thread_display: None,
200        source_file: None,
201        format: LogFormat::Timestamped,
202        file_path: file_path.to_string(),
203        timezone_offset: None,
204        error_code_spans: Vec::new(),
205        ip_address: None,
206        host_name: None,
207        mac_address: None,
208        result_code: None,
209        gle_code: None,
210        setup_phase: None,
211        operation_name: None,
212        http_method: None,
213        uri_stem: None,
214        uri_query: None,
215        status_code: None,
216        sub_status: None,
217        time_taken_ms: None,
218        client_ip: None,
219        server_ip: None,
220        user_agent: None,
221        server_port: None,
222        username: None,
223        win32_status: None,
224        query_name: None,
225        query_type: None,
226        response_code: None,
227        dns_direction: None,
228        dns_protocol: None,
229        source_ip: None,
230        dns_flags: None,
231        dns_event_id: None,
232        zone_name: None,
233        entry_kind: None,
234        whatif: None,
235        section_name: None,
236        section_color: None,
237        iteration: None,
238        tags: None,
239    };
240
241    post_process_entry(&mut entry);
242
243    Some(entry)
244}
245
246/// Extract additional structured fields from the message text.
247fn post_process_entry(entry: &mut LogEntry) {
248    // 1. Extract bracketed exe tag → source_file + component fallback
249    if let Some(caps) = exe_tag_re().captures(&entry.message) {
250        let exe_name = caps.get(1).unwrap().as_str().to_string();
251        // Strip the tag from the message
252        entry.message = entry.message[caps.get(0).unwrap().end()..].to_string();
253        // Use as component fallback when none was matched by the header regex
254        if entry.component.is_none() {
255            entry.component = Some(exe_name.trim_end_matches(".exe").to_string());
256        }
257        entry.source_file = Some(exe_name);
258    }
259
260    // 2. Extract C++ Class::Method as source_file (keep in message for context)
261    if entry.source_file.is_none() {
262        if let Some(caps) = class_method_re().captures(&entry.message) {
263            entry.source_file = Some(caps.get(1).unwrap().as_str().to_string());
264        }
265    }
266
267    // 3. Extract DISM TID as thread
268    if entry.thread.is_none() {
269        if let Some(caps) = tid_re().captures(&entry.message) {
270            if let Ok(tid) = caps.get(1).unwrap().as_str().parse::<u32>() {
271                entry.thread = Some(tid);
272                entry.thread_display = Some(format!("{} (0x{:04X})", tid, tid));
273            }
274        }
275    }
276
277    // 4. Extract primary result/error/status code
278    if let Some(caps) = result_code_re().captures(&entry.message) {
279        entry.result_code = Some(caps.get(1).unwrap().as_str().to_uppercase());
280    }
281
282    // 5. Extract GetLastError code
283    if let Some(caps) = gle_re().captures(&entry.message) {
284        entry.gle_code = Some(caps.get(1).unwrap().as_str().to_uppercase());
285    }
286
287    // 6. Extract current setup phase
288    if let Some(caps) = setup_phase_re().captures(&entry.message) {
289        entry.setup_phase = Some(caps.get(1).unwrap().as_str().to_string());
290    }
291
292    // 7. Extract operation name from execution/completion lines
293    if let Some(caps) = operation_re().captures(&entry.message) {
294        entry.operation_name = Some(caps.get(1).unwrap().as_str().trim().to_string());
295    }
296}
297
298fn severity_from_level(level: &str, message: &str) -> Severity {
299    match level {
300        "Error" | "Fatal Error" => Severity::Error,
301        "Warning" => Severity::Warning,
302        "Perf" | "Info" => Severity::Info,
303        _ => detect_severity_from_text(message),
304    }
305}
306
307fn flush_pending(
308    entries: &mut Vec<LogEntry>,
309    pending: &mut Option<PendingEntry>,
310    next_id: &mut u64,
311) {
312    if let Some(mut pending_entry) = pending.take() {
313        pending_entry.entry.id = *next_id;
314        pending_entry.entry.line_number = pending_entry.start_line;
315        entries.push(pending_entry.entry);
316        *next_id += 1;
317    }
318}
319
320fn fallback_entry(id: u64, line_number: u32, line: &str, file_path: &str) -> LogEntry {
321    LogEntry {
322        id,
323        line_number,
324        message: line.trim_end().to_string(),
325        component: None,
326        timestamp: None,
327        timestamp_display: None,
328        severity: detect_severity_from_text(line),
329        thread: None,
330        thread_display: None,
331        source_file: None,
332        format: LogFormat::Timestamped,
333        file_path: file_path.to_string(),
334        timezone_offset: None,
335        error_code_spans: Vec::new(),
336        ip_address: None,
337        host_name: None,
338        mac_address: None,
339        result_code: None,
340        gle_code: None,
341        setup_phase: None,
342        operation_name: None,
343        http_method: None,
344        uri_stem: None,
345        uri_query: None,
346        status_code: None,
347        sub_status: None,
348        time_taken_ms: None,
349        client_ip: None,
350        server_ip: None,
351        user_agent: None,
352        server_port: None,
353        username: None,
354        win32_status: None,
355        query_name: None,
356        query_type: None,
357        response_code: None,
358        dns_direction: None,
359        dns_protocol: None,
360        source_ip: None,
361        dns_flags: None,
362        dns_event_id: None,
363        zone_name: None,
364        entry_kind: None,
365        whatif: None,
366        section_name: None,
367        section_color: None,
368        iteration: None,
369        tags: None,
370    }
371}
372
373#[cfg(test)]
374mod tests {
375    use super::*;
376
377    #[test]
378    fn test_matches_panther_record() {
379        assert!(matches_panther_record(
380            "2024-01-15 08:00:00, Info [0x080489] MIG Setting system object filter context (System)"
381        ));
382        assert!(!matches_panther_record("plain text"));
383    }
384
385    #[test]
386    fn test_parse_lines_groups_continuations() {
387        let lines = [
388            "2024-01-15 08:00:00, Info [0x080489] MIG Gather started",
389            "Additional migration detail",
390            "    indented continuation",
391            "2024-01-15 08:00:05, Warning SP Retry required",
392        ];
393
394        let (entries, parse_errors) = parse_lines(&lines, "C:/Windows/Panther/setupact.log");
395
396        assert_eq!(parse_errors, 0);
397        assert_eq!(entries.len(), 2);
398        assert_eq!(entries[0].component.as_deref(), Some("MIG"));
399        assert_eq!(
400            entries[0].message,
401            "[0x080489] Gather started\nAdditional migration detail\n    indented continuation"
402        );
403        assert_eq!(entries[0].line_number, 1);
404        assert_eq!(entries[1].severity, Severity::Warning);
405    }
406
407    #[test]
408    fn test_parse_lines_salvages_structural_segments_with_unexpected_levels() {
409        let lines = [
410            "orphan preamble",
411            "2024-01-15 08:00:00, Info SP Setup started",
412            "continuation detail",
413            "2024-01-15 08:00:01, UnexpectedLevel SP malformed header",
414            "2024-01-15 08:00:02, Error SP Setup failed",
415        ];
416
417        let (entries, parse_errors) = parse_lines(&lines, "C:/Windows/Panther/setuperr.log");
418
419        assert_eq!(parse_errors, 1);
420        assert_eq!(entries.len(), 4);
421        assert_eq!(entries[0].message, "orphan preamble");
422        assert_eq!(entries[1].message, "Setup started\ncontinuation detail");
423        assert_eq!(entries[2].message, "malformed header");
424        assert_eq!(entries[2].component.as_deref(), Some("SP"));
425        assert_eq!(entries[3].severity, Severity::Error);
426        assert_eq!(entries[3].component.as_deref(), Some("SP"));
427    }
428
429    #[test]
430    fn test_parse_lines_handles_missing_component() {
431        let lines =
432            ["2024-01-15 08:00:08, Error                  Gather failed. Last error: 0x00000000"];
433
434        let (entries, parse_errors) = parse_lines(&lines, "C:/Windows/Panther/setupact.log");
435
436        assert_eq!(parse_errors, 0);
437        assert_eq!(entries.len(), 1);
438        assert!(entries[0].component.is_none());
439        assert_eq!(entries[0].severity, Severity::Error);
440    }
441
442    #[test]
443    fn test_exe_tag_extracted_as_source_file_and_component_fallback() {
444        let lines = [
445            "2024-01-15 08:00:00, Error                  [SetupPlatform.exe] System disks found",
446        ];
447
448        let (entries, _) = parse_lines(&lines, "C:/Windows/Panther/setuperr.log");
449
450        assert_eq!(entries.len(), 1);
451        assert_eq!(entries[0].source_file.as_deref(), Some("SetupPlatform.exe"));
452        assert_eq!(entries[0].component.as_deref(), Some("SetupPlatform"));
453        assert_eq!(entries[0].message, "System disks found");
454    }
455
456    #[test]
457    fn test_exe_tag_does_not_overwrite_existing_component() {
458        let lines = [
459            "2024-01-15 08:00:00, Warning               MOUPG  [SetupHost.exe] Something happened",
460        ];
461
462        let (entries, _) = parse_lines(&lines, "C:/Windows/Panther/setupact.log");
463
464        assert_eq!(entries.len(), 1);
465        assert_eq!(entries[0].component.as_deref(), Some("MOUPG"));
466        assert_eq!(entries[0].source_file.as_deref(), Some("SetupHost.exe"));
467    }
468
469    #[test]
470    fn test_class_method_extracted_as_source_file() {
471        let lines = [
472            "2024-01-15 08:00:00, Error                 MOUPG  CUnattendManager::Initialize(90): Result = 0x80070490",
473        ];
474
475        let (entries, _) = parse_lines(&lines, "C:/Windows/Panther/setuperr.log");
476
477        assert_eq!(entries.len(), 1);
478        assert_eq!(
479            entries[0].source_file.as_deref(),
480            Some("CUnattendManager::Initialize")
481        );
482        // Message should still contain the full text for context
483        assert!(entries[0].message.contains("CUnattendManager::Initialize"));
484    }
485
486    #[test]
487    fn test_dism_tid_extracted_as_thread() {
488        let lines = [
489            "2024-01-15 08:00:00, Info                  DISM   API: PID=1452 TID=776 DismApi.dll: session started",
490        ];
491
492        let (entries, _) = parse_lines(&lines, "C:/Windows/Panther/setupact.log");
493
494        assert_eq!(entries.len(), 1);
495        assert_eq!(entries[0].thread, Some(776));
496        assert_eq!(entries[0].thread_display.as_deref(), Some("776 (0x0308)"));
497    }
498
499    #[test]
500    fn test_perf_severity_mapped_to_info() {
501        let lines =
502            ["2024-01-15 08:00:00, Perf                   SP     Timing checkpoint reached"];
503
504        let (entries, _) = parse_lines(&lines, "C:/Windows/Panther/setupact.log");
505
506        assert_eq!(entries.len(), 1);
507        assert_eq!(entries[0].severity, Severity::Info);
508    }
509}