Struct chase::Chaser
[−]
[src]
pub struct Chaser { pub line: Line, pub path: PathBuf, pub initial_no_file_wait: Duration, pub initial_no_file_attempts: Option<usize>, pub rotation_check_wait: Duration, pub rotation_check_attempts: Option<usize>, pub not_rotated_wait: Duration, }
Your entry point for following a file.
Fields
line: Line
Line to start chasing from
path: PathBuf
Path of the file you want to chase
initial_no_file_wait: Duration
When we start running and there is no file and/or file info to be read, how long to wait before retrying
initial_no_file_attempts: Option<usize>
When we start running and there is no file and/or file info to be read, how many times to keep trying. None means no limit.
rotation_check_wait: Duration
When we trying to detect a file rotation and there is no file and/or file info to be read, how long to wait before retrying
rotation_check_attempts: Option<usize>
When we trying to detect a file rotation and there is no file and/or file info to be read, how many times to keep trying. None means no limit.
not_rotated_wait: Duration
After we read a file to its end, how long to wait before trying to read the next line again.
Methods
impl Chaser
[src]
fn new<S>(path: S) -> Chaser where
S: Into<PathBuf>,
[src]
S: Into<PathBuf>,
Creates a new Chaser with default options
impl Chaser
[src]
fn run<F>(&mut self, f: F) -> Result<(), ChaseError> where
F: FnMut(&str, Line, Pos) -> Result<Control, ChaseError>,
[src]
F: FnMut(&str, Line, Pos) -> Result<Control, ChaseError>,
Start chasing a file synchronously.
The provided callback function will be invoked whenever a line is read.
let temp_dir = TempDir::new("chase-test-sync-docs").unwrap(); let file_path = temp_dir.path().join("test.log"); let mut chaser = Chaser::new(&file_path); let mut file_write = OpenOptions::new() .write(true) .append(true) .create(true) .open(&file_path) .unwrap(); write!(file_write, "Hello, world 1\n").unwrap(); write!(file_write, "Hello, world 2\n").unwrap(); write!(file_write, "Hello, world 3\n").unwrap(); let mut seen = Vec::with_capacity(3); // This is a synchronous loop; so we need to exit manually chaser.run(|line, _, _| { seen.push(line.to_string()); if seen.len() < 3 { Ok(Control::Continue) } else { Ok(Control::Stop) } }).unwrap(); assert_eq!(seen, vec!["Hello, world 1".to_string(), "Hello, world 2".to_string(), "Hello, world 3".to_string()]); drop(file_write); temp_dir.close().unwrap();Run
impl Chaser
[src]
fn run_stream(
self
) -> Result<(Receiver<(String, Line, Pos)>, JoinHandle<Result<(), ChaseError>>), ChaseError>
[src]
self
) -> Result<(Receiver<(String, Line, Pos)>, JoinHandle<Result<(), ChaseError>>), ChaseError>
Consume the given Chaser and returns a Stream from which you can read attempts to read lines from the file
let temp_dir = TempDir::new("chase-test-stream-docs").unwrap(); let file_path = temp_dir.path().join("test.log"); let chaser = Chaser::new(&file_path); let mut file_write = OpenOptions::new() .write(true) .append(true) .create(true) .open(&file_path) .unwrap(); write!(file_write, "Hello, world 1\n").unwrap(); write!(file_write, "Hello, world 2\n").unwrap(); let (stream, _) = chaser.run_stream().unwrap(); let accumulated = stream .take(3) // we'll add another one after this is declared to show things are really async .fold(String::new(), |mut acc, (line, _, _)| { acc.push_str(&line); future::ok(acc) }); write!(file_write, "Hello, world 3\n").unwrap(); assert_eq!( accumulated.wait(), Ok("Hello, world 1Hello, world 2Hello, world 3".to_string()) ); drop(file_write); temp_dir.close().unwrap();Run
impl Chaser
[src]
fn run_channel(
self
) -> Result<(Receiver<(String, Line, Pos)>, JoinHandle<Result<(), ChaseError>>), ChaseError>
[src]
self
) -> Result<(Receiver<(String, Line, Pos)>, JoinHandle<Result<(), ChaseError>>), ChaseError>
Consumes the given chaser and gives you back a standard lib Channel to read from
let temp_dir = TempDir::new("chase-test-channel-docs").unwrap(); let file_path = temp_dir.path().join("test.log"); let chaser = Chaser::new(&file_path); let mut file_write = OpenOptions::new() .write(true) .append(true) .create(true) .open(&file_path) .unwrap(); write!(file_write, "Hello, world 1\n").unwrap(); write!(file_write, "Hello, world 2\n").unwrap(); let mut seen = String::new(); let (receiver, _) = chaser.run_channel().unwrap(); seen.push_str(&receiver.recv().unwrap().0); seen.push_str(&receiver.recv().unwrap().0); assert_eq!(seen.as_str(), "Hello, world 1Hello, world 2"); write!(file_write, "Hello, world 3\n").unwrap(); seen.push_str(&receiver.recv().unwrap().0); assert_eq!(seen.as_str(), "Hello, world 1Hello, world 2Hello, world 3"); drop(receiver); drop(file_write); temp_dir.close().unwrap();Run