freeswitch-log-parser 0.12.0

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
//! Behavioral tests for entry rendering and filtering.

use freeswitch_log_parser::{AttachedLines, Block, LogEntry, LogLevel, MessageKind};

use super::color::{uuid_truecolor, BRIGHT_GREEN, RED, RESET};
use super::*;

fn entry(uuid: &str, message: &str, attached: &[&str]) -> LogEntry {
    let mut a = AttachedLines::new();
    for l in attached {
        a.push(l).expect("fits");
    }
    LogEntry {
        uuid: (!uuid.is_empty()).then(|| uuid.to_string()),
        attached: a,
        ..LogEntry::synthetic(message)
    }
}

const PEER: &str = "11111111-2222-3333-4444-555555555555";

fn printer(color: ColorMode, show_blocks: bool) -> EntryPrinter {
    EntryPrinter {
        color,
        show_blocks,
        show_session: false,
        show_filename: false,
        show_line_numbers: false,
    }
}

fn render(printer: &EntryPrinter, entry: &LogEntry) -> String {
    let mut out: Vec<u8> = Vec::new();
    printer.print_entry(&mut out, entry, None, None).unwrap();
    String::from_utf8(out).unwrap()
}

#[test]
fn attached_lines_inline_under_blocks() {
    let e = entry(
        "u",
        "Dialplan: parsing",
        &["Regex (PASS) x =~ /y/", "Action set"],
    );
    let out = render(&printer(ColorMode::Never, true), &e);
    assert!(out.contains("Regex (PASS) x =~ /y/"), "{out}");
    assert!(out.contains("Action set"), "{out}");
    assert!(!out.contains("attached lines"), "{out}");
}

const CHAN: &str = "sofia/internal/1262@pbx.example.test:5062";

#[test]
fn the_channel_heads_the_block_and_every_body_line_is_data() {
    let uuid = "9865d278-537b-4d4a-af91-f836729f78f2";
    let e = entry(
        uuid,
        &format!("Dialplan: {CHAN} parsing [default->unloop] continue=false"),
        &[format!("{uuid} Dialplan: {CHAN} Regex (PASS) [unloop] break=on-false").as_str()],
    );
    let out = render(&printer(ColorMode::Never, true), &e);
    let lines: Vec<&str> = out.lines().collect();

    assert!(lines[0].ends_with(&format!("Dialplan: {CHAN}")), "{out}");
    assert_eq!(lines[1].trim(), "parsing [default->unloop] continue=false");
    assert_eq!(lines[2].trim(), "Regex (PASS) [unloop] break=on-false");
}

#[test]
fn a_dialplan_line_with_no_body_keeps_its_data() {
    // Splitting a lone line would put its data on a continuation row of its own.
    let e = entry(
        "u",
        &format!("Dialplan: {CHAN} Absolute Condition [global]"),
        &[],
    );
    let out = render(&printer(ColorMode::Never, true), &e);
    assert!(
        out.trim_end().ends_with("Absolute Condition [global]"),
        "{out}"
    );
}

#[test]
fn a_foreign_uuid_in_a_body_line_survives() {
    // Only the entry's own UUID is redundant; any other one is a real value.
    let e = entry(
        "u",
        &format!("Dialplan: {CHAN} parsing [a->b] continue=true"),
        &[format!("u Dialplan: {CHAN} Regex (FAIL) ${{hdr}}({PEER}) =~ /^$/").as_str()],
    );
    let out = render(&printer(ColorMode::Never, true), &e);
    assert!(out.contains(PEER), "{out}");
    assert!(!out.contains(&format!("Dialplan: {CHAN} Regex")), "{out}");
}

#[test]
fn a_non_dialplan_continuation_only_loses_its_uuid() {
    let e = entry(
        "u",
        "msg",
        &["u EXECUTE [depth=0] sofia/internal/1001 bridge(sofia/gateway/gw/5551234)"],
    );
    let out = render(&printer(ColorMode::Never, false), &e);
    assert!(
        out.contains("  EXECUTE [depth=0] sofia/internal/1001 bridge(sofia/gateway/gw/5551234)"),
        "{out}"
    );
}

#[test]
fn attached_lines_collapse_without_blocks() {
    let e = entry("u", "Dialplan: parsing", &["one", "two"]);
    let out = render(&printer(ColorMode::Never, false), &e);
    assert!(out.contains("(2 attached lines)"), "{out}");
}

#[test]
fn lone_attached_line_always_inline() {
    let e = entry("u", "msg", &["the only continuation"]);
    let out = render(&printer(ColorMode::Never, false), &e);
    assert!(out.contains("the only continuation"), "{out}");
}

