Skip to main content

cmtraceopen_parser/parser/
mod.rs

1pub mod burn;
2pub mod cbs;
3pub mod ccm;
4pub mod cmtlog;
5pub mod detect;
6pub mod dhcp;
7pub mod dism;
8pub mod dns_debug;
9pub mod dns_types;
10pub mod iis_w3c;
11pub mod intune_macos;
12pub mod msi;
13pub mod panther;
14pub mod patchmypc_detection;
15pub mod plain;
16pub mod psadt;
17pub mod registry;
18pub mod reporting_events;
19pub mod secureboot_log;
20pub mod severity;
21pub mod simple;
22pub mod timestamped;
23
24use crate::models::log_entry::{LogEntry, ParseResult};
25use std::path::Path;
26
27/// Post-process parsed entries to detect error code spans in messages.
28pub fn annotate_error_code_spans(entries: &mut [LogEntry]) {
29    for entry in entries.iter_mut() {
30        let spans = crate::error_db::lookup::detect_error_code_spans(&entry.message);
31        if !spans.is_empty() {
32            entry.error_code_spans = spans;
33        }
34    }
35}
36
37pub use detect::ResolvedParser;
38
39/// Result of parsing a single batch of records with a preselected parser.
40pub struct ParsedChunk {
41    pub entries: Vec<LogEntry>,
42    pub total_lines: u32,
43    pub parse_errors: u32,
44}
45
46/// Parse log content that has already been read into memory, auto-detecting its format.
47///
48/// `file_path` is used only for display/provenance and by format detection heuristics
49/// (e.g. extension-based hints) — no filesystem I/O is performed. `file_size` is the
50/// source file size in bytes and is passed through to `ParseResult`; callers that do
51/// not have a source file (e.g. pasted content) may pass `content.len() as u64`.
52///
53/// Native-only concerns (reading file bytes, decoding EVTX event logs, etc.) live in
54/// the src-tauri parser shim on top of this pure entry point.
55pub fn parse_content(
56    content: &str,
57    file_path: &str,
58    file_size: u64,
59) -> (ParseResult, ResolvedParser) {
60    let path_obj = Path::new(file_path);
61    let selection = detect::detect_parser(file_path, content);
62    let parsed_chunk = parse_content_with_selection(content, file_path, &selection);
63
64    let result = ParseResult {
65        entries: parsed_chunk.entries,
66        format_detected: selection.compatibility_format(),
67        parser_selection: selection.to_info(),
68        total_lines: parsed_chunk.total_lines,
69        parse_errors: parsed_chunk.parse_errors,
70        file_path: path_obj.to_string_lossy().to_string(),
71        file_size,
72        byte_offset: file_size,
73    };
74
75    (result, selection)
76}
77
78/// Parse already-split lines using the backend-owned parser selection.
79pub fn parse_lines_with_selection(
80    lines: &[&str],
81    file_path: &str,
82    selection: &ResolvedParser,
83) -> (Vec<LogEntry>, u32) {
84    let (mut entries, parse_errors) = match selection.implementation {
85        crate::models::log_entry::ParserImplementation::Ccm => {
86            ccm::parse_lines_with_specialization(lines, file_path, selection.specialization)
87        }
88        crate::models::log_entry::ParserImplementation::Simple => {
89            simple::parse_lines(lines, file_path)
90        }
91        crate::models::log_entry::ParserImplementation::IisW3c => {
92            iis_w3c::parse_lines(lines, file_path)
93        }
94        crate::models::log_entry::ParserImplementation::ReportingEvents => {
95            reporting_events::parse_lines(lines, file_path)
96        }
97        crate::models::log_entry::ParserImplementation::PlainText => {
98            plain::parse_lines(lines, file_path)
99        }
100        crate::models::log_entry::ParserImplementation::Msi => msi::parse_lines(lines, file_path),
101        crate::models::log_entry::ParserImplementation::PsadtLegacy => {
102            psadt::parse_lines(lines, file_path)
103        }
104        crate::models::log_entry::ParserImplementation::IntuneMacOs => {
105            intune_macos::parse_lines(lines, file_path)
106        }
107        crate::models::log_entry::ParserImplementation::Dhcp => dhcp::parse_lines(lines, file_path),
108        crate::models::log_entry::ParserImplementation::Burn => burn::parse_lines(lines, file_path),
109        crate::models::log_entry::ParserImplementation::PatchMyPcDetection => {
110            patchmypc_detection::parse_lines(lines, file_path)
111        }
112        crate::models::log_entry::ParserImplementation::Registry => {
113            // Registry files are parsed via a dedicated IPC command, not the log pipeline.
114            (vec![], 0)
115        }
116        crate::models::log_entry::ParserImplementation::SecureBootLog => {
117            secureboot_log::parse_lines(lines, file_path)
118        }
119        crate::models::log_entry::ParserImplementation::DnsDebug => {
120            dns_debug::parse_lines(lines, file_path, selection.date_order)
121        }
122        crate::models::log_entry::ParserImplementation::DnsAudit => {
123            // EVTX files are parsed via the binary path in parse_file(), not the line-based pipeline.
124            // SecureBootLog has its own dedicated IPC command; not routed through this pipeline.
125            (vec![], 0)
126        }
127        crate::models::log_entry::ParserImplementation::CmtLog => {
128            cmtlog::parse_lines(lines, file_path)
129        }
130        crate::models::log_entry::ParserImplementation::GenericTimestamped => {
131            match selection.parser {
132                crate::models::log_entry::ParserKind::Cbs => cbs::parse_lines(lines, file_path),
133                crate::models::log_entry::ParserKind::Dism => dism::parse_lines(lines, file_path),
134                crate::models::log_entry::ParserKind::Panther => {
135                    panther::parse_lines(lines, file_path)
136                }
137                _ => timestamped::parse_lines(lines, file_path, selection.date_order),
138            }
139        }
140    };
141    annotate_error_code_spans(&mut entries);
142    (entries, parse_errors)
143}
144
145/// Parse text content using the backend-owned parser selection.
146pub fn parse_content_with_selection(
147    content: &str,
148    file_path: &str,
149    selection: &ResolvedParser,
150) -> ParsedChunk {
151    let total_lines = content.lines().count() as u32;
152    let (mut entries, parse_errors) = match selection.implementation {
153        crate::models::log_entry::ParserImplementation::Ccm => {
154            ccm::parse_content(content, file_path, selection.specialization)
155        }
156        _ => {
157            let lines: Vec<&str> = content.lines().collect();
158            parse_lines_with_selection(&lines, file_path, selection)
159        }
160    };
161    // For CCM content path (which doesn't go through parse_lines_with_selection),
162    // ensure error code spans are annotated.
163    if matches!(
164        selection.implementation,
165        crate::models::log_entry::ParserImplementation::Ccm
166    ) {
167        annotate_error_code_spans(&mut entries);
168    }
169
170    ParsedChunk {
171        entries,
172        total_lines,
173        parse_errors,
174    }
175}
176
177/// Encoding detected from file BOM, used for both initial read and tailing.
178#[derive(Debug, Clone, Copy, PartialEq, Eq)]
179pub enum FileEncoding {
180    Utf8,
181    Utf16Le,
182    Utf16Be,
183}
184
185/// Detect encoding from the leading bytes of a file.
186pub fn detect_encoding(bytes: &[u8]) -> FileEncoding {
187    if bytes.starts_with(&[0xFF, 0xFE]) {
188        FileEncoding::Utf16Le
189    } else if bytes.starts_with(&[0xFE, 0xFF]) {
190        FileEncoding::Utf16Be
191    } else {
192        FileEncoding::Utf8
193    }
194}
195
196/// Decode raw bytes to a String based on the detected encoding.
197/// For UTF-16, also normalizes CRLF to LF.
198pub fn decode_bytes(bytes: &[u8], encoding: FileEncoding) -> Result<String, String> {
199    match encoding {
200        FileEncoding::Utf16Le => {
201            let data = if bytes.starts_with(&[0xFF, 0xFE]) {
202                &bytes[2..]
203            } else {
204                bytes
205            };
206            let (cow, _, had_errors) = encoding_rs::UTF_16LE.decode(data);
207            if had_errors {
208                log::warn!("Encoding errors during UTF-16LE decode");
209            }
210            Ok(cow.into_owned().replace("\r\n", "\n"))
211        }
212        FileEncoding::Utf16Be => {
213            let data = if bytes.starts_with(&[0xFE, 0xFF]) {
214                &bytes[2..]
215            } else {
216                bytes
217            };
218            let (cow, _, had_errors) = encoding_rs::UTF_16BE.decode(data);
219            if had_errors {
220                log::warn!("Encoding errors during UTF-16BE decode");
221            }
222            Ok(cow.into_owned().replace("\r\n", "\n"))
223        }
224        FileEncoding::Utf8 => {
225            let bytes_no_bom = if bytes.starts_with(&[0xEF, 0xBB, 0xBF]) {
226                &bytes[3..]
227            } else {
228                bytes
229            };
230            match std::str::from_utf8(bytes_no_bom) {
231                Ok(s) => Ok(s.to_string()),
232                Err(_) => {
233                    let (cow, _, had_errors) = encoding_rs::WINDOWS_1252.decode(bytes_no_bom);
234                    if had_errors {
235                        log::warn!("Encoding errors during Windows-1252 fallback decode");
236                    }
237                    Ok(cow.into_owned())
238                }
239            }
240        }
241    }
242}
243
244#[cfg(test)]
245mod tests {
246    use super::*;
247    use crate::models::log_entry::{
248        ParseQuality, ParserImplementation, ParserKind, ParserProvenance, ParserSpecialization,
249        RecordFraming,
250    };
251    use crate::parser::timestamped::DateOrder;
252
253    #[test]
254    fn test_parse_lines_with_selection_uses_timestamp_date_order() {
255        let selection = ResolvedParser::generic_timestamped(DateOrder::DayFirst);
256        let lines = ["15/01/2024 08:00:00 Processing request"];
257
258        let (entries, parse_errors) = parse_lines_with_selection(&lines, "sample.log", &selection);
259
260        assert_eq!(parse_errors, 0);
261        assert_eq!(entries.len(), 1);
262        assert_eq!(
263            entries[0].timestamp_display.as_deref(),
264            Some("2024-01-15 08:00:00.000")
265        );
266    }
267
268    #[test]
269    fn test_parse_lines_with_selection_can_use_panther_selection() {
270        let selection = ResolvedParser::new(
271            ParserKind::Panther,
272            ParserImplementation::GenericTimestamped,
273            ParserProvenance::Dedicated,
274            ParseQuality::SemiStructured,
275            RecordFraming::LogicalRecord,
276            DateOrder::MonthFirst,
277            None,
278        );
279        let lines = ["2024-01-15 08:00:00, Info SP Setup complete"];
280
281        let (entries, parse_errors) =
282            parse_lines_with_selection(&lines, "setupact.log", &selection);
283
284        assert_eq!(parse_errors, 0);
285        assert_eq!(entries.len(), 1);
286        assert_eq!(
287            selection.compatibility_format(),
288            crate::models::log_entry::LogFormat::Timestamped
289        );
290        assert_eq!(entries[0].message, "Setup complete");
291        assert_eq!(entries[0].component.as_deref(), Some("SP"));
292    }
293
294    #[test]
295    fn test_parse_lines_with_selection_can_use_cbs_selection() {
296        let selection = ResolvedParser::new(
297            ParserKind::Cbs,
298            ParserImplementation::GenericTimestamped,
299            ParserProvenance::Dedicated,
300            ParseQuality::SemiStructured,
301            RecordFraming::LogicalRecord,
302            DateOrder::MonthFirst,
303            None,
304        );
305        let lines = [
306            "2024-01-15 08:00:00, Info                  CBS    Exec: Started servicing",
307            "Continuation detail",
308        ];
309
310        let (entries, parse_errors) = parse_lines_with_selection(&lines, "CBS.log", &selection);
311
312        assert_eq!(parse_errors, 0);
313        assert_eq!(entries.len(), 1);
314        assert_eq!(
315            selection.compatibility_format(),
316            crate::models::log_entry::LogFormat::Timestamped
317        );
318        assert_eq!(entries[0].component.as_deref(), Some("CBS"));
319        assert_eq!(
320            entries[0].message,
321            "Exec: Started servicing\nContinuation detail"
322        );
323    }
324
325    #[test]
326    fn test_parse_lines_with_selection_can_use_dism_selection() {
327        let selection = ResolvedParser::new(
328            ParserKind::Dism,
329            ParserImplementation::GenericTimestamped,
330            ParserProvenance::Dedicated,
331            ParseQuality::SemiStructured,
332            RecordFraming::LogicalRecord,
333            DateOrder::MonthFirst,
334            None,
335        );
336        let lines = [
337            "2024-01-15 08:00:00, Warning               DISM   DISM Package Manager: Retry needed",
338            "Extra context",
339        ];
340
341        let (entries, parse_errors) = parse_lines_with_selection(&lines, "DISM.log", &selection);
342
343        assert_eq!(parse_errors, 0);
344        assert_eq!(entries.len(), 1);
345        assert_eq!(
346            selection.compatibility_format(),
347            crate::models::log_entry::LogFormat::Timestamped
348        );
349        assert_eq!(entries[0].component.as_deref(), Some("DISM"));
350        assert_eq!(
351            entries[0].message,
352            "DISM Package Manager: Retry needed\nExtra context"
353        );
354    }
355
356    #[test]
357    fn test_parse_lines_with_selection_can_use_reporting_events_selection() {
358        let selection = ResolvedParser::new(
359            ParserKind::ReportingEvents,
360            ParserImplementation::ReportingEvents,
361            ParserProvenance::Dedicated,
362            ParseQuality::Structured,
363            RecordFraming::PhysicalLine,
364            DateOrder::MonthFirst,
365            None,
366        );
367        let lines = [
368            "{11111111-1111-1111-1111-111111111111}\t2024-01-15 08:00:00:123\t1\tSoftware Update\t3\t{22222222-2222-2222-2222-222222222222}\t0x80240022\tWindows Update Agent\tFailure\tInstallation\tInstallation failed for KB5034123",
369        ];
370
371        let (entries, parse_errors) =
372            parse_lines_with_selection(&lines, "ReportingEvents.log", &selection);
373
374        assert_eq!(parse_errors, 0);
375        assert_eq!(entries.len(), 1);
376        assert_eq!(
377            selection.compatibility_format(),
378            crate::models::log_entry::LogFormat::Timestamped
379        );
380        assert_eq!(
381            entries[0].component.as_deref(),
382            Some("Windows Update Agent")
383        );
384        assert_eq!(
385            entries[0].severity,
386            crate::models::log_entry::Severity::Error
387        );
388    }
389
390    #[test]
391    fn test_parse_lines_with_selection_can_use_ime_specialization() {
392        let selection = ResolvedParser::new(
393            ParserKind::Ccm,
394            ParserImplementation::Ccm,
395            ParserProvenance::Dedicated,
396            ParseQuality::Structured,
397            RecordFraming::LogicalRecord,
398            DateOrder::MonthFirst,
399            Some(ParserSpecialization::Ime),
400        );
401        let lines = [
402            r#"<![LOG[Powershell execution is done, exitCode = 1]LOG]!><time="11:16:37.3093207" date="3-12-2026" component="HealthScripts" context="" type="1" thread="50" file="">"#,
403            r#"<![LOG[[HS] err output = Downloaded profile payload is not valid JSON."#,
404            r#"At C:\Windows\IMECache\HealthScripts\script.ps1:457 char:9"#,
405            r#"]LOG]!><time="11:16:42.3322734" date="3-12-2026" component="HealthScripts" context="" type="1" thread="50" file="">"#,
406        ];
407
408        let (entries, parse_errors) =
409            parse_lines_with_selection(&lines, "HealthScripts.log", &selection);
410
411        assert_eq!(parse_errors, 0);
412        assert_eq!(entries.len(), 2);
413        assert_eq!(entries[0].line_number, 1);
414        assert_eq!(entries[1].line_number, 2);
415        assert!(entries[1]
416            .message
417            .contains("Downloaded profile payload is not valid JSON"));
418        assert!(entries[1]
419            .message
420            .contains("At C:\\Windows\\IMECache\\HealthScripts\\script.ps1:457 char:9"));
421    }
422
423    #[test]
424    fn test_parse_content_with_selection_can_use_ime_specialization() {
425        let selection = ResolvedParser::new(
426            ParserKind::Ccm,
427            ParserImplementation::Ccm,
428            ParserProvenance::Dedicated,
429            ParseQuality::Structured,
430            RecordFraming::LogicalRecord,
431            DateOrder::MonthFirst,
432            Some(ParserSpecialization::Ime),
433        );
434        let content = concat!(
435            "<![LOG[Client Health evaluation starts.]LOG]!><time=\"23:00:10.6893636\" date=\"11-12-2025\" component=\"ClientHealth\" context=\"\" type=\"1\" thread=\"1\" file=\"\">\n",
436            "<![LOG[Set MdmDeviceCertificate : 3788C1E384FDCB3F173A3222CB4191883A94224E\n",
437            "More detail]LOG]!><time=\"23:00:11.4573058\" date=\"11-12-2025\" component=\"ClientHealth\" context=\"\" type=\"3\" thread=\"1\" file=\"\">"
438        );
439
440        let parsed = parse_content_with_selection(content, "ClientHealth.log", &selection);
441
442        assert_eq!(parsed.total_lines, 3);
443        assert_eq!(parsed.parse_errors, 0);
444        assert_eq!(parsed.entries.len(), 2);
445        assert_eq!(
446            parsed.entries[0].format,
447            crate::models::log_entry::LogFormat::Ccm
448        );
449        assert_eq!(parsed.entries[1].line_number, 2);
450        assert_eq!(
451            parsed.entries[1].severity,
452            crate::models::log_entry::Severity::Error
453        );
454        assert!(parsed.entries[1].message.contains("More detail"));
455    }
456
457    #[test]
458    fn test_parse_content_with_selection_keeps_non_ime_ccm_physical_line_behavior() {
459        let selection = ResolvedParser::ccm();
460        let content = concat!(
461            "<![LOG[Normal CCM record]LOG]!><time=\"08:00:00.000+000\" date=\"01-01-2024\" component=\"Test\" context=\"\" type=\"1\" thread=\"100\" file=\"\">\n",
462            "Continuation that should remain a plain fallback line"
463        );
464
465        let parsed = parse_content_with_selection(content, "sample.log", &selection);
466
467        assert_eq!(parsed.total_lines, 2);
468        assert_eq!(parsed.parse_errors, 1);
469        assert_eq!(parsed.entries.len(), 2);
470        assert_eq!(
471            parsed.entries[0].format,
472            crate::models::log_entry::LogFormat::Ccm
473        );
474        assert_eq!(
475            parsed.entries[1].format,
476            crate::models::log_entry::LogFormat::Plain
477        );
478        assert_eq!(parsed.entries[1].line_number, 2);
479    }
480
481    #[test]
482    fn test_parsed_entries_have_error_spans() {
483        let content = r#"<![LOG[Installation failed with error 0x80070005 access denied]LOG]!><time="10:00:00.000+000" date="01-01-2024" component="TestComp" context="" type="3" thread="1234" file="">"#;
484        let selection = ResolvedParser::ccm();
485        let parsed = parse_content_with_selection(content, "test.log", &selection);
486        assert_eq!(parsed.entries.len(), 1);
487        assert!(!parsed.entries[0].error_code_spans.is_empty());
488        assert_eq!(parsed.entries[0].error_code_spans[0].code_hex, "0x80070005");
489    }
490
491    #[test]
492    fn test_parse_lines_with_dns_debug_selection() {
493        let selection = ResolvedParser::dns_debug(DateOrder::MonthFirst);
494        let lines = [
495            "4/11/2026 3:29:17 PM 0294 PACKET  000002DAEC36D650 UDP Rcv 127.0.0.1       d07e   Q [0001   D   NOERROR] SOA    (4)home(4)gell(3)one(0)",
496            "4/11/2026 3:29:17 PM 0294 PACKET  000002DAEC36D650 UDP Snd 127.0.0.1       d07e R Q [8085 A DR  NOERROR] SOA    (4)home(4)gell(3)one(0)",
497        ];
498
499        let (entries, parse_errors) = parse_lines_with_selection(&lines, "dns.log", &selection);
500
501        assert_eq!(parse_errors, 0);
502        assert_eq!(entries.len(), 2);
503        assert_eq!(entries[0].query_name.as_deref(), Some("home.gell.one"));
504        assert_eq!(
505            entries[0].format,
506            crate::models::log_entry::LogFormat::DnsDebug
507        );
508    }
509}