freeswitch-log-parser 0.4.2

Parser for FreeSWITCH log files — handles compressed .xz files, multi-line dumps, truncated buffers, and stateful UUID/timestamp tracking
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
use crate::level::LogLevel;

use std::fmt;

/// Length of a session UUID in canonical 8-4-4-4-12 hex form.
pub(crate) const UUID_LEN: usize = 36;

/// Length of a UUID followed by its trailing space — the prefix
/// `mod_logfile` prepends to every line when `log_uuid=true`.
pub(crate) const UUID_PREFIX_LEN: usize = UUID_LEN + 1;

/// Classification of a single log line's structural format.
///
/// FreeSWITCH's `switch_log_printf` emits five distinct line shapes depending
/// on whether a session UUID is active, whether the line has a timestamp, and
/// whether a buffer collision truncated the output. See `docs/design-rationale.md`
/// for the full anatomy.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LineKind {
    /// Format A — UUID, timestamp, idle%, level, source, and message.
    Full,
    /// Format B — same as `Full` but without a UUID prefix (system/global events).
    System,
    /// Format C — UUID and message only, no timestamp or level.
    UuidContinuation,
    /// Format D — raw text with no UUID or timestamp; inherits context from the previous entry.
    BareContinuation,
    /// Format E — buffer collision produced a garbage prefix before the UUID.
    Truncated,
    /// Blank or whitespace-only line.
    Empty,
}

impl fmt::Display for LineKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            LineKind::Full => f.pad("full"),
            LineKind::System => f.pad("system"),
            LineKind::UuidContinuation => f.pad("uuid-cont"),
            LineKind::BareContinuation => f.pad("bare-cont"),
            LineKind::Truncated => f.pad("truncated"),
            LineKind::Empty => f.pad("empty"),
        }
    }
}

/// Zero-copy result of parsing a single log line.
///
/// Fields are `None` when the line's format doesn't include them (e.g. a
/// `BareContinuation` has no `uuid`, `timestamp`, `level`, or `source`).
/// The `message` field always contains the remaining text.
#[derive(Debug, PartialEq, Eq)]
pub struct RawLine<'a> {
    /// Session UUID, present for `Full`, `UuidContinuation`, and `Truncated` lines.
    pub uuid: Option<&'a str>,
    /// Microsecond-precision timestamp, present only for `Full` and `System` lines.
    pub timestamp: Option<&'a str>,
    /// Core scheduler idle percentage (e.g. `"95.97%"`), a system health indicator.
    pub idle_pct: Option<&'a str>,
    /// Log severity, present only for `Full` and `System` lines.
    pub level: Option<LogLevel>,
    /// Source file and line (e.g. `"sofia.c:7624"`), present only for `Full` and `System` lines.
    pub source: Option<&'a str>,
    /// The message text after all structured fields have been consumed.
    pub message: &'a str,
    /// Which of the five line formats this line matched.
    pub kind: LineKind,
}

pub(crate) fn is_uuid_at(bytes: &[u8], offset: usize) -> bool {
    if bytes.len() < offset + UUID_PREFIX_LEN {
        return false;
    }
    if bytes[offset + UUID_LEN] != b' ' {
        return false;
    }
    for (i, &b) in bytes[offset..offset + UUID_LEN].iter().enumerate() {
        match i {
            8 | 13 | 18 | 23 => {
                if b != b'-' {
                    return false;
                }
            }
            _ => {
                if !b.is_ascii_hexdigit() {
                    return false;
                }
            }
        }
    }
    true
}

fn find_uuid_in(bytes: &[u8]) -> Option<usize> {
    if bytes.len() < UUID_PREFIX_LEN {
        return None;
    }
    let max_start = (bytes.len() - UUID_PREFIX_LEN).min(50);
    (1..=max_start).find(|&start| is_uuid_at(bytes, start))
}

pub(crate) fn is_date_at(bytes: &[u8], offset: usize) -> bool {
    if bytes.len() < offset + 5 {
        return false;
    }
    bytes[offset..offset + 4].iter().all(u8::is_ascii_digit) && bytes[offset + 4] == b'-'
}

