freeswitch-log-parser 0.3.1

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
mod complete;
#[cfg(feature = "tui")]
mod config;
mod files;
#[cfg(feature = "tui")]
mod monitor;
mod output;

use std::io::{self, BufRead, IsTerminal, Write};
use std::path::{Path, PathBuf};
use std::process;

use clap::{CommandFactory, Parser, Subcommand, ValueEnum};

use freeswitch_log_parser::{
    LineKind, LogEntry, LogLevel, LogStream, MessageKind, SessionTracker, TrackedChain,
    UnclassifiedTracking,
};

use files::{
    discover_log_files, filter_files_by_date, format_size, lazy_log_reader, normalize_date_from,
    normalize_date_until, open_log_reader, open_tail_reader,
};
use output::{ColorMode, EntryPrinter, FilterConfig};

#[derive(Clone, Copy, ValueEnum)]
enum ColorWhen {
    Auto,
    Always,
    Never,
}

#[derive(Parser)]
#[command(name = "fslog", about = "FreeSWITCH log file query tool")]
struct Cli {
    /// Log directory
    #[arg(long, default_value = "/var/log/freeswitch", env = "FSLOG_DIR")]
    dir: PathBuf,

    /// Color output: auto, always, never
    #[arg(long, default_value = "auto", value_enum)]
    color: ColorWhen,

    /// Disable auto-pager
    #[arg(long)]
    no_pager: bool,

    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    /// List log files with dates and sizes
    List,

    /// Search/filter entries across multiple files
    Search(SearchArgs),

    /// Parse and display a single log file
    Read(ReadArgs),

    /// Follow the log file and display new entries in color
    Tail(TailArgs),

    /// Live TUI dashboard of active calls
    #[cfg(feature = "tui")]
    Monitor(monitor::MonitorArgs),

    /// Generate shell completion script
    Completions {
        /// Shell to generate completions for
        shell: clap_complete::aot::Shell,
    },
}

#[derive(clap::Args)]
struct FilterArgs {
    /// UUID substring filter (case-insensitive)
    #[arg(short, long, value_name = "UUID")]
    uuid: Option<String>,

    /// Minimum log level
    #[arg(short, long, value_name = "LEVEL")]
    level: Option<String>,

    /// Message category filter
    #[arg(short, long, value_name = "KIND")]
    category: Option<String>,

    /// Fixed string substring search (case-insensitive)
    #[arg(long, value_name = "PATTERN")]
    fgrep: Option<String>,

    /// Regex pattern search
    #[arg(long, value_name = "PATTERN")]
    grep: Option<String>,

    /// Expand structured blocks inline (CHANNEL_DATA fields/variables, SDP bodies, codec negotiation)
    #[arg(long)]
    blocks: bool,

    /// Annotate entries with tracked session state (dialplan context, channel state, channel name)
    #[arg(long)]
    session: bool,

    /// Summary only, no per-entry output
    #[arg(long)]
    stats: bool,

    /// Report unclassified lines
    #[arg(long)]
    unclassified: bool,

    /// Show line numbers in output
    #[arg(short = 'n', long)]
    line_numbers: bool,
}

#[derive(clap::Args)]
struct SearchArgs {
    /// Start date (progressive tab-complete from filenames)
    #[arg(long)]
    from: Option<String>,

    /// End date (progressive tab-complete from filenames)
    #[arg(long)]
    until: Option<String>,

    /// Skip confirmation prompt for large file sets
    #[arg(short = 'y', long)]
    yes: bool,

    #[command(flatten)]
    filter: FilterArgs,

    /// Explicit files (overrides --from/--until auto-discovery)
    #[arg(value_name = "FILES")]
    files: Vec<PathBuf>,
}

#[derive(clap::Args)]
struct ReadArgs {
    #[command(flatten)]
    filter: FilterArgs,

    /// Log file to read (default: freeswitch.log in --dir, or stdin if `-`)
    #[arg(value_name = "FILE")]
    file: Option<String>,
}

#[derive(clap::Args)]
struct TailArgs {
    #[command(flatten)]
    filter: FilterArgs,

    /// Number of recent lines to show initially
    #[arg(long, default_value = "50")]
    lines: usize,

    /// Log file to tail (default: freeswitch.log in --dir)
    #[arg(value_name = "FILE")]
    file: Option<String>,
}

