freeswitch-log-parser 0.10.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
use std::fs;
use std::io::{self, BufRead, BufReader};
use std::path::{Path, PathBuf};

use freeswitch_log_parser::{decode_log_line, log_rotation_stamp, read_log_lines, Utf8Decode};
use log::{error, warn};
use xz2::read::XzDecoder;

pub struct LogFile {
    pub path: PathBuf,
    pub date: Option<String>,
    pub size: u64,
}

pub fn discover_log_files(dir: &Path) -> io::Result<Vec<LogFile>> {
    let mut files = Vec::new();
    for entry in fs::read_dir(dir)? {
        let entry = entry?;
        let path = entry.path();
        let name = match path.file_name().and_then(|n| n.to_str()) {
            Some(n) => n.to_string(),
            None => continue,
        };
        if !name.starts_with("freeswitch.log") {
            continue;
        }
        let meta = entry.metadata()?;
        if !meta.is_file() {
            continue;
        }
        let date = log_rotation_stamp(&name).map(str::to_string);
        files.push(LogFile {
            path,
            date,
            size: meta.len(),
        });
    }
    files.sort_by(|a, b| a.date.cmp(&b.date));
    Ok(files)
}

pub fn normalize_date(input: &str) -> String {
    let mut s = input.replace(['T', ':', ' '], "-");
    // Remove trailing dashes from replacement
    while s.ends_with('-') {
        s.pop();
    }
    s
}

pub fn normalize_date_from(input: &str) -> String {
    pad_date(
        &normalize_date(input),
        &["0000", "01", "01", "00", "00", "00"],
    )
}

pub fn normalize_date_until(input: &str) -> String {
    pad_date(
        &normalize_date(input),
        &["9999", "12", "31", "23", "59", "59"],
    )
}

/// Pad a partial `YYYY-MM-DD-HH-MM-SS` date with per-component defaults.
fn pad_date(s: &str, defaults: &[&str; 6]) -> String {
    let parts: Vec<&str> = s.split('-').collect();
    let mut result = Vec::new();
    for (i, default) in defaults.iter().enumerate() {
        if i < parts.len() && !parts[i].is_empty() {
            result.push(parts[i].to_string());
        } else {
            result.push(default.to_string());
        }
    }
    result.join("-")
}

pub fn filter_files_by_date<'a>(
    files: &'a [LogFile],
    from: Option<&str>,
    until: Option<&str>,
) -> Vec<&'a LogFile> {
    let from_norm = from.map(normalize_date_from);
    let until_norm = until.map(normalize_date_until);

    files
        .iter()
        .enumerate()
        .filter(|(i, f)| {
            let Some(ref file_date) = f.date else {
                // Current log (no date) — always include
                return true;
            };

            if let Some(ref until) = until_norm {
                // Skip file N if file_date > until AND previous file also > until
                if file_date.as_str() > until.as_str() && *i > 0 {
                    if let Some(ref prev_date) = files[*i - 1].date {
                        if prev_date.as_str() > until.as_str() {
                            return false;
                        }
                    }
                }
            }

            if let Some(ref from) = from_norm {
                // Include if file_date >= from (file might contain entries up to file_date)
                // But also include the file just before from, since it spans from previous rotation
                if file_date.as_str() < from.as_str() {
                    // Check if next file's date >= from (this file might contain the start)
                    if *i + 1 < files.len() {
                        if let Some(ref next_date) = files[*i + 1].date {
                            if next_date.as_str() >= from.as_str() {
                                return true;
                            }
                        } else {
                            // Next is current log — include this file
                            return true;
                        }
                    }
                    return false;
                }
            }

            true
        })
        .map(|(_, f)| f)
        .collect()
}

/// Resolve an optional FILE argument, defaulting to the live log in `dir`.
pub fn resolve_log_path(dir: &Path, file: Option<&str>) -> PathBuf {
    match file {
        Some(p) => PathBuf::from(p),
        None => dir.join("freeswitch.log"),
    }
}

pub fn open_log_file(path: &Path) -> io::Result<Box<dyn BufRead>> {
    let file = fs::File::open(path)?;
    let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
    if ext == "xz" {
        Ok(Box::new(BufReader::new(XzDecoder::new(file))))
    } else {
        Ok(Box::new(BufReader::new(file)))
    }
}