/// Check for a full FreeSWITCH log header at `offset`:
/// `YYYY-MM-DD HH:MM:SS.UUUUUU D+.D+% [`
///
/// Used by Layer 2 to detect same-line collisions where multiple log entries
/// were concatenated without a newline (thread contention on file write).
pub(crate) fn is_log_header_at(bytes: &[u8], offset: usize) -> bool {
    // Minimum: 27-byte timestamp + space + "0% [" = 31 bytes
    if bytes.len() < offset + 31 {
        return false;
    }
    // YYYY-MM-DD HH:MM:SS.UUUUUU (26 bytes + space)
    if !(bytes[offset..offset + 4].iter().all(u8::is_ascii_digit)
        && bytes[offset + 4] == b'-'
        && bytes[offset + 5..offset + 7].iter().all(u8::is_ascii_digit)
        && bytes[offset + 7] == b'-'
        && bytes[offset + 8..offset + 10]
            .iter()
            .all(u8::is_ascii_digit)
        && bytes[offset + 10] == b' '
        && bytes[offset + 11..offset + 13]
            .iter()
            .all(u8::is_ascii_digit)
        && bytes[offset + 13] == b':'
        && bytes[offset + 14..offset + 16]
            .iter()
            .all(u8::is_ascii_digit)
        && bytes[offset + 16] == b':'
        && bytes[offset + 17..offset + 19]
            .iter()
            .all(u8::is_ascii_digit)
        && bytes[offset + 19] == b'.'
        && bytes[offset + 20..offset + 26]
            .iter()
            .all(u8::is_ascii_digit)
        && bytes[offset + 26] == b' ')
    {
        return false;
    }
    // Idle percentage: starts with digit, has % within 6 bytes, then " ["
    let rest = &bytes[offset + 27..];
    if !rest[0].is_ascii_digit() {
        return false;
    }
    let Some(pct_pos) = rest[..rest.len().min(7)].iter().position(|&b| b == b'%') else {
        return false;
    };
    rest.len() > pct_pos + 2 && rest[pct_pos + 1] == b' ' && rest[pct_pos + 2] == b'['
}

/// Try to parse idle percentage from the start of `rest`.
///
/// The idle percentage appears immediately after the timestamp, starts with a
/// digit, contains only digits and dots, and the `%` falls within the first 7
/// bytes (max value: `"100.00%"`). When absent (some FS versions/configurations
/// omit it), `rest` starts with `[LEVEL]` instead.
///
/// Returns `(Some(idle_pct), remaining)` on success, or `(None, rest)` unchanged.
fn parse_idle_pct(rest: &str) -> (Option<&str>, &str) {
    let bytes = rest.as_bytes();
    if bytes.is_empty() || !bytes[0].is_ascii_digit() {
        return (None, rest);
    }
    let search_len = rest.len().min(7);
    let pct_pos = match bytes[..search_len].iter().position(|&b| b == b'%') {
        Some(p) => p,
        None => return (None, rest),
    };
    if !bytes[..pct_pos]
        .iter()
        .all(|&b| b.is_ascii_digit() || b == b'.')
    {
        return (None, rest);
    }
    let idle_pct = &rest[0..=pct_pos];
    let after = if rest.len() > pct_pos + 2 {
        &rest[pct_pos + 2..]
    } else {
        ""
    };
    (Some(idle_pct), after)
}

fn parse_timestamped_fields(
    s: &str,
) -> (
    Option<&str>,
    Option<&str>,
    Option<LogLevel>,
    Option<&str>,
    &str,
) {
    if s.len() < 27 {
        return (None, None, None, None, s);
    }
    let timestamp = &s[0..26];
    let rest = &s[27..];

    let (idle_pct, rest) = parse_idle_pct(rest);

    let bracket_end = match rest.find(']') {
        Some(p) => p,
        None => return (Some(timestamp), idle_pct, None, None, rest),
    };
    let level = LogLevel::from_bracketed(&rest[0..=bracket_end]);

    if rest.len() < bracket_end + 3 {
        return (Some(timestamp), idle_pct, level, None, "");
    }
    let rest = &rest[bracket_end + 2..];

    let source_end = rest.find(' ').unwrap_or(rest.len());
    let source = &rest[0..source_end];
    let message = if source_end < rest.len() {
        &rest[source_end + 1..]
    } else {
        ""
    };

    (Some(timestamp), idle_pct, level, Some(source), message)
}

