cmtraceopen-parser 0.1.1

Parse ConfigMgr/SCCM, Intune, CBS, DISM, Panther, MSI, and PSADT logs with format auto-detection, severity classification, and Windows error-code lookup. Pure Rust, no I/O, native + wasm32.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! PSADT Legacy format parser.
//!
//! Parses log lines produced by PSAppDeployToolkit v4 with `LogStyle = 'Legacy'`:
//!   [2024-12-24 14:44:13.658] [Install] [Start-ADTMsiProcess] [Error] :: Message text here
//!
//! Fields extracted:
//!   1. timestamp  (yyyy-MM-dd HH:mm:ss.fff)
//!   2. section    (Initialization, Pre-Install, Install, Post-Install, Finalization)
//!   3. source     (PSADT function name, e.g. Close-ADTSession)
//!   4. severity   (Info, Warning, Error, Success)
//!   5. message    (everything after `:: `)

use regex::Regex;

use super::severity::detect_severity_from_text;
use crate::models::log_entry::{LogEntry, LogFormat, Severity};
use std::sync::OnceLock;

/// Full PSADT Legacy line regex.
fn psadt_line_re() -> &'static Regex {
    static CELL: OnceLock<Regex> = OnceLock::new();
    CELL.get_or_init(|| {
    Regex::new(
        r"^\[(\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3})\]\s\[([^\]]+)\]\s\[([^\]]+)\]\s\[(\w+)\]\s::\s(.*)$",
    )
    .expect("PSADT Legacy line regex must compile")
})
}

/// Lightweight prefix check for detection (avoids running the full regex).
fn psadt_prefix_re() -> &'static Regex {
    static CELL: OnceLock<Regex> = OnceLock::new();
    CELL.get_or_init(|| {
        Regex::new(r"^\[\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}\.\d{3}\]\s\[")
            .expect("PSADT prefix regex must compile")
    })
}

/// Known PSADT section names.
static KNOWN_SECTIONS: &[&str] = &[
    "Initialization",
    "Pre-Install",
    "Install",
    "Post-Install",
    "Finalization",
];

/// Known PSADT function names.
static KNOWN_FUNCTIONS: &[&str] = &["Open-ADTSession", "Close-ADTSession", "Start-ADTMsiProcess"];

/// Map the PSADT severity token to a `Severity` variant.
fn map_severity(token: &str) -> Severity {
    match token {
        "Error" => Severity::Error,
        "Warning" | "Warn" => Severity::Warning,
        // "Info", "Information", "Success", or anything else → Info
        _ => Severity::Info,
    }
}

/// Parse a single PSADT Legacy format line.
fn parse_line(line: &str) -> Option<PsadtParsed> {
    let caps = psadt_line_re().captures(line)?;

    let ts_str = caps.get(1)?.as_str();
    let section = caps.get(2)?.as_str();
    let source = caps.get(3)?.as_str();
    let sev_token = caps.get(4)?.as_str();
    let message = caps.get(5)?.as_str();

    // Parse timestamp with chrono
    let ndt = chrono::NaiveDateTime::parse_from_str(ts_str, "%Y-%m-%d %H:%M:%S%.3f").ok()?;
    let timestamp = ndt.and_utc().timestamp_millis();

    // Format display as MM-dd-yyyy HH:mm:ss.fff
    let timestamp_display = format!(
        "{:02}-{:02}-{:04} {:02}:{:02}:{:02}.{:03}",
        ndt.month(),
        ndt.day(),
        ndt.year(),
        ndt.hour(),
        ndt.minute(),
        ndt.second(),
        ndt.and_utc().timestamp_subsec_millis()
    );

    Some(PsadtParsed {
        timestamp,
        timestamp_display,
        section: section.to_string(),
        source: source.to_string(),
        severity: map_severity(sev_token),
        message: message.to_string(),
    })
}

struct PsadtParsed {
    timestamp: i64,
    timestamp_display: String,
    section: String,
    source: String,
    severity: Severity,
    message: String,
}

// Need these for the timestamp formatting in parse_line
use chrono::{Datelike, Timelike};