pub fn open_log_reader(path: &Path) -> io::Result<Box<dyn Iterator<Item = String>>> {
    let reader = open_log_file(path)?;
    Ok(lossy_line_iter(reader))
}

/// Yield log lines as `String`, replacing invalid UTF-8 with U+FFFD instead of
/// panicking. mod_logfile's 2 KiB buffer truncation can chop a multi-byte
/// codepoint mid-character; that benign case is recovered silently. A byte that
/// can't be part of any UTF-8 sequence is genuine corruption — warn, don't hide.
pub fn lossy_line_iter(reader: Box<dyn BufRead>) -> Box<dyn Iterator<Item = String>> {
    Box::new(read_log_lines(reader).map_while(|decoded| match decoded {
        Ok(line) => {
            if let Utf8Decode::InvalidBytes { at } = line.decode {
                warn!("invalid UTF-8 byte at offset {at}, recovered with U+FFFD");
            }
            Some(line.text)
        }
        Err(e) => {
            error!("read error: {e}");
            None
        }
    }))
}

pub fn lazy_log_reader(path: PathBuf) -> Box<dyn Iterator<Item = String>> {
    Box::new(LazyLogReader { path, inner: None })
}

struct LazyLogReader {
    path: PathBuf,
    inner: Option<Box<dyn Iterator<Item = String>>>,
}

impl Iterator for LazyLogReader {
    type Item = String;

    fn next(&mut self) -> Option<String> {
        if self.inner.is_none() {
            match open_log_reader(&self.path) {
                Ok(reader) => self.inner = Some(reader),
                Err(e) => {
                    warn!("skipping {}: open failed: {e}", self.path.display());
                    return None;
                }
            }
        }
        let result = self.inner.as_mut()?.next();
        if result.is_none() {
            self.inner = None;
        }
        result
    }
}

struct TailLines<R: BufRead> {
    reader: R,
    /// Bytes of a line the writer has not terminated yet. Emitting it would hand
    /// the parser half a record and the remainder as a bogus continuation.
    pending: Vec<u8>,
    path: PathBuf,
}

impl TailLines<BufReader<fs::File>> {
    fn new(file: fs::File, path: PathBuf) -> Self {
        TailLines {
            reader: BufReader::new(file),
            pending: Vec::new(),
            path,
        }
    }
}

impl<R: BufRead> Iterator for TailLines<R> {
    type Item = String;

    fn next(&mut self) -> Option<String> {
        loop {
            match self.reader.read_until(b'\n', &mut self.pending) {
                Ok(0) => std::thread::sleep(std::time::Duration::from_millis(250)),
                Ok(_) => {
                    if self.pending.last() != Some(&b'\n') {
                        continue;
                    }
                    let decoded = decode_log_line(&self.pending);
                    self.pending.clear();
                    if let Utf8Decode::InvalidBytes { at } = decoded.decode {
                        warn!(
                            "invalid UTF-8 byte at offset {at} while tailing {}, recovered with U+FFFD",
                            self.path.display()
                        );
                    }
                    return Some(decoded.text);
                }
                Err(e) => {
                    error!("tail read error on {}, stopping: {e}", self.path.display());
                    return None;
                }
            }
        }
    }
}

fn read_tail_context(path: &Path, n_lines: usize) -> io::Result<(Vec<String>, u64)> {
    use std::io::{Seek, SeekFrom};

    let mut file = fs::File::open(path)?;
    let len = file.metadata()?.len();

    if n_lines == 0 || len == 0 {
        return Ok((Vec::new(), len));
    }

    let seek_back = (n_lines as u64).saturating_mul(1024).min(len);
    let seek_pos = len - seek_back;

    if seek_pos > 0 {
        file.seek(SeekFrom::Start(seek_pos))?;
    }

    let reader = BufReader::new(file);
    let mut lines = Vec::new();
    for decoded in read_log_lines(reader) {
        let line = decoded?;
        if let Utf8Decode::InvalidBytes { at } = line.decode {
            warn!("invalid UTF-8 byte at offset {at}, recovered with U+FFFD");
        }
        lines.push(line.text);
    }

    if seek_pos > 0 && !lines.is_empty() {
        lines.remove(0);
    }

    if lines.len() > n_lines {
        lines.drain(..lines.len() - n_lines);
    }

    Ok((lines, len))
}