/// Layer 1 entry point: classify a single line and extract its fields.
///
/// Pure function — no state, no allocation. All returned string slices borrow
/// from the input. Use [`classify_message`](crate::classify_message) on the
/// `message` field for semantic classification.
pub fn parse_line(line: &str) -> RawLine<'_> {
    if line.trim().is_empty() {
        return RawLine {
            uuid: None,
            timestamp: None,
            idle_pct: None,
            level: None,
            source: None,
            message: line,
            kind: LineKind::Empty,
        };
    }

    let bytes = line.as_bytes();

    if is_uuid_at(bytes, 0) {
        let uuid = &line[0..UUID_LEN];
        let after_uuid = &line[UUID_PREFIX_LEN..];

        if is_date_at(bytes, UUID_PREFIX_LEN) {
            let (timestamp, idle_pct, level, source, message) =
                parse_timestamped_fields(after_uuid);
            return RawLine {
                uuid: Some(uuid),
                timestamp,
                idle_pct,
                level,
                source,
                message,
                kind: LineKind::Full,
            };
        }

        return RawLine {
            uuid: Some(uuid),
            timestamp: None,
            idle_pct: None,
            level: None,
            source: None,
            message: after_uuid,
            kind: LineKind::UuidContinuation,
        };
    }

    if is_date_at(bytes, 0) {
        let (timestamp, idle_pct, level, source, message) = parse_timestamped_fields(line);
        let (uuid, message) = if is_uuid_at(message.as_bytes(), 0) {
            (Some(&message[0..UUID_LEN]), &message[UUID_PREFIX_LEN..])
        } else {
            (None, message)
        };
        return RawLine {
            uuid,
            timestamp,
            idle_pct,
            level,
            source,
            message,
            kind: LineKind::System,
        };
    }

    if let Some(uuid_start) = find_uuid_in(bytes) {
        let uuid = &line[uuid_start..uuid_start + UUID_LEN];
        let message = if line.len() > uuid_start + UUID_PREFIX_LEN {
            &line[uuid_start + UUID_PREFIX_LEN..]
        } else {
            ""
        };
        return RawLine {
            uuid: Some(uuid),
            timestamp: None,
            idle_pct: None,
            level: None,
            source: None,
            message,
            kind: LineKind::Truncated,
        };
    }

    RawLine {
        uuid: None,
        timestamp: None,
        idle_pct: None,
        level: None,
        source: None,
        message: line,
        kind: LineKind::BareContinuation,
    }
}

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

    const UUID1: &str = "a1b2c3d4-e5f6-7890-abcd-ef1234567890";

    // --- Format A (Full) ---

    #[test]
    fn full_line_all_fields() {
        let line = format!(
            "{UUID1} 2025-01-15 10:30:45.123456 95.97% [DEBUG] sofia.c:100 Test message here"
        );
        let parsed = parse_line(&line);
        assert_eq!(parsed.kind, LineKind::Full);
        assert_eq!(parsed.uuid, Some(UUID1));
        assert_eq!(parsed.timestamp, Some("2025-01-15 10:30:45.123456"));
        assert_eq!(parsed.idle_pct, Some("95.97%"));
        assert_eq!(parsed.level, Some(LogLevel::Debug));
        assert_eq!(parsed.source, Some("sofia.c:100"));
        assert_eq!(parsed.message, "Test message here");
    }

    #[test]
    fn full_line_each_level() {
        for (name, expected) in [
            ("DEBUG", LogLevel::Debug),
            ("INFO", LogLevel::Info),
            ("NOTICE", LogLevel::Notice),
            ("WARNING", LogLevel::Warning),
            ("ERR", LogLevel::Err),
            ("CRIT", LogLevel::Crit),
            ("ALERT", LogLevel::Alert),
            ("CONSOLE", LogLevel::Console),
        ] {
            let line =
                format!("{UUID1} 2025-01-15 10:30:45.123456 95.97% [{name}] sofia.c:100 Test");
            let parsed = parse_line(&line);
            assert_eq!(parsed.kind, LineKind::Full);
            assert_eq!(parsed.level, Some(expected), "failed for [{name}]");
        }
    }

    #[test]
    fn full_line_high_idle() {
        let line =
            format!("{UUID1} 2025-01-15 10:30:45.123456 99.99% [DEBUG] sofia.c:100 High idle");
        let parsed = parse_line(&line);
        assert_eq!(parsed.idle_pct, Some("99.99%"));
    }

    #[test]
    fn full_line_low_idle() {
        let line = format!("{UUID1} 2025-01-15 10:30:45.123456 0.00% [DEBUG] sofia.c:100 Low idle");
        let parsed = parse_line(&line);
        assert_eq!(parsed.idle_pct, Some("0.00%"));
    }

    #[test]
    fn full_line_long_message() {
        let line = format!(
            "{UUID1} 2025-01-15 10:30:45.123456 95.97% [DEBUG] sofia.c:100 Channel [sofia/internal] key=val:123 (test) {{braces}}"
        );
        let parsed = parse_line(&line);
        assert_eq!(
            parsed.message,
            "Channel [sofia/internal] key=val:123 (test) {braces}"
        );
    }

    // --- Format B (System) ---

    #[test]
    fn system_line_no_uuid() {
        let line =
            "2025-01-15 10:30:45.123456 95.97% [INFO] mod_event_socket.c:1772 Event Socket command";
        let parsed = parse_line(line);
        assert_eq!(parsed.kind, LineKind::System);
        assert_eq!(parsed.uuid, None);
        assert_eq!(parsed.timestamp, Some("2025-01-15 10:30:45.123456"));
        assert_eq!(parsed.idle_pct, Some("95.97%"));
        assert_eq!(parsed.level, Some(LogLevel::Info));
        assert_eq!(parsed.source, Some("mod_event_socket.c:1772"));
        assert_eq!(parsed.message, "Event Socket command");
    }

    #[test]
    fn system_line_with_embedded_uuid() {
        let line = format!(
            "2025-01-15 10:30:45.123456 95.97% [DEBUG] switch_cpp.cpp:1466 {UUID1} DAA-LOG WaveManager PSAP 911 originate"
        );
        let parsed = parse_line(&line);
        assert_eq!(parsed.kind, LineKind::System);
        assert_eq!(parsed.uuid, Some(UUID1));
        assert_eq!(parsed.timestamp, Some("2025-01-15 10:30:45.123456"));
        assert_eq!(parsed.level, Some(LogLevel::Debug));
        assert_eq!(parsed.source, Some("switch_cpp.cpp:1466"));
        assert_eq!(parsed.message, "DAA-LOG WaveManager PSAP 911 originate");
    }

    #[test]
    fn system_line_with_embedded_uuid_empty_message() {
        let line = format!("2025-01-15 10:30:45.123456 95.97% [INFO] switch_cpp.cpp:1466 {UUID1} ");
        let parsed = parse_line(&line);
        assert_eq!(parsed.kind, LineKind::System);
        assert_eq!(parsed.uuid, Some(UUID1));
        assert_eq!(parsed.message, "");
    }

    #[test]
    fn system_line_without_embedded_uuid() {
        let line =
            "2025-01-15 10:30:45.123456 95.97% [INFO] mod_event_socket.c:1772 Event Socket command";
        let parsed = parse_line(line);
        assert_eq!(parsed.kind, LineKind::System);
        assert_eq!(parsed.uuid, None);
        assert_eq!(parsed.message, "Event Socket command");
    }

    #[test]
    fn system_line_event_socket() {
        let line = "2025-01-15 10:30:45.123456 95.97% [NOTICE] mod_logfile.c:217 New log started.";
        let parsed = parse_line(line);
        assert_eq!(parsed.kind, LineKind::System);
        assert_eq!(parsed.level, Some(LogLevel::Notice));
        assert_eq!(parsed.message, "New log started.");
    }

    // --- Format C (UuidContinuation) ---

    #[test]
    fn uuid_continuation_dialplan() {
        let line =
            format!("{UUID1} Dialplan: sofia/internal/+15550001234@192.0.2.1 parsing [public]");
        let parsed = parse_line(&line);
        assert_eq!(parsed.kind, LineKind::UuidContinuation);
        assert_eq!(parsed.uuid, Some(UUID1));
        assert_eq!(parsed.timestamp, None);
        assert_eq!(parsed.level, None);
        assert_eq!(
            parsed.message,
            "Dialplan: sofia/internal/+15550001234@192.0.2.1 parsing [public]"
        );
    }

    #[test]
    fn uuid_continuation_execute() {
        let line =
            format!("{UUID1} EXECUTE [depth=0] sofia/internal/+15550001234@192.0.2.1 set(foo=bar)");
        let parsed = parse_line(&line);
        assert_eq!(parsed.kind, LineKind::UuidContinuation);
        assert_eq!(parsed.uuid, Some(UUID1));
        assert_eq!(
            parsed.message,
            "EXECUTE [depth=0] sofia/internal/+15550001234@192.0.2.1 set(foo=bar)"
        );
    }

    #[test]
    fn uuid_continuation_channel_var() {
        let line = format!("{UUID1} Channel-State: [CS_EXECUTE]");
        let parsed = parse_line(&line);
        assert_eq!(parsed.kind, LineKind::UuidContinuation);
        assert_eq!(parsed.uuid, Some(UUID1));
        assert_eq!(parsed.message, "Channel-State: [CS_EXECUTE]");
    }

    #[test]
    fn uuid_continuation_variable() {
        let line = format!("{UUID1} variable_sip_call_id: [test123@192.0.2.1]");
        let parsed = parse_line(&line);
        assert_eq!(parsed.kind, LineKind::UuidContinuation);
        assert_eq!(parsed.uuid, Some(UUID1));
        assert_eq!(parsed.message, "variable_sip_call_id: [test123@192.0.2.1]");
    }

    #[test]
    fn uuid_continuation_blank() {
        let line = format!("{UUID1} ");
        let parsed = parse_line(&line);
        assert_eq!(parsed.kind, LineKind::UuidContinuation);
        assert_eq!(parsed.uuid, Some(UUID1));
        assert_eq!(parsed.message, "");
    }

    // --- Format D (BareContinuation) ---

    #[test]
    fn bare_variable() {
        let line = "variable_foo: [bar]";
        let parsed = parse_line(line);
        assert_eq!(parsed.kind, LineKind::BareContinuation);
        assert_eq!(parsed.uuid, None);
        assert_eq!(parsed.message, "variable_foo: [bar]");
    }

    #[test]
    fn bare_sdp_origin() {
        let line = "o=- 1234 5678 IN IP4 192.0.2.1";
        let parsed = parse_line(line);
        assert_eq!(parsed.kind, LineKind::BareContinuation);
        assert_eq!(parsed.message, line);
    }

    #[test]
    fn bare_sdp_media() {
        let line = "m=audio 47758 RTP/AVP 0 101";
        let parsed = parse_line(line);
        assert_eq!(parsed.kind, LineKind::BareContinuation);
        assert_eq!(parsed.message, line);
    }

    #[test]
    fn bare_sdp_attribute() {
        let line = "a=rtpmap:0 PCMU/8000";
        let parsed = parse_line(line);
        assert_eq!(parsed.kind, LineKind::BareContinuation);
        assert_eq!(parsed.message, line);
    }

    #[test]
    fn bare_closing_bracket() {
        let line = "]";
        let parsed = parse_line(line);
        assert_eq!(parsed.kind, LineKind::BareContinuation);
        assert_eq!(parsed.message, "]");
    }

    #[test]
    fn bare_empty_line() {
        let parsed = parse_line("");
        assert_eq!(parsed.kind, LineKind::Empty);
        assert_eq!(parsed.message, "");
    }

    // --- Format E (Truncated) ---

    #[test]
    fn truncated_varia_prefix() {
        let line = format!(
            "varia{UUID1} EXECUTE [depth=0] sofia/internal/+15550001234@192.0.2.1 set(x=y)"
        );
        let parsed = parse_line(&line);
        assert_eq!(parsed.kind, LineKind::Truncated);
        assert_eq!(parsed.uuid, Some(UUID1));
        assert_eq!(
            parsed.message,
            "EXECUTE [depth=0] sofia/internal/+15550001234@192.0.2.1 set(x=y)"
        );
    }

    #[test]
    fn truncated_variab_prefix() {
        let line = format!(
            "variab{UUID1} EXECUTE [depth=0] sofia/internal/+15550001234@192.0.2.1 set(x=y)"
        );
        let parsed = parse_line(&line);
        assert_eq!(parsed.kind, LineKind::Truncated);
        assert_eq!(parsed.uuid, Some(UUID1));
    }

    #[test]
    fn truncated_var_prefix() {
        let line =
            format!("var{UUID1} EXECUTE [depth=0] sofia/internal/+15550001234@192.0.2.1 set(x=y)");
        let parsed = parse_line(&line);
        assert_eq!(parsed.kind, LineKind::Truncated);
        assert_eq!(parsed.uuid, Some(UUID1));
    }

    #[test]
    fn truncated_variable_prefix() {
        let line = format!(
            "variable{UUID1} EXECUTE [depth=0] sofia/internal/+15550001234@192.0.2.1 set(x=y)"
        );
        let parsed = parse_line(&line);
        assert_eq!(parsed.kind, LineKind::Truncated);
        assert_eq!(parsed.uuid, Some(UUID1));
    }

    // --- No idle percentage (issue #1) ---

    #[test]
    fn full_line_no_idle_pct() {
        let line = format!(
            "{UUID1} 2025-01-15 10:30:45.123456 [NOTICE] switch_core_session.c:1744 Session 3178948 ended"
        );
        let parsed = parse_line(&line);
        assert_eq!(parsed.kind, LineKind::Full);
        assert_eq!(parsed.uuid, Some(UUID1));
        assert_eq!(parsed.timestamp, Some("2025-01-15 10:30:45.123456"));
        assert_eq!(parsed.idle_pct, None);
        assert_eq!(parsed.level, Some(LogLevel::Notice));
        assert_eq!(parsed.source, Some("switch_core_session.c:1744"));
        assert_eq!(parsed.message, "Session 3178948 ended");
    }

    #[test]
    fn full_line_no_idle_pct_url_encoded_percent() {
        let line = format!(
            "{UUID1} 2025-01-15 10:30:45.123456 [NOTICE] switch_core_session.c:1744 Session 3178948 (sofia/psap/gw%2Bsg1vofswb-inbound@198.51.100.5:5060) Ended"
        );
        let parsed = parse_line(&line);
        assert_eq!(parsed.kind, LineKind::Full);
        assert_eq!(parsed.uuid, Some(UUID1));
        assert_eq!(parsed.timestamp, Some("2025-01-15 10:30:45.123456"));
        assert_eq!(parsed.idle_pct, None);
        assert_eq!(parsed.level, Some(LogLevel::Notice));
        assert_eq!(parsed.source, Some("switch_core_session.c:1744"));
        assert_eq!(
            parsed.message,
            "Session 3178948 (sofia/psap/gw%2Bsg1vofswb-inbound@198.51.100.5:5060) Ended"
        );
    }

    #[test]
    fn system_line_no_idle_pct() {
        let line = "2025-01-15 10:30:45.123456 [INFO] mod_event_socket.c:1772 Event Socket command";
        let parsed = parse_line(line);
        assert_eq!(parsed.kind, LineKind::System);
        assert_eq!(parsed.uuid, None);
        assert_eq!(parsed.timestamp, Some("2025-01-15 10:30:45.123456"));
        assert_eq!(parsed.idle_pct, None);
        assert_eq!(parsed.level, Some(LogLevel::Info));
        assert_eq!(parsed.source, Some("mod_event_socket.c:1772"));
        assert_eq!(parsed.message, "Event Socket command");
    }

    #[test]
    fn full_line_no_idle_pct_hangup_url_encoded() {
        let line = format!(
            "{UUID1} 2025-01-15 10:30:45.123456 [NOTICE] sofia.c:1089 Hangup sofia/psap/gw%2Bgateway@198.51.100.5:5060 [CS_EXCHANGE_MEDIA] [CALL_AWARDED_DELIVERED]"
        );
        let parsed = parse_line(&line);
        assert_eq!(parsed.kind, LineKind::Full);
        assert_eq!(parsed.idle_pct, None);
        assert_eq!(parsed.level, Some(LogLevel::Notice));
        assert_eq!(parsed.source, Some("sofia.c:1089"));
        assert_eq!(
            parsed.message,
            "Hangup sofia/psap/gw%2Bgateway@198.51.100.5:5060 [CS_EXCHANGE_MEDIA] [CALL_AWARDED_DELIVERED]"
        );
    }

    // --- Edge cases ---

    #[test]
    fn not_uuid_36_chars() {
        let line = "this-is-not-a-valid-uuid-value-12345 rest of line";
        let parsed = parse_line(line);
        assert_eq!(parsed.kind, LineKind::BareContinuation);
        assert_eq!(parsed.message, line);
    }

    #[test]
    fn uuid_in_message_not_prefix() {
        let line =
            format!("This is some log message body with extra context then {UUID1} appears here");
        let parsed = parse_line(&line);
        assert_eq!(parsed.kind, LineKind::BareContinuation);
        assert_eq!(parsed.message, line.as_str());
    }

    #[test]
    fn whitespace_only_is_empty() {
        let parsed = parse_line("   \t  ");
        assert_eq!(parsed.kind, LineKind::Empty);
    }
}