fn channel_data_entry() -> LogEntry {
    let mut e = entry(
        "u",
        "CHANNEL_DATA:",
        &["Channel-Name: [x]", "variable_a: [b]"],
    );
    e.block = Some(Block::ChannelData {
        fields: vec![("Channel-Name".into(), "x".into())],
        variables: vec![("variable_a".into(), "b".into())],
    });
    e
}

#[test]
fn an_expanded_block_does_not_also_count_its_raw_lines() {
    let out = render(&printer(ColorMode::Never, true), &channel_data_entry());
    assert!(out.contains("field  Channel-Name: x"), "{out}");
    assert!(!out.contains("attached lines"), "{out}");
}

#[test]
fn without_blocks_the_count_is_the_only_signal() {
    let out = render(&printer(ColorMode::Never, false), &channel_data_entry());
    assert!(out.contains("(2 attached lines)"), "{out}");
}

#[test]
fn a_marker_prints_as_a_rule_not_empty_columns() {
    let mut e = entry("", "freeswitch.log.1.xz", &[]);
    e.message_kind = MessageKind::FileChange;
    let out = render(&printer(ColorMode::Never, true), &e);
    assert_eq!(out, "── freeswitch.log.1.xz\n");
}

#[test]
fn embedded_uuid_gets_its_own_color() {
    let e = entry("aaaa", &format!("Peer UUID: {PEER}"), &[]);
    let out = render(&printer(ColorMode::Always, false), &e);
    let (r, g, b) = uuid_truecolor(PEER);
    assert!(
        out.contains(&format!("\x1b[38;2;{r};{g};{b}m{PEER}")),
        "{out}"
    );
}

#[test]
fn embedded_uuid_left_alone_without_color() {
    let e = entry("aaaa", &format!("Peer UUID: {PEER}"), &[]);
    let out = render(&printer(ColorMode::Never, false), &e);
    assert!(out.contains(&format!("Peer UUID: {PEER}")), "{out}");
    assert!(!out.contains("\x1b["), "{out}");
}

#[test]
fn pass_and_fail_are_colored_in_attached_lines() {
    let e = entry(
        "u",
        "Dialplan: parsing",
        &["Regex (PASS) a", "Regex (FAIL) b"],
    );
    let out = render(&printer(ColorMode::Always, true), &e);
    assert!(
        out.contains(&format!("{BRIGHT_GREEN}(PASS){RESET}")),
        "{out}"
    );
    assert!(out.contains(&format!("{RED}(FAIL){RESET}")), "{out}");
}

#[test]
fn long_variable_values_are_not_truncated() {
    let long = "x".repeat(500);
    let mut e = entry("u", "CHANNEL_DATA:", &[]);
    e.block = Some(Block::ChannelData {
        fields: Vec::new(),
        variables: vec![("variable_sip_multipart".to_string(), long.clone())],
    });
    let out = render(&printer(ColorMode::Never, true), &e);
    assert!(out.contains(&long), "{out}");
    assert!(!out.contains("..."), "{out}");
}

pub fn filter(p: FilterParams) -> FilterConfig {
    FilterConfig::new(FilterParams {
        uuid_strict: true,
        ..p
    })
    .unwrap()
}

#[test]
fn uuid_or_matches_any_needle() {
    let f = filter(FilterParams {
        uuid: vec!["aaaa".into(), "bbbb".into()],
        ..Default::default()
    });
    assert!(f.matches(&entry("xx-bbbb-yy", "msg", &[])));
    assert!(f.matches(&entry("aaaa-0000", "msg", &[])));
    assert!(!f.matches(&entry("cccc-0000", "msg", &[])));
}

#[test]
fn uuid_match_is_case_insensitive() {
    let f = filter(FilterParams {
        uuid: vec!["AAAABBBB".into()],
        ..Default::default()
    });
    assert!(f.matches(&entry("aaaabbbb-2222-3333-4444-555555555555", "msg", &[])));
}

#[test]
fn uuid_strict_ignores_message_body() {
    let mut f = filter(FilterParams {
        uuid: vec!["dead".into()],
        ..Default::default()
    });
    // strict: only the uuid field counts, not the message text
    assert!(!f.matches(&entry("0000", "peer dead leg", &[])));
    f.uuid_strict = false;
    assert!(f.matches(&entry("0000", "peer dead leg", &[])));
}