pub fn open_tail_reader(
    path: &Path,
    initial_lines: usize,
) -> io::Result<Box<dyn Iterator<Item = String>>> {
    use std::io::{Seek, SeekFrom};

    let (context, file_len) = read_tail_context(path, initial_lines)?;

    let mut file = fs::File::open(path)?;
    file.seek(SeekFrom::Start(file_len))?;
    let tail = TailLines::new(file, path.to_path_buf());

    Ok(Box::new(context.into_iter().chain(tail)))
}

#[cfg(feature = "tui")]
pub fn open_full_tail_reader(path: &Path) -> io::Result<Box<dyn Iterator<Item = String>>> {
    use std::io::{Seek, SeekFrom};

    let reader = open_log_file(path)?;
    let end_pos = fs::File::open(path)?.metadata()?.len();
    let lines = lossy_line_iter(reader);

    let mut file = fs::File::open(path)?;
    file.seek(SeekFrom::Start(end_pos))?;
    let tail = TailLines::new(file, path.to_path_buf());

    Ok(Box::new(lines.chain(tail)))
}

pub fn format_size(bytes: u64) -> String {
    const KB: u64 = 1024;
    const MB: u64 = 1024 * KB;
    const GB: u64 = 1024 * MB;
    if bytes >= GB {
        format!("{:.1}G", bytes as f64 / GB as f64)
    } else if bytes >= MB {
        format!("{:.1}M", bytes as f64 / MB as f64)
    } else if bytes >= KB {
        format!("{:.1}K", bytes as f64 / KB as f64)
    } else {
        format!("{bytes}B")
    }
}

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

    /// Drive the follower over a fixed buffer. Every assertion below consumes
    /// only terminated lines, so the EOF sleep is never reached.
    fn tail_over<R: BufRead>(reader: R) -> TailLines<R> {
        TailLines {
            reader,
            pending: Vec::new(),
            path: PathBuf::from("test.log"),
        }
    }

    fn cursor(bytes: &[u8]) -> io::Cursor<Vec<u8>> {
        io::Cursor::new(bytes.to_vec())
    }

    /// Errors instead of reporting EOF, so a test can reach the terminal arm of
    /// `TailLines::next` without waiting on the follower's EOF sleep.
    struct FailAtEof(io::Cursor<Vec<u8>>);

    impl io::Read for FailAtEof {
        fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
            match io::Read::read(&mut self.0, buf)? {
                0 => Err(io::Error::other("simulated read failure")),
                n => Ok(n),
            }
        }
    }

    #[test]
    fn tail_survives_truncated_codepoint() {
        // "é" (0xC3 0xA9) cut after its lead byte, as mod_logfile's 2 KiB
        // buffer does; the follower must keep going, not stop on InvalidData.
        let lines: Vec<String> = tail_over(cursor(b"caf\xc3\nnext line\n")).take(2).collect();
        assert_eq!(lines.len(), 2);
        assert!(lines[0].starts_with("caf"), "line: {:?}", lines[0]);
        assert_eq!(lines[1], "next line");
    }

    #[test]
    fn tail_recovers_invalid_bytes() {
        let lines: Vec<String> = tail_over(cursor(b"bad\xffbyte\nafter\n")).take(2).collect();
        assert_eq!(lines[0], "bad\u{fffd}byte");
        assert_eq!(lines[1], "after");
    }

    #[test]
    fn tail_strips_crlf() {
        let lines: Vec<String> = tail_over(cursor(b"one\r\ntwo\n")).take(2).collect();
        assert_eq!(lines, vec!["one", "two"]);
    }

    #[test]
    fn tail_withholds_unterminated_line() {
        // The writer has not flushed the newline yet: emitting now would yield
        // half a record, and the remainder as a bogus continuation.
        let mut tail = tail_over(BufReader::new(FailAtEof(cursor(b"complete\nhalf-writ"))));
        assert_eq!(tail.next(), Some("complete".to_string()));
        assert_eq!(tail.next(), None);
        assert_eq!(tail.pending, b"half-writ");
    }

    #[test]
    fn normalize_iso_date() {
        assert_eq!(normalize_date("2026-03-08T15:48"), "2026-03-08-15-48");
    }

    #[test]
    fn normalize_fs_style() {
        assert_eq!(normalize_date("2026-03-08-15-48"), "2026-03-08-15-48");
    }

    #[test]
    fn normalize_space_date() {
        assert_eq!(normalize_date("2026-03-08 15:48"), "2026-03-08-15-48");
    }

    #[test]
    fn pad_from_year_month() {
        assert_eq!(normalize_date_from("2026-03"), "2026-03-01-00-00-00");
    }

    #[test]
    fn pad_until_year_month() {
        assert_eq!(normalize_date_until("2026-03"), "2026-03-31-23-59-59");
    }

    #[test]
    fn pad_from_date() {
        assert_eq!(normalize_date_from("2026-03-08"), "2026-03-08-00-00-00");
    }

    #[test]
    fn pad_until_date() {
        assert_eq!(normalize_date_until("2026-03-08"), "2026-03-08-23-59-59");
    }

    #[test]
    fn format_size_megabytes() {
        assert_eq!(format_size(12_900_000), "12.3M");
    }

    #[test]
    fn format_size_gigabytes() {
        assert_eq!(format_size(2_147_483_648), "2.0G");
    }

    #[test]
    fn format_size_kilobytes() {
        assert_eq!(format_size(500_000), "488.3K");
    }

    #[test]
    fn format_size_bytes() {
        assert_eq!(format_size(512), "512B");
    }

    /// A rotation stamp is the *end* of the file's span, so the selection has to
    /// reach one file past each bound. These fixtures rotate daily at midnight.
    fn day_files(days: &[Option<&str>]) -> Vec<LogFile> {
        days.iter()
            .map(|d| LogFile {
                path: PathBuf::from("freeswitch.log"),
                date: d.map(|d| format!("2026-03-{d}-00-00-00")),
                size: 0,
            })
            .collect()
    }

    fn selected(files: &[LogFile], from: Option<&str>, until: Option<&str>) -> Vec<String> {
        filter_files_by_date(files, from, until)
            .iter()
            .map(|f| match &f.date {
                Some(d) => d[8..10].to_string(),
                None => "live".to_string(),
            })
            .collect()
    }

    #[test]
    fn unbounded_selection_keeps_everything() {
        let files = day_files(&[Some("08"), Some("09"), None]);
        assert_eq!(selected(&files, None, None), ["08", "09", "live"]);
    }

    #[test]
    fn from_reaches_back_one_file() {
        // Entries from the 10th live in the file stamped the 11th, and the file
        // stamped the 10th holds the small hours the 9th's rotation left behind.
        let files = day_files(&[Some("08"), Some("09"), Some("10"), Some("11")]);
        assert_eq!(
            selected(&files, Some("2026-03-10"), None),
            ["09", "10", "11"]
        );
    }

    #[test]
    fn from_drops_files_that_end_before_the_next_one_starts() {
        let files = day_files(&[Some("08"), Some("09"), Some("10"), Some("11")]);
        assert_eq!(selected(&files, Some("2026-03-11"), None), ["10", "11"]);
    }

    #[test]
    fn until_keeps_the_file_the_bound_falls_inside() {
        // The 10th's file spans the 9th, so an `until` on the 9th still needs it;
        // the 11th's cannot hold anything that early.
        let files = day_files(&[Some("08"), Some("09"), Some("10"), Some("11")]);
        assert_eq!(
            selected(&files, None, Some("2026-03-09")),
            ["08", "09", "10"]
        );
    }

    #[test]
    fn the_active_log_is_never_excluded_by_date() {
        // It has no stamp, so nothing can prove it lacks entries in the window.
        let files = day_files(&[Some("08"), Some("09"), None]);
        assert_eq!(
            selected(&files, None, Some("2026-03-08")),
            ["08", "09", "live"]
        );
        assert_eq!(selected(&files, Some("2026-03-20"), None), ["09", "live"]);
    }

    #[test]
    fn a_month_bound_spans_the_whole_month() {
        let files = day_files(&[Some("08"), Some("09")]);
        assert_eq!(
            selected(&files, Some("2026-03"), Some("2026-03")),
            ["08", "09"]
        );
        assert!(selected(&files, Some("2026-04"), Some("2026-04")).is_empty());
    }
}