chase/
data.rs

1//! Holds various data structures used for following files
2
3use std::io::BufReader;
4use std::fs::File;
5use std::time::Duration;
6
7use std::path::PathBuf;
8
9pub const DEFAULT_ROTATION_CHECK_WAIT_MILLIS: u64 = 100;
10pub const DEFAULT_NOT_ROTATED_WAIT_MILLIS: u64 = 50;
11
12#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd)]
13#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
14pub struct Line(pub usize);
15
16#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd)]
17#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
18pub struct Pos(pub u64);
19
20#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd)]
21#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
22pub(crate) struct FileId(pub(crate) u64);
23
24/// Your entry point for following a file.
25#[derive(Debug, Clone)]
26#[cfg_attr(feature = "with-serde", derive(Serialize, Deserialize))]
27pub struct Chaser {
28    /// Line to start chasing from
29    pub line: Line,
30    /// Path of the file you want to chase
31    pub path: PathBuf,
32    /// When we start running and there is no file and/or file info to be read, how long to
33    /// wait before retrying
34    pub initial_no_file_wait: Duration,
35    /// When we start running and there is no file and/or file info to be read, how many
36    /// times to keep trying. None means no limit.
37    pub initial_no_file_attempts: Option<usize>,
38    /// When we trying to detect a file rotation  and there is no file and/or file info to be
39    /// read, how long to wait before retrying
40    pub rotation_check_wait: Duration,
41    /// When we trying to detect a file rotation  and there is no file and/or file info to be
42    /// read, how many times to keep trying. None means no limit.
43    pub rotation_check_attempts: Option<usize>,
44    /// After we read a file to its end, how long to wait before trying to read the next line
45    /// again.
46    pub not_rotated_wait: Duration,
47}
48
49#[derive(Debug)]
50pub(crate) struct Chasing<'a> {
51    pub(crate) chaser: &'a mut Chaser,
52    pub(crate) file_id: FileId,
53    pub(crate) reader: BufReader<File>,
54    pub(crate) buffer: String,
55    pub(crate) line: Line,
56    pub(crate) pos: Pos,
57}
58
59impl Chaser {
60    /// Creates a new Chaser with default options
61    pub fn new<S>(path: S) -> Chaser
62    where
63        S: Into<PathBuf>,
64    {
65        Chaser {
66            line: Line(0),
67            path: path.into(),
68            initial_no_file_attempts: None,
69            initial_no_file_wait: Duration::from_millis(DEFAULT_ROTATION_CHECK_WAIT_MILLIS),
70            rotation_check_attempts: None,
71            rotation_check_wait: Duration::from_millis(DEFAULT_ROTATION_CHECK_WAIT_MILLIS),
72            not_rotated_wait: Duration::from_millis(DEFAULT_NOT_ROTATED_WAIT_MILLIS),
73        }
74    }
75}