freeswitch-log-parser 0.17.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
//! Turning a log file into lines: the decompressing reader, the follower, and
//! the read cap they share.

use std::cell::RefCell;
use std::fs;
use std::io::{BufRead, BufReader};
use std::path::{Path, PathBuf};
use std::rc::Rc;

use anyhow::Context;
use freeswitch_log_parser::{
    decode_log_line, read_log_line_capped, read_log_lines_capped, trim_capped_tail, OverCap,
    Utf8Decode,
};
use log::warn;
use xz2::read::XzDecoder;

use super::display_name;

/// One file's line-level anomalies: first of each kind reported where it
/// happens, rest counted into one summary, or a corrupt file warns per line.
struct LineReport {
    name: String,
    over_cap: u64,
    largest: usize,
    invalid_utf8: u64,
}

impl LineReport {
    fn new(name: String) -> Self {
        LineReport {
            name,
            over_cap: 0,
            largest: 0,
            invalid_utf8: 0,
        }
    }

    fn record_over_cap(&mut self, over: OverCap) {
        self.over_cap += 1;
        self.largest = self.largest.max(over.line_bytes);
        if self.over_cap == 1 {
            warn!(
                "{}: line of {} bytes read only to {}, remainder dropped",
                self.name, over.line_bytes, over.cap
            );
        }
    }

    fn record_invalid_utf8(&mut self, at: usize) {
        self.invalid_utf8 += 1;
        if self.invalid_utf8 == 1 {
            warn!(
                "{}: invalid UTF-8 byte at offset {at}, recovered with U+FFFD",
                self.name
            );
        }
    }
}

impl Drop for LineReport {
    fn drop(&mut self) {
        if self.over_cap > 1 {
            warn!(
                "{}: {} lines exceeded the read cap, largest {} bytes",
                self.name, self.over_cap, self.largest
            );
        }
        if self.invalid_utf8 > 1 {
            warn!(
                "{}: {} lines held invalid UTF-8, recovered with U+FFFD",
                self.name, self.invalid_utf8
            );
        }
    }
}

pub fn open_log_file(path: &Path) -> anyhow::Result<Box<dyn BufRead>> {
    let file =
        fs::File::open(path).with_context(|| format!("opening log file {}", path.display()))?;
    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,
    max_line_bytes: usize,
    failures: &ReadFailures,
) -> anyhow::Result<Box<dyn Iterator<Item = String>>> {
    let reader = open_log_file(path)?;
    Ok(lossy_line_iter(
        reader,
        display_name(path),
        max_line_bytes,
        failures.clone(),
    ))
}

/// Yield log lines as `String`. A codepoint mod_logfile's buffer chopped is
/// recovered silently; genuine corruption and an over-cap line are reported.
pub fn lossy_line_iter(
    reader: Box<dyn BufRead>,
    name: String,
    max_line_bytes: usize,
    failures: ReadFailures,
) -> Box<dyn Iterator<Item = String>> {
    let mut report = LineReport::new(name.clone());
    Box::new(read_log_lines_capped(reader, max_line_bytes).map_while(
        move |decoded| match decoded {
            Ok(capped) => {
                if let Some(over) = capped.over_cap {
                    report.record_over_cap(over);
                }
                if let Utf8Decode::InvalidBytes { at } = capped.line.decode {
                    report.record_invalid_utf8(at);
                }
                Some(capped.line.text)
            }
            Err(e) => {
                failures.record(anyhow::Error::new(e).context(format!("reading {name}")));
                None
            }
        },
    ))
}

/// Where a reader parks a failure it cannot return: its item type is the
/// parser's `String`, and a scan that skipped a file must not exit clean.
#[derive(Clone, Default)]
pub struct ReadFailures(Rc<RefCell<Vec<anyhow::Error>>>);

impl ReadFailures {
    pub fn record(&self, err: anyhow::Error) {
        self.0.borrow_mut().push(err);
    }

    /// The first failure recorded so far, counting the rest, and clear them.
    pub fn check(&self) -> anyhow::Result<()> {
        let mut held = self.0.borrow_mut();
        let extra = held.len().saturating_sub(1);
        let Some(first) = held.drain(..).next() else {
            return Ok(());
        };
        match extra {
            0 => Err(first),
            n => Err(first.context(format!("{n} further file(s) also failed to be read"))),
        }
    }
}

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

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

impl Iterator for LazyLogReader {
    type Item = String;

