1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use notify::{
    self, watcher, DebouncedEvent, PollWatcher, RecommendedWatcher, RecursiveMode, Watcher,
};
use std::collections::HashMap;
use std::error::Error;
use std::fs::File;
use std::io::prelude::*;
use std::io::{BufReader, SeekFrom};
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::sync::mpsc::channel;
use std::time::Duration;
use walkdir::WalkDir;

type HandleMap = HashMap<String, Handle>;
type BoxError = Box<Error>;
type FileFilter = Rc<Fn(&str) -> bool>;
type LineFilter = Rc<Fn(&str) -> bool>;

#[derive(Debug)]
struct Handle {
    pos: u64,
    fd: File,
    path: PathBuf,
}

#[allow(dead_code)]
pub enum WatcherType {
    Poll,
    Recommend,
}

enum NotifyWatcherType {
    PollWatcher(PollWatcher),
    RecommendedWatcher(RecommendedWatcher),
}

impl NotifyWatcherType {
    fn watch(&mut self, dir: &str, mode: RecursiveMode) -> Result<(), notify::Error> {
        match self {
            NotifyWatcherType::PollWatcher(watcher) => watcher.watch(dir, mode),
            NotifyWatcherType::RecommendedWatcher(watcher) => watcher.watch(dir, mode),
        }
    }
}

pub struct WatchOption {
    dir: String,
    debounce_seconds: u64,
    watcher_type: WatcherType,
    // Determin if the file should be watched
    file_filter: FileFilter,
    // Determin if the line should be collected
    line_filter: LineFilter,
    // TODO:
    // Support Transform
}

fn identity(_: &str) -> bool {
    true
}

#[allow(dead_code)]
impl WatchOption {
    pub fn new(dir: String, seconds: u64) -> Self {
        WatchOption {
            dir,
            debounce_seconds: seconds,
            watcher_type: WatcherType::Recommend,
            file_filter: Rc::new(identity),
            line_filter: Rc::new(identity),
        }
    }

    pub fn watcher_type(mut self, watcher_type: WatcherType) -> Self {
        self.watcher_type = watcher_type;
        self
    }

    pub fn file_filter(mut self, filter: FileFilter) -> Self {
        self.file_filter = filter;
        self
    }

    pub fn line_filter(mut self, filter: LineFilter) -> Self {
        self.line_filter = filter;
        self
    }
}

pub fn watch_dir<F: ?Sized>(option: &WatchOption, callback: &F) -> Result<(), Box<Error>>
where
    F: Fn(&str, Vec<String>),
{
    let mut fds: HandleMap = register_dir(&option.dir, &option.file_filter)?;

    let (tx, rx) = channel();
    let mut watcher = match option.watcher_type {
        WatcherType::Poll => NotifyWatcherType::PollWatcher(PollWatcher::new(
            tx,
            Duration::from_secs(option.debounce_seconds),
        )?),
        WatcherType::Recommend => NotifyWatcherType::RecommendedWatcher(watcher(
            tx,
            Duration::from_secs(option.debounce_seconds),
        )?),
    };

    watcher.watch(&option.dir, RecursiveMode::Recursive)?;

    loop {
        match rx.recv() {
            Ok(event) => {
                match collect_logs(
                    event,
                    &mut fds,
                    option.file_filter.clone(),
                    option.line_filter.clone(),
                ) {
                    Ok(Some((name, logs))) => callback(&name, logs),
                    Err(e) => println!("watch error: {:?}", e),
                    _ => {}
                }
            }
            Err(e) => println!("watch error: {:?}", e),
        }
    }
}

fn register_dir(dir: &str, filter: &FileFilter) -> Result<(HandleMap), Box<Error>> {
    let mut fds: HandleMap = HashMap::new();
    for entry in WalkDir::new(dir).into_iter().filter_map(|e| e.ok()) {
        let path = entry.path();
        if let Some(name) = path.file_name() {
            if path.is_file() && filter(name.to_str().unwrap()) {
                insert_handle(&mut fds, path, true)?;
            }
        }
    }
    Ok(fds)
}

fn collect_logs(
    event: DebouncedEvent,
    fds: &mut HandleMap,
    file_filter: FileFilter,
    line_filter: LineFilter,
) -> Result<Option<(String, Vec<String>)>, BoxError> {
    // println!("Receive {:?}", event);
    match event {
        DebouncedEvent::Create(p) | DebouncedEvent::NoticeWrite(p) | DebouncedEvent::Write(p) => {
            return collect(fds, &p, file_filter, line_filter)
        }
        DebouncedEvent::NoticeRemove(p) | DebouncedEvent::Remove(p) => remove_handle(fds, &p)?,
        _ => {}
    }
    Ok(None)
}

fn insert_handle(fds: &mut HandleMap, path: &Path, at_tail: bool) -> Result<(), BoxError> {
    let name = path.file_name().unwrap().to_str().unwrap();
    let mut fd = File::open(path).unwrap();
    let meta = &fd.metadata().unwrap();

    let pos = if at_tail { meta.len() } else { 0 };
    fd.seek(SeekFrom::Start(pos))?;

    let handle = Handle {
        pos,
        fd,
        path: path.to_path_buf(),
    };
    println!("Resitered {}, pos: {}", name, pos);
    fds.insert(name.to_string(), handle);
    Ok(())
}

fn remove_handle(fds: &mut HandleMap, path: &Path) -> Result<(), BoxError> {
    let name = path.file_name().unwrap().to_str().unwrap();
    if fds.get(name).is_some() {
        fds.remove(name).unwrap();
    }
    println!("Handle removed, {}", name);
    Ok(())
}

fn collect(
    fds: &mut HandleMap,
    path: &Path,
    file_filter: FileFilter,
    line_filter: LineFilter,
) -> Result<Option<(String, Vec<String>)>, BoxError> {
    match path.file_name() {
        Some(file) => {
            match file.to_str() {
                Some(name) => {
                    if !file_filter(name) {
                        return Ok(None);
                    }
                    let mut handle = match fds.get_mut(name) {
                        Some(handle) => handle,
                        None => {
                            // When rotating
                            // Should trigger `Remove` and `Create`
                            // But we will not get any of them under debouncing mode
                            // So reopen file here
                            insert_handle(fds, path, false)?;
                            println!("File rotated, reopened: {}", name);
                            return collect(fds, path, file_filter, line_filter);
                        }
                    };
                    let meta = &handle.fd.metadata()?;
                    let end = meta.len();

                    let mut logs = Vec::new();
                    while handle.pos < end {
                        let mut reader = BufReader::new(&handle.fd);
                        let mut line = String::new();
                        let len = reader.read_line(&mut line)?;
                        if line_filter(&line) {
                            logs.push(line);
                        }
                        handle.pos += len as u64;
                        handle.fd.seek(SeekFrom::Start(handle.pos))?;
                    }

                    if end < handle.pos {
                        // Reset
                        handle.pos = 0;
                    }
                    Ok(Some((String::from(name), logs)))
                }
                None => Ok(None),
            }
        }
        None => Ok(None),
    }
}