fn resolve_color(when: ColorWhen, use_pager: bool) -> ColorMode {
    match when {
        ColorWhen::Always => ColorMode::Always,
        ColorWhen::Never => ColorMode::Never,
        ColorWhen::Auto => {
            if use_pager || io::stdout().is_terminal() {
                ColorMode::Always
            } else {
                ColorMode::Never
            }
        }
    }
}

fn build_filter(filter: &FilterArgs, from: Option<&str>, until: Option<&str>) -> FilterConfig {
    let min_level: Option<LogLevel> = filter.level.as_ref().map(|l| {
        l.parse().unwrap_or_else(|_| {
            eprintln!("invalid log level: {l}");
            eprintln!("valid levels: {}", LogLevel::ALL_LABELS.join(", "));
            process::exit(2);
        })
    });

    if let Some(ref cat) = filter.category {
        if !MessageKind::ALL_LABELS.contains(&cat.as_str()) {
            eprintln!("invalid category: {cat}");
            eprintln!("valid categories: {}", MessageKind::ALL_LABELS.join(", "));
            process::exit(2);
        }
    }

    let grep = filter.grep.as_ref().map(|pattern| {
        regex::Regex::new(pattern).unwrap_or_else(|e| {
            eprintln!("invalid regex: {e}");
            process::exit(2);
        })
    });

    FilterConfig {
        uuid_filter: filter.uuid.as_deref().map(|u| u.to_lowercase()),
        min_level,
        category: filter.category.clone(),
        fgrep: filter.fgrep.clone(),
        grep,
        from_ts: from.map(normalize_date_from),
        until_ts: until.map(normalize_date_until),
    }
}

fn setup_pager(cli: &Cli) -> Option<process::Child> {
    if cli.no_pager || !io::stdout().is_terminal() {
        return None;
    }
    if matches!(cli.command, Command::Completions { .. } | Command::Tail(_)) {
        return None;
    }
    let pager_cmd = std::env::var("FSLOG_PAGER").unwrap_or_else(|_| "less".to_string());
    let mut parts = pager_cmd.split_whitespace();
    let program = parts.next()?;
    let args: Vec<&str> = parts.collect();
    let default_args;
    let final_args = if args.is_empty() && program == "less" {
        default_args = ["-RFX"];
        &default_args[..]
    } else {
        &args[..]
    };
    process::Command::new(program)
        .args(final_args)
        .stdin(process::Stdio::piped())
        .spawn()
        .ok()
}

fn run_with_output(cli: Cli, use_pager: bool, out: &mut dyn Write) -> io::Result<()> {
    let color = resolve_color(cli.color, use_pager);
    match cli.command {
        Command::List => cmd_list(&cli.dir, out),
        Command::Search(ref args) => cmd_search(&cli.dir, args, color, out),
        Command::Read(ref args) => cmd_read(&cli.dir, args, color, out),
        Command::Tail(ref args) => cmd_tail(&cli.dir, args, color, out),
        #[cfg(feature = "tui")]
        Command::Monitor(_) => unreachable!("handled in main()"),
        Command::Completions { shell } => {
            let mut cmd = Cli::command();
            complete::generate_completions(shell, &mut cmd);
            Ok(())
        }
    }
}

fn cmd_list(dir: &Path, out: &mut dyn Write) -> io::Result<()> {
    let files = discover_log_files(dir)?;
    for f in &files {
        let date = f
            .date
            .as_deref()
            .map(|d| {
                // "2026-03-08-16-52-07" → "2026-03-08 16:52"
                if d.len() >= 16 {
                    format!("{} {}:{}", &d[..10], &d[11..13], &d[14..16])
                } else {
                    d.to_string()
                }
            })
            .unwrap_or_else(|| "(current)".to_string());
        let size = format_size(f.size);
        let name = f.path.file_name().unwrap().to_string_lossy();
        writeln!(out, "{date:<17} {size:>6}  {name}")?;
    }
    Ok(())
}

fn separator_entry(kind: MessageKind, msg: String) -> LogEntry {
    LogEntry {
        uuid: String::new(),
        timestamp: String::new(),
        level: None,
        idle_pct: None,
        source: None,
        message: msg,
        kind: LineKind::Full,
        message_kind: kind,
        block: None,
        attached: Vec::new(),
        line_number: 0,
        warnings: Vec::new(),
    }
}

fn cmd_search(
    dir: &Path,
    args: &SearchArgs,
    color: ColorMode,
    out: &mut dyn Write,
) -> io::Result<()> {
    let filter = build_filter(&args.filter, args.from.as_deref(), args.until.as_deref());
    let tracking = if args.filter.unclassified {
        UnclassifiedTracking::CaptureData
    } else {
        UnclassifiedTracking::CountOnly
    };

    let segments: Vec<(String, Box<dyn Iterator<Item = String>>)> = if !args.files.is_empty() {
        args.files
            .iter()
            .map(|p| {
                let name = p
                    .file_name()
                    .unwrap_or_default()
                    .to_string_lossy()
                    .into_owned();
                (name, lazy_log_reader(p.clone()))
            })
            .collect()
    } else {
        let all_files = discover_log_files(dir)?;
        let selected =
            filter_files_by_date(&all_files, args.from.as_deref(), args.until.as_deref());
        if selected.is_empty() {
            eprintln!("no log files match the date range");
            return Ok(());
        }
        if !args.yes && selected.len() > 20 {
            let total_size: u64 = selected.iter().map(|f| f.size).sum();
            if !io::stdin().is_terminal() {
                return Err(io::Error::other(format!(
                    "refusing to scan {} files ({}) without confirmation; pass -y to override",
                    selected.len(),
                    format_size(total_size)
                )));
            }
            eprint!(
                "about to scan {} files ({}), proceed? [y/N] ",
                selected.len(),
                format_size(total_size)
            );
            let mut answer = String::new();
            io::stdin().read_line(&mut answer)?;
            if !answer.trim().eq_ignore_ascii_case("y") {
                return Ok(());
            }
        }
        selected
            .iter()
            .map(|f| {
                let name = f
                    .path
                    .file_name()
                    .map(|n| n.to_string_lossy().into_owned())
                    .unwrap_or_default();
                (name, lazy_log_reader(f.path.clone()))
            })
            .collect()
    };

    let (chain, seg_tracker) = TrackedChain::new(segments);

    let printer = EntryPrinter {
        color,
        show_blocks: args.filter.blocks,
        show_session: args.filter.session,
        show_filename: false,
        show_line_numbers: args.filter.line_numbers,
    };

    let stream = LogStream::new(chain).unclassified_tracking(tracking);
    let mut count: u64 = 0;
    let mut last_seg: Option<usize> = None;
    let mut last_date = String::new();

    let mut print_matching = |out: &mut dyn Write,
                              entry: &LogEntry,
                              session: Option<&freeswitch_log_parser::SessionSnapshot>|
     -> io::Result<()> {
        count += 1;
        if !filter.matches(entry) {
            return Ok(());
        }
        if args.filter.stats {
            return Ok(());
        }
        if let Some((idx, name)) = seg_tracker.segment_for_line(entry.line_number) {
            if last_seg != Some(idx) {
                last_seg = Some(idx);
                let sep = separator_entry(MessageKind::FileChange, name.to_string());
                printer.print_entry(out, &sep, None, None)?;
            }
        }
        if entry.timestamp.len() >= 10 {
            let date = &entry.timestamp[..10];
            if date != last_date {
                last_date = date.to_string();
                let sep = separator_entry(MessageKind::DateChange, last_date.clone());
                printer.print_entry(out, &sep, None, None)?;
            }
        }
        printer.print_entry(out, entry, session, None)
    };

    let (stats, session_count) = if args.filter.session {
        let mut tracker = SessionTracker::new(stream);
        for enriched in tracker.by_ref() {
            print_matching(out, &enriched.entry, enriched.session.as_ref())?;
        }
        (tracker.stats().clone(), tracker.sessions().len())
    } else {
        let mut stream = stream;
        for entry in stream.by_ref() {
            print_matching(out, &entry, None)?;
        }
        (stream.stats().clone(), 0)
    };

    if args.filter.stats || args.filter.unclassified {
        printer.print_stats(&mut io::stderr(), &stats, count, session_count)?;
    }
    if args.filter.unclassified {
        printer.print_unclassified(&mut io::stderr(), &stats)?;
    }

    Ok(())
}

fn cmd_read(dir: &Path, args: &ReadArgs, color: ColorMode, out: &mut dyn Write) -> io::Result<()> {
    let filter = build_filter(&args.filter, None, None);
    let tracking = if args.filter.unclassified {
        UnclassifiedTracking::CaptureData
    } else {
        UnclassifiedTracking::CountOnly
    };

    let lines: Box<dyn Iterator<Item = String>> = match args.file.as_deref() {
        Some("-") => {
            let stdin = io::stdin();
            Box::new(
                stdin
                    .lock()
                    .lines()
                    .map(|l| l.expect("read error"))
                    .collect::<Vec<_>>()
                    .into_iter(),
            )
        }
        Some(path) => {
            let p = PathBuf::from(path);
            let p = if p.is_absolute() || p.exists() {
                p
            } else {
                dir.join(&p)
            };
            open_log_reader(&p)?
        }
        None => {
            let p = dir.join("freeswitch.log");
            open_log_reader(&p)?
        }
    };

    let printer = EntryPrinter {
        color,
        show_blocks: args.filter.blocks,
        show_session: args.filter.session,
        show_filename: false,
        show_line_numbers: args.filter.line_numbers,
    };

    let stream = LogStream::new(lines).unclassified_tracking(tracking);
    let mut count: u64 = 0;
    let mut last_date = String::new();

    let mut print_matching = |out: &mut dyn Write,
                              entry: &LogEntry,
                              session: Option<&freeswitch_log_parser::SessionSnapshot>|
     -> io::Result<()> {
        count += 1;
        if !filter.matches(entry) {
            return Ok(());
        }
        if args.filter.stats {
            return Ok(());
        }
        if entry.timestamp.len() >= 10 {
            let date = &entry.timestamp[..10];
            if date != last_date {
                last_date = date.to_string();
                let sep = separator_entry(MessageKind::DateChange, last_date.clone());
                printer.print_entry(out, &sep, None, None)?;
            }
        }
        printer.print_entry(out, entry, session, None)
    };

    let (stats, session_count) = if args.filter.session {
        let mut tracker = SessionTracker::new(stream);
        for enriched in tracker.by_ref() {
            print_matching(out, &enriched.entry, enriched.session.as_ref())?;
        }
        (tracker.stats().clone(), tracker.sessions().len())
    } else {
        let mut stream = stream;
        for entry in stream.by_ref() {
            print_matching(out, &entry, None)?;
        }
        (stream.stats().clone(), 0)
    };

    if args.filter.stats || args.filter.unclassified {
        printer.print_stats(&mut io::stderr(), &stats, count, session_count)?;
    }
    if args.filter.unclassified {
        printer.print_unclassified(&mut io::stderr(), &stats)?;
    }

    Ok(())
}

fn cmd_tail(dir: &Path, args: &TailArgs, color: ColorMode, out: &mut dyn Write) -> io::Result<()> {
    let filter = build_filter(&args.filter, None, None);
    let tracking = if args.filter.unclassified {
        UnclassifiedTracking::CaptureData
    } else {
        UnclassifiedTracking::CountOnly
    };

    let path = match args.file.as_deref() {
        Some(p) => PathBuf::from(p),
        None => dir.join("freeswitch.log"),
    };

    let lines = open_tail_reader(&path, args.lines)?;

    let printer = EntryPrinter {
        color,
        show_blocks: args.filter.blocks,
        show_session: args.filter.session,
        show_filename: false,
        show_line_numbers: args.filter.line_numbers,
    };

    let stream = LogStream::new(lines).unclassified_tracking(tracking);
    let mut tracker = SessionTracker::new(stream);

    for enriched in tracker.by_ref() {
        if !filter.matches(&enriched.entry) {
            continue;
        }
        if !args.filter.stats {
            printer.print_entry(out, &enriched.entry, enriched.session.as_ref(), None)?;
            out.flush()?;
        }
    }

    Ok(())
}

fn main() {
    let cli = Cli::parse();

    #[cfg(feature = "tui")]
    if let Command::Monitor(args) = cli.command {
        if let Err(e) = monitor::run(&cli.dir, args) {
            eprintln!("fslog: {e}");
            process::exit(1);
        }
        return;
    }

    let mut pager = setup_pager(&cli);
    let use_pager = pager.is_some();

    let result = if let Some(ref mut child) = pager {
        let mut stdin = child.stdin.take().expect("pager stdin");
        let result = run_with_output(cli, use_pager, &mut stdin);
        drop(stdin);
        let _ = child.wait();
        result
    } else {
        let stdout = io::stdout();
        let mut lock = stdout.lock();
        run_with_output(cli, use_pager, &mut lock)
    };

    if let Err(e) = result {
        if e.kind() != io::ErrorKind::BrokenPipe {
            eprintln!("fslog: {e}");
            process::exit(1);
        }
    }
}