    fn next(&mut self) -> Option<String> {
        if self.inner.is_none() {
            match open_log_reader(&self.path, self.max_line_bytes, &self.failures) {
                Ok(reader) => self.inner = Some(reader),
                Err(e) => {
                    self.failures.record(e);
                    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>,
    /// Length of that line as written, which past the cap outruns `pending`.
    pending_bytes: usize,
    path: PathBuf,
    max_line_bytes: usize,
    report: LineReport,
    failures: ReadFailures,
}

impl TailLines<BufReader<fs::File>> {
    fn new(file: fs::File, path: PathBuf, max_line_bytes: usize, failures: ReadFailures) -> Self {
        let report = LineReport::new(display_name(&path));
        TailLines {
            reader: BufReader::new(file),
            pending: Vec::new(),
            pending_bytes: 0,
            path,
            max_line_bytes,
            report,
            failures,
        }
    }
}

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

    fn next(&mut self) -> Option<String> {
        loop {
            let read = match read_log_line_capped(
                &mut self.reader,
                self.max_line_bytes,
                &mut self.pending,
            ) {
                Ok(read) => read,
                Err(e) => {
                    self.failures.record(
                        anyhow::Error::new(e).context(format!("tailing {}", self.path.display())),
                    );
                    return None;
                }
            };
            let Some(read) = read else {
                std::thread::sleep(std::time::Duration::from_millis(250));
                continue;
            };
            self.pending_bytes += read.line_bytes;
            if !read.terminated {
                continue;
            }
            if self.pending_bytes > self.max_line_bytes {
                self.report
                    .record_over_cap(OverCap::new(self.max_line_bytes, self.pending_bytes));
                trim_capped_tail(&mut self.pending);
            }
            let decoded = decode_log_line(&self.pending);
            self.pending.clear();
            self.pending_bytes = 0;
            if let Utf8Decode::InvalidBytes { at } = decoded.decode {
                self.report.record_invalid_utf8(at);
            }
            return Some(decoded.text);
        }
    }
}

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

    let mut file =
        fs::File::open(path).with_context(|| format!("opening log file {}", path.display()))?;
    let len = file
        .metadata()
        .with_context(|| format!("stat {}", path.display()))?
        .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))
            .with_context(|| format!("seeking to {seek_pos} in {}", path.display()))?;
    }

    let failures = ReadFailures::default();
    let mut lines: Vec<String> = lossy_line_iter(
        Box::new(BufReader::new(file)),
        display_name(path),
        max_line_bytes,
        failures.clone(),
    )
    .collect();
    failures.check()?;

    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,
    max_line_bytes: usize,
    failures: &ReadFailures,
) -> anyhow::Result<Box<dyn Iterator<Item = String>>> {
    use std::io::{Seek, SeekFrom};

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

    let mut file =
        fs::File::open(path).with_context(|| format!("opening log file {}", path.display()))?;
    file.seek(SeekFrom::Start(file_len))
        .with_context(|| format!("seeking to the end of {}", path.display()))?;
    let tail = TailLines::new(file, path.to_path_buf(), max_line_bytes, failures.clone());

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

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

    let reader = open_log_file(path)?;
    let end_pos = fs::File::open(path)
        .and_then(|f| f.metadata())
        .with_context(|| format!("stat {}", path.display()))?
        .len();
    let lines = lossy_line_iter(reader, display_name(path), max_line_bytes, failures.clone());

    let mut file =
        fs::File::open(path).with_context(|| format!("opening log file {}", path.display()))?;
    file.seek(SeekFrom::Start(end_pos))
        .with_context(|| format!("seeking to the end of {}", path.display()))?;
    let tail = TailLines::new(file, path.to_path_buf(), max_line_bytes, failures.clone());

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

#[cfg(test)]
mod tests {
    use std::io;

    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> {
        tail_over_capped(reader, usize::MAX)
    }

    fn tail_over_capped<R: BufRead>(reader: R, max_line_bytes: usize) -> TailLines<R> {
        TailLines {
            reader,
            pending: Vec::new(),
            pending_bytes: 0,
            path: PathBuf::from("test.log"),
            max_line_bytes,
            report: LineReport::new("test.log".to_string()),
            failures: ReadFailures::default(),
        }
    }

    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_hands_the_cr_on_to_the_stream() {
        // LogStream counts a line's bytes against mod_logfile's write budget
        // before trimming, so the reader must not drop one.
        let lines: Vec<String> = tail_over(cursor(b"one\r\ntwo\n")).take(2).collect();
        assert_eq!(lines, vec!["one\r", "two"]);
    }

    #[test]
    fn tail_caps_a_long_line_and_keeps_framing() {
        let lines: Vec<String> = tail_over_capped(cursor(b"aaaaaaaaaa\nbb\n"), 4)
            .take(2)
            .collect();
        assert_eq!(lines, vec!["aaaa", "bb"]);
    }

    #[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");
    }
}