/// Parse all lines as PSADT Legacy format.
/// Lines that don't match the format fall back to plain text entries with
/// text-based severity detection.
pub fn parse_lines(lines: &[&str], file_path: &str) -> (Vec<LogEntry>, u32) {
    let mut entries = Vec::with_capacity(lines.len());
    let mut errors = 0u32;
    let mut id_counter = 0u64;

    for (i, line) in lines.iter().enumerate() {
        if line.trim().is_empty() {
            continue;
        }

        match parse_line(line) {
            Some(parsed) => {
                entries.push(LogEntry {
                    id: id_counter,
                    line_number: (i + 1) as u32,
                    message: parsed.message,
                    component: Some(parsed.section),
                    timestamp: Some(parsed.timestamp),
                    timestamp_display: Some(parsed.timestamp_display),
                    severity: parsed.severity,
                    thread: None,
                    thread_display: None,
                    source_file: Some(parsed.source),
                    format: LogFormat::Timestamped,
                    file_path: file_path.to_string(),
                    timezone_offset: None,
                    error_code_spans: Vec::new(),
                    ip_address: None,
                    host_name: None,
                    mac_address: None,
                    result_code: None,
                    gle_code: None,
                    setup_phase: None,
                    operation_name: None,
                    http_method: None,
                    uri_stem: None,
                    uri_query: None,
                    status_code: None,
                    sub_status: None,
                    time_taken_ms: None,
                    client_ip: None,
                    server_ip: None,
                    user_agent: None,
                    server_port: None,
                    username: None,
                    win32_status: None,
                    query_name: None,
                    query_type: None,
                    response_code: None,
                    dns_direction: None,
                    dns_protocol: None,
                    source_ip: None,
                    dns_flags: None,
                    dns_event_id: None,
                    zone_name: None,
                    entry_kind: None,
                    whatif: None,
                    section_name: None,
                    section_color: None,
                    iteration: None,
                    tags: None,
                });
                id_counter += 1;
            }
            None => {
                // Doesn't match PSADT format — treat as plain text
                entries.push(LogEntry {
                    id: id_counter,
                    line_number: (i + 1) as u32,
                    message: line.to_string(),
                    component: None,
                    timestamp: None,
                    timestamp_display: None,
                    severity: detect_severity_from_text(line),
                    thread: None,
                    thread_display: None,
                    source_file: None,
                    format: LogFormat::Plain,
                    file_path: file_path.to_string(),
                    timezone_offset: None,
                    error_code_spans: Vec::new(),
                    ip_address: None,
                    host_name: None,
                    mac_address: None,
                    result_code: None,
                    gle_code: None,
                    setup_phase: None,
                    operation_name: None,
                    http_method: None,
                    uri_stem: None,
                    uri_query: None,
                    status_code: None,
                    sub_status: None,
                    time_taken_ms: None,
                    client_ip: None,
                    server_ip: None,
                    user_agent: None,
                    server_port: None,
                    username: None,
                    win32_status: None,
                    query_name: None,
                    query_type: None,
                    response_code: None,
                    dns_direction: None,
                    dns_protocol: None,
                    source_ip: None,
                    dns_flags: None,
                    dns_event_id: None,
                    zone_name: None,
                    entry_kind: None,
                    whatif: None,
                    section_name: None,
                    section_color: None,
                    iteration: None,
                    tags: None,
                });
                id_counter += 1;
                errors += 1;
            }
        }
    }

    (entries, errors)
}

