Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature position #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ use std::path::Path;
use std::thread::sleep;
use std::time::Duration;

/// Where shall it starts to print from
pub enum StartFrom {
/// The beginning of the file
Beginning,
/// Specify the cursor position offset
Offset(u64),
/// The end of the file, which is the last known position
End,
}

pub enum LogWatcherAction {
None,
SeekToEnd,
Expand All @@ -23,7 +33,10 @@ pub struct LogWatcher {
}

impl LogWatcher {
pub fn register<P: AsRef<Path>>(filename: P) -> Result<LogWatcher, io::Error> {
pub fn register<P: AsRef<Path>>(
filename: P,
starts_from: StartFrom,
) -> Result<LogWatcher, io::Error> {
let f = match File::open(&filename) {
Ok(x) => x,
Err(err) => return Err(err),
Expand All @@ -35,20 +48,27 @@ impl LogWatcher {
};

let mut reader = BufReader::new(f);
let pos = metadata.len();
reader.seek(SeekFrom::Start(pos)).unwrap();

let starts_from = match starts_from {
StartFrom::Beginning => 0u64,
StartFrom::Offset(pos) => pos,
StartFrom::End => metadata.len(),
};

reader.seek(SeekFrom::Start(starts_from)).unwrap();

Ok(LogWatcher {
filename: filename.as_ref().to_string_lossy().to_string(),
inode: metadata.ino(),
pos: pos,
reader: reader,
pos: starts_from,
reader,
finish: false,
})
}

fn reopen_if_log_rotated<F: ?Sized>(&mut self, callback: &mut F)
where
F: FnMut(String) -> LogWatcherAction,
F: FnMut(u64, usize, String) -> LogWatcherAction,
{
loop {
match File::open(&self.filename) {
Expand Down Expand Up @@ -86,17 +106,18 @@ impl LogWatcher {

pub fn watch<F: ?Sized>(&mut self, callback: &mut F)
where
F: FnMut(String) -> LogWatcherAction,
F: FnMut(u64, usize, String) -> LogWatcherAction,
{
loop {
let mut line = String::new();
let resp = self.reader.read_line(&mut line);
match resp {
Ok(len) => {
if len > 0 {
let old_pos = self.pos;
self.pos += len as u64;
self.reader.seek(SeekFrom::Start(self.pos)).unwrap();
match callback(line.replace("\n", "")) {
match callback(old_pos, len, line.replace("\n", "")) {
LogWatcherAction::SeekToEnd => {
println!("SeekToEnd");
self.reader.seek(SeekFrom::End(0)).unwrap();
Expand Down
12 changes: 6 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
extern crate logwatcher;

use logwatcher::{LogWatcher, LogWatcherAction, StartFrom};
use std::env::args;
use std::process::exit;

extern crate logwatcher;
use logwatcher::{LogWatcher, LogWatcherAction};

fn main() {
let filename = match args().nth(1) {
Some(x) => x,
Expand All @@ -13,10 +13,10 @@ fn main() {
}
};

let mut log_watcher = LogWatcher::register(filename).unwrap();
let mut log_watcher = LogWatcher::register(filename, StartFrom::End).unwrap();

log_watcher.watch(&mut move |line: String| {
println!("Line {}", line);
log_watcher.watch(&mut move |pos: u64, len: usize, line: String| {
println!("Pos #{}, len {} char, Line: `{}`", pos, len, line);
LogWatcherAction::None
});
}