#[test]
fn fgrep_into_blocks_only_with_match_blocks() {
    let mut f = filter(FilterParams {
        fgrep: Some("m=audio".into()),
        ..Default::default()
    });
    let e = entry("u", "Remote SDP:", &["v=0", "m=audio 5004 RTP/AVP 0"]);
    assert!(!f.matches(&e));
    f.match_blocks = true;
    assert!(f.matches(&e));
}

#[test]
fn fgrep_is_case_insensitive() {
    let f = filter(FilterParams {
        fgrep: Some("RECEIVING INVITE".into()),
        ..Default::default()
    });
    assert!(f.matches(&entry("u", "receiving invite from 192.0.2.1", &[])));
}

#[test]
fn category_matches_any_of_several() {
    let f = filter(FilterParams {
        category: vec!["execute".into(), "dialplan".into()],
        ..Default::default()
    });
    let mut e = entry("u", "msg", &[]);
    e.message_kind = MessageKind::Dialplan {
        channel: "sofia/internal/1001".to_string(),
        detail: "parsing".to_string(),
    };
    assert!(f.matches(&e));
    e.message_kind = MessageKind::General;
    assert!(!f.matches(&e));
}

#[test]
fn for_discovery_clears_category_and_loosens() {
    let f = filter(FilterParams {
        category: vec!["execute".into()],
        uuid: vec!["seed".into()],
        ..Default::default()
    });
    let d = f.for_discovery();
    assert!(d.category.is_empty());
    assert!(!d.uuid_strict);
    assert!(d.match_blocks);
    // seed found in message body survives discovery despite category mismatch
    assert!(d.matches(&entry("0000", "found seed here", &[])));
}

fn codec_entry(names: &[&str], matched: &[&str]) -> LogEntry {
    let offer = |name: &str| {
        freeswitch_log_parser::CodecOffer::parse(
            freeswitch_log_parser::CodecMedia::Audio,
            &format!("{name}:0:8000:20:64000:1"),
        )
        .expect("token parses")
    };
    let mut e = entry("u", "Audio Codec Compare", &[]);
    e.block = Some(Block::CodecNegotiation {
        media: freeswitch_log_parser::CodecMedia::Audio,
        comparisons: names.iter().map(|n| (offer(n), offer("PCMU"))).collect(),
        matched: matched.iter().map(|n| offer(n)).collect(),
        near_matched: Vec::new(),
    });
    e
}

#[test]
fn codec_filter_matches_offers_and_matches() {
    let f = filter(FilterParams {
        codec: vec!["opus".into()],
        ..Default::default()
    });
    assert!(f.matches(&codec_entry(&["opus"], &[])), "a remote offer");
    assert!(f.matches(&codec_entry(&["G722"], &["opus"])), "the winner");
    assert!(!f.matches(&codec_entry(&["G722"], &["G722"])));
}

#[test]
fn codec_filter_is_case_insensitive() {
    let f = filter(FilterParams {
        codec: vec!["OPUS".into()],
        ..Default::default()
    });
    assert!(f.matches(&codec_entry(&["opus"], &[])));
}

/// A live audio stream, a held video one, and a stream carrying no payload type.
#[cfg(feature = "sdp")]
fn sdp_entry() -> LogEntry {
    let body = [
        "v=0",
        "o=- 1 1 IN IP4 192.0.2.10",
        "s=-",
        "c=IN IP4 192.0.2.10",
        "t=0 0",
        "m=audio 30000 RTP/AVP 0 101",
        "a=rtpmap:0 PCMU/8000",
        "a=rtpmap:101 telephone-event/8000",
        "a=fmtp:101 0-16",
        "m=video 0 RTP/AVP 99",
        "a=rtpmap:99 H264/90000",
        "m=application 5000 UDP/DTLS/SCTP webrtc-datachannel",
    ];
    let mut e = entry("u", "Remote SDP:", &[]);
    e.block = Some(Block::Sdp {
        direction: freeswitch_log_parser::SdpDirection::Remote,
        body: body.iter().map(|l| l.to_string()).collect(),
    });
    e
}

#[cfg(feature = "sdp")]
#[test]
fn sdp_summary_names_the_streams_that_carry_no_codec() {
    let out = render(&printer(ColorMode::Never, true), &sdp_entry());
    assert!(
        out.contains("PCMU/8000, 101 telephone-event/8000 0-16"),
        "{out}"
    );
    assert!(
        out.contains("held m=video port 0, skipped m=application/UDP/DTLS/SCTP"),
        "{out}"
    );
}

