Skip to main content

cmtraceopen_parser/models/
log_entry.rs

1use serde::{Deserialize, Serialize};
2
3/// Log entry severity level.
4/// Maps directly to CMTrace's type field: 0=Success, 1=Info, 2=Warning, 3=Error.
5/// `Success` corresponds to CCM/PSADT `type="0"` — a completed operation that
6/// OneTrace renders with a green tick.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
8pub enum Severity {
9    Success,
10    Info,
11    Warning,
12    Error,
13}
14
15/// Distinguishes the role of a CmtLog entry: regular log line, section divider, loop
16/// iteration marker, or file header.
17#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
18pub enum EntryKind {
19    #[default]
20    Log,
21    Section,
22    Iteration,
23    Header,
24}
25
26/// Which log format was detected/used to parse this entry.
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
28pub enum LogFormat {
29    /// CCM/SCCM format: <![LOG[msg]LOG]!><time="..." date="..." ...>
30    Ccm,
31    /// Simple/legacy format: message$$<Component><timestamp><thread>
32    Simple,
33    /// Plain text (no structured format detected)
34    Plain,
35    /// Generic timestamped format (ISO 8601, slash-dates, syslog, time-only)
36    Timestamped,
37    /// Windows DNS Server debug log (dns.log)
38    DnsDebug,
39    /// Windows DNS Server analytical/audit EVTX log
40    DnsAudit,
41    /// CCM-style text lines with extended attributes (.cmtlog)
42    CmtLog,
43}
44
45/// High-level parser selection resolved by the backend.
46#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
47#[serde(rename_all = "camelCase")]
48pub enum ParserKind {
49    Ccm,
50    Simple,
51    Timestamped,
52    Plain,
53    IisW3c,
54    Panther,
55    Cbs,
56    Dism,
57    ReportingEvents,
58    Msi,
59    PsadtLegacy,
60    IntuneMacOs,
61    Dhcp,
62    Burn,
63    PatchMyPcDetection,
64    Registry,
65    SecureBootLog,
66    DnsDebug,
67    DnsAudit,
68    CmtLog,
69}
70
71/// Concrete parser implementation currently used by the backend.
72#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
73#[serde(rename_all = "camelCase")]
74pub enum ParserImplementation {
75    Ccm,
76    Simple,
77    GenericTimestamped,
78    IisW3c,
79    ReportingEvents,
80    PlainText,
81    Msi,
82    PsadtLegacy,
83    IntuneMacOs,
84    Dhcp,
85    Burn,
86    PatchMyPcDetection,
87    Registry,
88    SecureBootLog,
89    DnsDebug,
90    DnsAudit,
91    CmtLog,
92}
93
94/// How the backend arrived at the parser selection.
95#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
96#[serde(rename_all = "camelCase")]
97pub enum ParserProvenance {
98    Dedicated,
99    Heuristic,
100    Fallback,
101}
102
103/// Approximate structure quality of the current parse path.
104#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
105#[serde(rename_all = "camelCase")]
106pub enum ParseQuality {
107    Structured,
108    SemiStructured,
109    TextFallback,
110}
111
112/// How input is framed before it is handed to a parser implementation.
113#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
114#[serde(rename_all = "camelCase")]
115pub enum RecordFraming {
116    PhysicalLine,
117    LogicalRecord,
118}
119
120/// Slash-date interpretation used by timestamp-aware parsers.
121#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
122#[serde(rename_all = "camelCase")]
123pub enum DateFieldOrder {
124    MonthFirst,
125    DayFirst,
126}
127
128/// Optional parser specialization layered on top of the base parser kind.
129#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
130#[serde(rename_all = "camelCase")]
131pub enum ParserSpecialization {
132    Ime,
133}
134
135/// Rich parser selection metadata returned to the frontend.
136#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
137#[serde(rename_all = "camelCase")]
138pub struct ParserSelectionInfo {
139    pub parser: ParserKind,
140    pub implementation: ParserImplementation,
141    pub provenance: ParserProvenance,
142    pub parse_quality: ParseQuality,
143    pub record_framing: RecordFraming,
144    pub date_order: Option<DateFieldOrder>,
145    pub specialization: Option<ParserSpecialization>,
146}
147
148/// A single parsed log entry.
149/// Field names use camelCase for direct JSON serialization to TypeScript.
150#[derive(Debug, Clone, Serialize, Deserialize)]
151#[serde(rename_all = "camelCase")]
152pub struct LogEntry {
153    /// Sequential ID for stable row identity
154    pub id: u64,
155    /// 1-based line number in the source file
156    pub line_number: u32,
157    /// The log message text
158    pub message: String,
159    /// Component name (up to 100 chars in CCM format)
160    pub component: Option<String>,
161    /// Unix timestamp in milliseconds (for sorting/merging)
162    pub timestamp: Option<i64>,
163    /// Formatted display string: "MM-dd-yyyy HH:mm:ss.fff"
164    pub timestamp_display: Option<String>,
165    /// Severity level
166    pub severity: Severity,
167    /// Thread ID as a number
168    pub thread: Option<u32>,
169    /// Thread display string: "N (0xNNNN)"
170    pub thread_display: Option<String>,
171    /// Source file attribute (CCM format only)
172    pub source_file: Option<String>,
173    /// Which format was used to parse this entry
174    pub format: LogFormat,
175    /// Path to the file this entry came from
176    pub file_path: String,
177    /// Timezone offset in minutes
178    pub timezone_offset: Option<i32>,
179    /// Spans of recognized error codes within the message text
180    #[serde(default, skip_serializing_if = "Vec::is_empty")]
181    pub error_code_spans: Vec<crate::error_db::lookup::ErrorCodeSpan>,
182    /// IP address (DHCP logs)
183    #[serde(default, skip_serializing_if = "Option::is_none")]
184    pub ip_address: Option<String>,
185    /// Host/device name (DHCP logs)
186    #[serde(default, skip_serializing_if = "Option::is_none")]
187    pub host_name: Option<String>,
188    /// MAC address (DHCP logs)
189    #[serde(default, skip_serializing_if = "Option::is_none")]
190    pub mac_address: Option<String>,
191    /// Primary result/error code extracted from the message (Panther logs)
192    /// e.g. from "Result = 0x80070490", "Error: 0x80070002", "Status: 0xC000000F"
193    #[serde(default, skip_serializing_if = "Option::is_none")]
194    pub result_code: Option<String>,
195    /// GetLastError code from "[gle=0x...]" (Panther logs)
196    #[serde(default, skip_serializing_if = "Option::is_none")]
197    pub gle_code: Option<String>,
198    /// Windows setup phase name (Panther logs)
199    /// e.g. "SetupPhaseInstall", "SetupPhasePrepare", "SetupPhaseFinalize"
200    #[serde(default, skip_serializing_if = "Option::is_none")]
201    pub setup_phase: Option<String>,
202    /// Setup operation being executed or completed (Panther logs)
203    /// e.g. "Apply Drivers", "Mount WIM file", "Set Language Settings"
204    #[serde(default, skip_serializing_if = "Option::is_none")]
205    pub operation_name: Option<String>,
206    /// HTTP method (IIS W3C logs)
207    #[serde(default, skip_serializing_if = "Option::is_none")]
208    pub http_method: Option<String>,
209    /// URI stem/path (IIS W3C logs)
210    #[serde(default, skip_serializing_if = "Option::is_none")]
211    pub uri_stem: Option<String>,
212    /// URI query string without the leading '?' (IIS W3C logs)
213    #[serde(default, skip_serializing_if = "Option::is_none")]
214    pub uri_query: Option<String>,
215    /// HTTP response status code (IIS W3C logs)
216    #[serde(default, skip_serializing_if = "Option::is_none")]
217    pub status_code: Option<u16>,
218    /// HTTP response sub-status code (IIS W3C logs)
219    #[serde(default, skip_serializing_if = "Option::is_none")]
220    pub sub_status: Option<u16>,
221    /// Request duration in milliseconds (IIS W3C logs)
222    #[serde(default, skip_serializing_if = "Option::is_none")]
223    pub time_taken_ms: Option<u64>,
224    /// Client/source IP address (IIS W3C logs)
225    #[serde(default, skip_serializing_if = "Option::is_none")]
226    pub client_ip: Option<String>,
227    /// Server/listener IP address (IIS W3C logs)
228    #[serde(default, skip_serializing_if = "Option::is_none")]
229    pub server_ip: Option<String>,
230    /// HTTP user agent (IIS W3C logs)
231    #[serde(default, skip_serializing_if = "Option::is_none")]
232    pub user_agent: Option<String>,
233    /// Server/listener port (IIS W3C logs)
234    #[serde(default, skip_serializing_if = "Option::is_none")]
235    pub server_port: Option<u16>,
236    /// Authenticated username (IIS W3C logs)
237    #[serde(default, skip_serializing_if = "Option::is_none")]
238    pub username: Option<String>,
239    /// Win32 status code (IIS W3C logs)
240    #[serde(default, skip_serializing_if = "Option::is_none")]
241    pub win32_status: Option<u32>,
242    /// DNS query name, decoded from wire-format (DNS logs)
243    #[serde(default, skip_serializing_if = "Option::is_none")]
244    pub query_name: Option<String>,
245    /// DNS query type name: A, AAAA, SRV, etc. (DNS logs)
246    #[serde(default, skip_serializing_if = "Option::is_none")]
247    pub query_type: Option<String>,
248    /// DNS response code: NOERROR, NXDOMAIN, etc. (DNS logs)
249    #[serde(default, skip_serializing_if = "Option::is_none")]
250    pub response_code: Option<String>,
251    /// Packet direction: Snd or Rcv (DNS debug log)
252    #[serde(default, skip_serializing_if = "Option::is_none")]
253    pub dns_direction: Option<String>,
254    /// Transport protocol: UDP or TCP (DNS debug log)
255    #[serde(default, skip_serializing_if = "Option::is_none")]
256    pub dns_protocol: Option<String>,
257    /// Remote IP address, optionally with port (DNS logs)
258    #[serde(default, skip_serializing_if = "Option::is_none")]
259    pub source_ip: Option<String>,
260    /// DNS header flags as hex string (DNS debug log)
261    #[serde(default, skip_serializing_if = "Option::is_none")]
262    pub dns_flags: Option<String>,
263    /// DNS event ID for EVTX-sourced entries (DNS audit)
264    #[serde(default, skip_serializing_if = "Option::is_none")]
265    pub dns_event_id: Option<u32>,
266    /// DNS zone name (DNS audit)
267    #[serde(default, skip_serializing_if = "Option::is_none")]
268    pub zone_name: Option<String>,
269    // CmtLog extended fields
270    /// Entry role — distinguishes sections, iterations, and headers from normal log lines
271    #[serde(skip_serializing_if = "Option::is_none")]
272    pub entry_kind: Option<EntryKind>,
273    /// WhatIf/simulation flag (CmtLog)
274    #[serde(skip_serializing_if = "Option::is_none")]
275    pub whatif: Option<bool>,
276    /// Section label for Section entries (CmtLog)
277    #[serde(skip_serializing_if = "Option::is_none")]
278    pub section_name: Option<String>,
279    /// Optional color hint associated with a section (CmtLog)
280    #[serde(skip_serializing_if = "Option::is_none")]
281    pub section_color: Option<String>,
282    /// Iteration identifier for Iteration entries (CmtLog)
283    #[serde(skip_serializing_if = "Option::is_none")]
284    pub iteration: Option<String>,
285    /// Arbitrary tag list attached to a log entry (CmtLog)
286    #[serde(skip_serializing_if = "Option::is_none")]
287    pub tags: Option<Vec<String>>,
288}
289
290/// Result of parsing a complete log file.
291#[derive(Debug, Clone, Serialize, Deserialize)]
292#[serde(rename_all = "camelCase")]
293pub struct ParseResult {
294    pub entries: Vec<LogEntry>,
295    pub format_detected: LogFormat,
296    pub parser_selection: ParserSelectionInfo,
297    pub total_lines: u32,
298    pub parse_errors: u32,
299    pub file_path: String,
300    pub file_size: u64,
301    /// Byte offset where parsing ended — used as the starting point for tailing
302    pub byte_offset: u64,
303}
304
305/// Per-file parse metadata for an aggregated folder open.
306#[derive(Debug, Clone, Serialize, Deserialize)]
307#[serde(rename_all = "camelCase")]
308pub struct AggregateParsedFileResult {
309    pub file_path: String,
310    pub total_lines: u32,
311    pub parse_errors: u32,
312    pub file_size: u64,
313    pub byte_offset: u64,
314}
315
316/// Result of parsing every file in a folder into one combined view.
317#[derive(Debug, Clone, Serialize, Deserialize)]
318#[serde(rename_all = "camelCase")]
319pub struct AggregateParseResult {
320    pub entries: Vec<LogEntry>,
321    pub total_lines: u32,
322    pub parse_errors: u32,
323    pub folder_path: String,
324    pub files: Vec<AggregateParsedFileResult>,
325}