Change closure from Fn to FnMut. (#5)
* Using FnMut, so works * Using FnMut for watch() and reopen_if_log_rotated() * adding fn to force reopen * adding actions to closure Co-authored-by: noyez <brad@bkn.local> Co-authored-by: noyez <brad@bkn.localdomain> Co-authored-by: Bradley Noyes <b@noyes.dev>
This commit is contained in:
parent
7c288c8d1e
commit
6f8f9bd75d
3 changed files with 22 additions and 8 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "logwatcher"
|
name = "logwatcher"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
authors = ["Aravinda VK <mail@aravindavk.in>"]
|
authors = ["Aravinda VK <mail@aravindavk.in>"]
|
||||||
|
|
||||||
description = "A lib to watch log files for new Changes, just like tail -f"
|
description = "A lib to watch log files for new Changes, just like tail -f"
|
||||||
|
|
|
||||||
23
src/lib.rs
23
src/lib.rs
|
|
@ -8,6 +8,11 @@ use std::os::unix::fs::MetadataExt;
|
||||||
use std::thread::sleep;
|
use std::thread::sleep;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
|
pub enum LogWatcherAction{
|
||||||
|
None,
|
||||||
|
SeekToEnd,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct LogWatcher {
|
pub struct LogWatcher {
|
||||||
filename: String,
|
filename: String,
|
||||||
inode: u64,
|
inode: u64,
|
||||||
|
|
@ -40,9 +45,9 @@ impl LogWatcher {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reopen_if_log_rotated<F: ?Sized>(&mut self, callback: &F)
|
fn reopen_if_log_rotated<F: ?Sized>(&mut self, callback: &mut F)
|
||||||
where
|
where
|
||||||
F: Fn(String),
|
F: FnMut(String) -> LogWatcherAction,
|
||||||
{
|
{
|
||||||
loop {
|
loop {
|
||||||
match File::open(self.filename.clone()) {
|
match File::open(self.filename.clone()) {
|
||||||
|
|
@ -78,9 +83,9 @@ impl LogWatcher {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn watch<F: ?Sized>(&mut self, callback: &F)
|
pub fn watch<F: ?Sized>(&mut self, callback: &mut F)
|
||||||
where
|
where
|
||||||
F: Fn(String),
|
F: FnMut(String) -> LogWatcherAction,
|
||||||
{
|
{
|
||||||
loop {
|
loop {
|
||||||
let mut line = String::new();
|
let mut line = String::new();
|
||||||
|
|
@ -90,7 +95,15 @@ impl LogWatcher {
|
||||||
if len > 0 {
|
if len > 0 {
|
||||||
self.pos += len as u64;
|
self.pos += len as u64;
|
||||||
self.reader.seek(SeekFrom::Start(self.pos)).unwrap();
|
self.reader.seek(SeekFrom::Start(self.pos)).unwrap();
|
||||||
callback(line.replace("\n", ""));
|
match callback(line.replace("\n", ""))
|
||||||
|
{
|
||||||
|
LogWatcherAction::SeekToEnd => {
|
||||||
|
println!("SeekToEnd");
|
||||||
|
self.reader.seek(SeekFrom::End(0)).unwrap();
|
||||||
|
}
|
||||||
|
LogWatcherAction::None => {
|
||||||
|
}
|
||||||
|
}
|
||||||
line.clear();
|
line.clear();
|
||||||
} else {
|
} else {
|
||||||
if self.finish {
|
if self.finish {
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ use std::env::args;
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
|
|
||||||
extern crate logwatcher;
|
extern crate logwatcher;
|
||||||
use logwatcher::LogWatcher;
|
use logwatcher::{LogWatcher, LogWatcherAction};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let filename = match args().nth(1) {
|
let filename = match args().nth(1) {
|
||||||
|
|
@ -15,7 +15,8 @@ fn main() {
|
||||||
|
|
||||||
let mut log_watcher = LogWatcher::register(filename).unwrap();
|
let mut log_watcher = LogWatcher::register(filename).unwrap();
|
||||||
|
|
||||||
log_watcher.watch(&|line: String| {
|
log_watcher.watch(&mut move |line: String| {
|
||||||
println!("Line {}", line);
|
println!("Line {}", line);
|
||||||
|
LogWatcherAction::None
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue