use std::path::Path;
use std::thread::sleep;
use std::time::Duration;
use staart::{StaartError, TailedFile};
type Result<T> = std::result::Result<T, StaartError>;
fn main() -> Result<()> {
const DEFAULT_DELAY: Duration = Duration::from_millis(100);
const OPEN_ERR_LIMIT: u8 = 3;
let args: Vec<String> = std::env::args().collect();
let path = Path::new(&args[1]);
let mut f = TailedFile::new(path)?;
let mut open_errors: u8 = 0;
loop {
if let Err(e) = f.read_and(|d| {
if let Ok(s) = std::str::from_utf8(d) {
print!("{s}")
}
}) {
match e {
StaartError::IO(err) if err.kind() == std::io::ErrorKind::NotFound => {
if open_errors >= OPEN_ERR_LIMIT {
let path_str = path.display();
eprintln!(
"Failed to open: {path_str}, more than {open_errors} times. Exiting!");
std::process::exit(1);
} else {
open_errors += 1;
}
}
StaartError::Utf8(_) => {
eprintln!("encountered non-utf8 bytes on read")
}
_ => return Err(e),
}
}
sleep(DEFAULT_DELAY);
}
}