#[cfg(feature = "sdp")]
#[test]
fn codec_filter_matches_an_offer_on_a_held_stream() {
    let f = filter(FilterParams {
        codec: vec!["h264".into()],
        ..Default::default()
    });
    assert!(f.matches(&sdp_entry()));
}

#[test]
fn codec_filter_ignores_entries_without_media_blocks() {
    let f = filter(FilterParams {
        codec: vec!["opus".into()],
        ..Default::default()
    });
    assert!(!f.matches(&entry("u", "opus appears only in the text", &[])));
}

const CALL: &str = "aaaaaaaa-1111-1111-1111-111111111111";

fn hidden(f: &FilterConfig, e: &LogEntry) -> Option<Hidden> {
    match f.verdict(e) {
        Verdict::Hidden(h) => Some(h),
        _ => None,
    }
}

fn grep(pattern: &str) -> Option<regex::Regex> {
    Some(regex::Regex::new(pattern).expect("test pattern compiles"))
}

#[test]
fn a_pattern_in_the_uuid_column_is_counted() {
    let f = filter(FilterParams {
        grep: grep(CALL),
        ..Default::default()
    });
    assert_eq!(
        hidden(&f, &entry(CALL, "Activating RTCP", &[])),
        Some(Hidden::PatternInUuid)
    );
}

#[test]
fn the_uuid_column_probe_ignores_case() {
    let f = filter(FilterParams {
        grep: grep(&CALL.to_uppercase()),
        ..Default::default()
    });
    assert_eq!(
        hidden(&f, &entry(CALL, "Activating RTCP", &[])),
        Some(Hidden::PatternInUuid)
    );
}

#[test]
fn a_pattern_in_an_attached_line_is_counted_until_match_blocks() {
    let mut f = filter(FilterParams {
        fgrep: Some("m=audio".into()),
        ..Default::default()
    });
    let e = entry("u", "Remote SDP:", &["v=0", "m=audio 5004 RTP/AVP 0"]);
    assert_eq!(hidden(&f, &e), Some(Hidden::PatternInBlocks));
    f.match_blocks = true;
    assert_eq!(hidden(&f, &e), None, "the flag is already in effect");
}

#[test]
fn the_uuid_column_wins_over_the_attached_bucket() {
    let f = filter(FilterParams {
        fgrep: Some(CALL.into()),
        ..Default::default()
    });
    // An attached line naming the channel's own UUID is the common shape;
    // overlapping buckets would count it twice.
    let e = entry(CALL, "CHANNEL_DATA:", &[&format!("Unique-ID: [{CALL}]")]);
    assert_eq!(hidden(&f, &e), Some(Hidden::PatternInUuid));
}

#[test]
fn a_rejection_on_another_predicate_advertises_nothing() {
    let f = filter(FilterParams {
        grep: grep(CALL),
        min_level: Some(LogLevel::Err),
        ..Default::default()
    });
    let mut e = entry(CALL, "Activating RTCP", &[]);
    e.level = Some(LogLevel::Debug);
    assert_eq!(hidden(&f, &e), None);
}

#[test]
fn conjunct_patterns_widen_together_or_not_at_all() {
    let f = filter(FilterParams {
        fgrep: Some("hangup".into()),
        grep: grep(CALL),
        ..Default::default()
    });
    // Only --grep reaches the UUID column, so no single scope admits this.
    assert_eq!(hidden(&f, &entry(CALL, "Activating RTCP", &[])), None);
}

#[test]
fn a_uuid_named_in_the_body_is_counted() {
    let f = filter(FilterParams {
        uuid: vec![CALL.into()],
        ..Default::default()
    });
    assert_eq!(
        hidden(&f, &entry("bbbb", &format!("Bridging to {CALL}"), &[])),
        Some(Hidden::UuidInBody)
    );
    assert_eq!(hidden(&f, &entry("bbbb", "unrelated", &[])), None);
}

#[test]
fn a_uuid_inside_a_pattern_names_the_command() {
    let f = filter(FilterParams {
        grep: grep(&format!("Hangup on {CALL}")),
        ..Default::default()
    });
    assert_eq!(f.suggested_uuid(), Some(CALL));
}

#[test]
fn no_command_for_a_uuid_already_being_filtered_on() {
    let f = filter(FilterParams {
        uuid: vec![CALL.to_uppercase()],
        fgrep: Some(CALL.into()),
        ..Default::default()
    });
    assert_eq!(f.suggested_uuid(), None);
    let plain = filter(FilterParams {
        fgrep: Some("receiving invite".into()),
        ..Default::default()
    });
    assert_eq!(plain.suggested_uuid(), None, "no uuid in the pattern");
}