/// Detection helper: returns a weight indicating how likely the line is PSADT Legacy format.
///
/// Weight breakdown:
/// - Full bracketed timestamp prefix `[yyyy-MM-dd HH:mm:ss.fff] [` → 3
/// - Known section name (e.g. `[Initialization]`) → +1
/// - Known PSADT function name (e.g. `[Open-ADTSession]`) → +1
/// - Banner delimiter (10+ asterisks in a bracketed line) → 2
pub fn matches_psadt_legacy_content(line: &str) -> u32 {
    let mut weight = 0u32;

    // Check for the bracketed timestamp prefix
    if psadt_prefix_re().is_match(line) {
        weight += 3;

        // Check for known section names
        for section in KNOWN_SECTIONS {
            let bracketed = format!("[{}]", section);
            if line.contains(&bracketed) {
                weight += 1;
                break;
            }
        }

        // Check for known PSADT function names
        for func in KNOWN_FUNCTIONS {
            let bracketed = format!("[{}]", func);
            if line.contains(&bracketed) {
                weight += 1;
                break;
            }
        }

        // Check for banner delimiter (10+ asterisks)
        if line.contains("**********") {
            weight += 2;
        }
    }

    weight
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_info_line() {
        let line =
            "[2024-12-24 14:44:13.658] [Finalization] [Close-ADTSession] [Info] :: Message text here";
        let parsed = parse_line(line).expect("should parse");
        assert_eq!(parsed.message, "Message text here");
        assert_eq!(parsed.section, "Finalization");
        assert_eq!(parsed.source, "Close-ADTSession");
        assert_eq!(parsed.severity, Severity::Info);
        assert_eq!(parsed.timestamp_display, "12-24-2024 14:44:13.658");
    }

    #[test]
    fn test_parse_error_line() {
        let line = "[2024-12-24 14:44:13.700] [Install] [Start-ADTMsiProcess] [Error] :: MSI installation failed. [Exit code: 1603]";
        let parsed = parse_line(line).expect("should parse");
        assert_eq!(parsed.message, "MSI installation failed. [Exit code: 1603]");
        assert_eq!(parsed.section, "Install");
        assert_eq!(parsed.source, "Start-ADTMsiProcess");
        assert_eq!(parsed.severity, Severity::Error);
    }

    #[test]
    fn test_parse_warning_line() {
        let line =
            "[2024-12-24 14:44:14.000] [Pre-Install] [Test-Function] [Warning] :: Something is off";
        let parsed = parse_line(line).expect("should parse");
        assert_eq!(parsed.severity, Severity::Warning);
        assert_eq!(parsed.section, "Pre-Install");
    }

    #[test]
    fn test_parse_success_maps_to_info() {
        let line = "[2024-12-24 14:44:14.000] [Post-Install] [Test-Function] [Success] :: All good";
        let parsed = parse_line(line).expect("should parse");
        assert_eq!(parsed.severity, Severity::Info);
    }

    #[test]
    fn test_parse_banner_line() {
        let line = "[2024-12-24 14:44:13.658] [Initialization] [Open-ADTSession] [Info] :: *******************************************************************************";
        let parsed = parse_line(line).expect("should parse");
        assert_eq!(parsed.severity, Severity::Info);
        assert!(parsed.message.contains("***"));
    }

    #[test]
    fn test_parse_lines_mixed() {
        let lines = vec![
            "[2024-12-24 14:44:13.658] [Finalization] [Close-ADTSession] [Info] :: Session closed",
            "Some random non-matching line",
            "[2024-12-24 14:44:13.700] [Install] [Start-ADTMsiProcess] [Error] :: MSI failed",
        ];
        let (entries, errors) = parse_lines(&lines, "test.log");
        assert_eq!(entries.len(), 3);
        assert_eq!(errors, 1);

        // First entry: structured
        assert_eq!(entries[0].component.as_deref(), Some("Finalization"));
        assert_eq!(entries[0].source_file.as_deref(), Some("Close-ADTSession"));
        assert_eq!(entries[0].severity, Severity::Info);
        assert_eq!(entries[0].format, LogFormat::Timestamped);

        // Second entry: plain fallback
        assert_eq!(entries[1].message, "Some random non-matching line");
        assert_eq!(entries[1].format, LogFormat::Plain);
        assert!(entries[1].component.is_none());

        // Third entry: structured error
        assert_eq!(entries[2].severity, Severity::Error);
        assert_eq!(entries[2].component.as_deref(), Some("Install"));
    }

    #[test]
    fn test_parse_lines_skips_empty() {
        let lines = vec![
            "",
            "  ",
            "[2024-12-24 14:44:13.658] [Install] [Func] [Info] :: msg",
        ];
        let (entries, errors) = parse_lines(&lines, "test.log");
        assert_eq!(entries.len(), 1);
        assert_eq!(errors, 0);
    }

    #[test]
    fn test_parse_lines_file_path_propagated() {
        let lines = vec!["[2024-12-24 14:44:13.658] [Install] [Func] [Info] :: msg"];
        let (entries, _) = parse_lines(&lines, "/path/to/deploy.log");
        assert_eq!(entries[0].file_path, "/path/to/deploy.log");
    }

    #[test]
    fn test_timestamp_to_millis() {
        let line = "[2024-12-24 14:44:13.658] [Install] [Func] [Info] :: msg";
        let parsed = parse_line(line).expect("should parse");
        // 2024-12-24 14:44:13.658 UTC → verify it's a reasonable millis value
        assert!(parsed.timestamp > 0);
        // Rough check: 2024-12-24 is around epoch millis 1735000000000
        assert!(parsed.timestamp > 1_735_000_000_000);
        assert!(parsed.timestamp < 1_736_000_000_000);
    }

    // Detection tests

    #[test]
    fn test_matches_full_line() {
        let line =
            "[2024-12-24 14:44:13.658] [Finalization] [Close-ADTSession] [Info] :: Message text";
        let weight = matches_psadt_legacy_content(line);
        // 3 (prefix) + 1 (known section) + 1 (known function) = 5
        assert_eq!(weight, 5);
    }

    #[test]
    fn test_matches_unknown_section_and_function() {
        let line =
            "[2024-12-24 14:44:13.658] [CustomSection] [Custom-Function] [Info] :: Some message";
        let weight = matches_psadt_legacy_content(line);
        // 3 (prefix only, no known section or function)
        assert_eq!(weight, 3);
    }

    #[test]
    fn test_matches_banner_line() {
        let line = "[2024-12-24 14:44:13.658] [Initialization] [Open-ADTSession] [Info] :: *******************************************************************************";
        let weight = matches_psadt_legacy_content(line);
        // 3 (prefix) + 1 (known section) + 1 (known function) + 2 (banner) = 7
        assert_eq!(weight, 7);
    }

    #[test]
    fn test_matches_non_psadt_line() {
        let weight = matches_psadt_legacy_content("Just a plain text line");
        assert_eq!(weight, 0);
    }

    #[test]
    fn test_matches_similar_but_not_psadt() {
        // Has a bracketed timestamp but wrong format
        let weight = matches_psadt_legacy_content("[2024-12-24] Some log message");
        assert_eq!(weight, 0);
    }
}