#io #logs #persistent #offset #reading #reader #processors

filetrack

persistent logrotated reading and other useful io things

7 releases

0.2.3 Oct 15, 2023
0.2.2 Oct 10, 2023
0.1.2 Oct 9, 2023

#699 in Data structures

Download history 13/week @ 2024-02-19 4/week @ 2024-02-26 2/week @ 2024-03-11 121/week @ 2024-04-01

123 downloads per month

MIT license

35KB
534 lines

Filetrack

Rust Latest version Documentation License

Filetrack is a library for persistent reading of logs similar to the mechanisms used in Filebeat and other software alike. It provides a few useful primitives for working with IO and its main intention is to be used for implementation of custom log processors.

  • Multireader that lets you work with a list of readers as if you had one single buffer.

  • InodeAwareReader that allows working with rotated logs and maintating persistent offset inside them. Scheme of persistence is to be implemented by user.

  • TrackedReader that allows to read logs or any other content from rotated files with offset persisted across restarts inside a file in case you want a ready-to-use structure.

Example

Read a file line-by-line keeping track of current offset so that you could start where you left off next time. Note that the library does not force you to use this scheme, you can implement your own persistence scheme using InodeAwareReader.

use filetrack::{TrackedReader, TrackedReaderError};
use std::io::BufRead;

// running this script will fetch and print new lines on each execution
fn main() -> Result<(), TrackedReaderError> {
    let mut reader = TrackedReader::new("examples/file.txt", "examples/registry")?;
    let mut input = String::new();
    loop {
        match reader.read_line(&mut input)? {
            0 => break Ok(()),
            _ => println!("read line: `{}`", input.trim_end()),
        };
    }
}

See documentation for more examples and working principles.

Dependencies

~0.7–1.3MB
~31K SLoC