Use closures
This commit is contained in:
parent
f475dc2746
commit
c08f460021
3 changed files with 15 additions and 23 deletions
12
README.md
12
README.md
|
|
@ -22,14 +22,10 @@ Add to your code,
|
|||
extern crate logwatcher;
|
||||
use logwatcher::LogWatcher;
|
||||
|
||||
Create a callback function, which accepts String as input
|
||||
|
||||
fn parse_line(line: String) {
|
||||
println!("Line {}", line);
|
||||
}
|
||||
|
||||
Register the logwatcher and watch it!
|
||||
Register the logwatcher, pass a closure and watch it!
|
||||
|
||||
let mut log_watcher = LogWatcher::register("/var/log/check.log".to_string()).unwrap();
|
||||
log_watcher.watch(parse_line);
|
||||
|
||||
log_watcher.watch(&|line: String| {
|
||||
println!("Line {}", line);
|
||||
});
|
||||
|
|
|
|||
11
src/lib.rs
11
src/lib.rs
|
|
@ -38,7 +38,8 @@ impl LogWatcher {
|
|||
finish: false})
|
||||
}
|
||||
|
||||
fn reopen_if_log_rotated(&mut self, callback: fn (line: String)){
|
||||
fn reopen_if_log_rotated<F: ?Sized>(&mut self, callback: &F)
|
||||
where F: Fn(String) {
|
||||
loop {
|
||||
match File::open(self.filename.clone()) {
|
||||
Ok(x) => {
|
||||
|
|
@ -74,7 +75,8 @@ impl LogWatcher {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn watch(&mut self, callback: fn (line: String)) {
|
||||
pub fn watch<F: ?Sized>(&mut self, callback: &F)
|
||||
where F: Fn(String) {
|
||||
loop{
|
||||
let mut line = String::new();
|
||||
let resp = self.reader.read_line(&mut line);
|
||||
|
|
@ -102,8 +104,3 @@ impl LogWatcher {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn it_works() {
|
||||
}
|
||||
|
|
|
|||
11
src/main.rs
11
src/main.rs
|
|
@ -4,11 +4,6 @@ use std::process::exit;
|
|||
extern crate logwatcher;
|
||||
use logwatcher::LogWatcher;
|
||||
|
||||
|
||||
fn parse_line(line: String) {
|
||||
println!("Line {}", line);
|
||||
}
|
||||
|
||||
fn main(){
|
||||
let filename = match args().nth(1) {
|
||||
Some(x) => x,
|
||||
|
|
@ -17,6 +12,10 @@ fn main(){
|
|||
exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
let mut log_watcher = LogWatcher::register(filename).unwrap();
|
||||
log_watcher.watch(parse_line);
|
||||
|
||||
log_watcher.watch(&|line: String| {
|
||||
println!("Line {}", line);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue