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

Add logging #499

Merged
merged 3 commits into from
Jul 13, 2023
Merged
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
6 changes: 6 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ notify-debouncer-mini = { version = "0.3.0", path = "../notify-debouncer-mini" }
notify-debouncer-full = { version = "0.2.0", path = "../notify-debouncer-full" }
futures = "0.3"
tempfile = "3.5.0"
log = "0.4.17"
env_logger = "0.10.0"

[[example]]
name = "async_monitor"
Expand All @@ -19,6 +21,10 @@ path = "async_monitor.rs"
name = "monitor_raw"
path = "monitor_raw.rs"

[[example]]
name = "monitor_debounced"
path = "monitor_debounced.rs"

[[example]]
name = "debouncer_mini"
path = "debouncer_mini.rs"
Expand Down
49 changes: 49 additions & 0 deletions examples/monitor_debounced.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use notify::{RecursiveMode, Watcher};
use notify_debouncer_full::new_debouncer;
use std::{path::Path, time::Duration};

fn main() {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();

let path = std::env::args()
.nth(1)
.expect("Argument 1 needs to be a path");

log::info!("Watching {path}");

if let Err(error) = watch(path) {
log::error!("Error: {error:?}");
}
}

fn watch<P: AsRef<Path>>(path: P) -> notify::Result<()> {
let (tx, rx) = std::sync::mpsc::channel();

// Create a new debounced file watcher with a timeout of 2 seconds.
// The tickrate will be selected automatically, as well as the underlying watch implementation.
let mut debouncer = new_debouncer(Duration::from_secs(2), None, tx)?;

// Add a path to be watched. All files and directories at that path and
// below will be monitored for changes.
debouncer
.watcher()
.watch(path.as_ref(), RecursiveMode::Recursive)?;

// Initialize the file id cache for the same path. This will allow the debouncer to stitch together move events,
// even if the underlying watch implementation doesn't support it.
// Without the cache and with some watch implementations,
// you may receive `move from` and `move to` events instead of one `move both` event.
debouncer
.cache()
.add_root(path.as_ref(), RecursiveMode::Recursive);

// print all events and errors
for result in rx {
match result {
Ok(events) => events.iter().for_each(|event| log::info!("{event:?}")),
Err(errors) => errors.iter().for_each(|error| log::error!("{error:?}")),
}
}

Ok(())
}
14 changes: 9 additions & 5 deletions examples/monitor_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};
use std::path::Path;

fn main() {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();

let path = std::env::args()
.nth(1)
.expect("Argument 1 needs to be a path");
println!("watching {}", path);
if let Err(e) = watch(path) {
println!("error: {:?}", e)

log::info!("Watching {path}");

if let Err(error) = watch(path) {
log::error!("Error: {error:?}");
}
}

Expand All @@ -24,8 +28,8 @@ fn watch<P: AsRef<Path>>(path: P) -> notify::Result<()> {

for res in rx {
match res {
Ok(event) => println!("changed: {:?}", event),
Err(e) => println!("watch error: {:?}", e),
Ok(event) => log::info!("Change: {event:?}"),
Err(error) => log::error!("Error: {error:?}"),
}
}

Expand Down
1 change: 1 addition & 0 deletions notify-debouncer-full/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ crossbeam-channel = { version = "0.5", optional = true }
file-id = { version = "0.1.0", path = "../file-id" }
walkdir = "2.2.2"
parking_lot = "0.12.1"
log = "0.4.17"

[dev-dependencies]
pretty_assertions = "1.3.0"
Expand Down
3 changes: 3 additions & 0 deletions notify-debouncer-full/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ impl<T: FileIdCache> DebounceDataInner<T> {

if let Some(event) = self.rescan_event.take() {
if now.saturating_duration_since(event.time) >= self.timeout {
log::trace!("debounced event: {event:?}");
events_expired.push(event);
} else {
self.rescan_event = Some(event);
Expand Down Expand Up @@ -270,6 +271,8 @@ impl<T: FileIdCache> DebounceDataInner<T> {

/// Add new event to debouncer cache
pub fn add_event(&mut self, event: Event) {
log::trace!("raw event: {event:?}");

if event.need_rescan() {
self.cache.rescan();
self.rescan_event = Some(event.into());
Expand Down
1 change: 1 addition & 0 deletions notify-debouncer-mini/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ crossbeam = ["crossbeam-channel","notify/crossbeam-channel"]
notify = { version = "6.0.1", path = "../notify" }
crossbeam-channel = { version = "0.5", optional = true }
serde = { version = "1.0.89", features = ["derive"], optional = true }
log = "0.4.17"
4 changes: 4 additions & 0 deletions notify-debouncer-mini/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,10 @@ impl DebounceDataInner {
// TODO: perfect fit for drain_filter https://github.com/rust-lang/rust/issues/59618
for (k, v) in self.d.drain() {
if v.update.elapsed() >= self.timeout {
log::trace!("debounced event: {:?}", DebouncedEventKind::Any);
events_expired.push(DebouncedEvent::new(k, DebouncedEventKind::Any));
} else if v.insert.elapsed() >= self.timeout {
log::trace!("debounced event: {:?}", DebouncedEventKind::AnyContinuous);
data_back.insert(k.clone(), v);
events_expired.push(DebouncedEvent::new(k, DebouncedEventKind::AnyContinuous));
} else {
Expand All @@ -208,6 +210,8 @@ impl DebounceDataInner {

/// Add new event to debouncer cache
pub fn add_event(&mut self, e: Event) {
log::trace!("raw event: {e:?}");

for path in e.paths.into_iter() {
if let Some(v) = self.d.get_mut(&path) {
v.update = Instant::now();
Expand Down
1 change: 1 addition & 0 deletions notify/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ bitflags = "1.0.4"
crossbeam-channel = { version = "0.5.0", optional = true }
filetime = "0.2.6"
libc = "0.2.4"
log = "0.4.17"
serde = { version = "1.0.89", features = ["derive"], optional = true }
walkdir = "2.2.2"

Expand Down
2 changes: 2 additions & 0 deletions notify/src/fsevent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,8 @@ unsafe fn callback_impl(
continue;
}

log::trace!("FSEvent: path = `{}`, flag = {:?}", path.display(), flag);

for ev in translate_flags(flag, true).into_iter() {
// TODO: precise
let ev = ev.add_path(path.clone());
Expand Down
6 changes: 6 additions & 0 deletions notify/src/inotify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ impl EventLoop {
Ok(events) => {
let mut num_events = 0;
for event in events {
log::trace!("inotify event: {event:?}");

num_events += 1;
if event.mask.contains(EventMask::Q_OVERFLOW) {
let ev = Ok(Event::new(EventKind::Other).set_flag(Flag::Rescan));
Expand Down Expand Up @@ -407,6 +409,8 @@ impl EventLoop {
}

if let Some(ref mut inotify) = self.inotify {
log::trace!("adding inotify watch: {}", path.display());

match inotify.add_watch(&path, watchmask) {
Err(e) => {
Err(if e.raw_os_error() == Some(libc::ENOSPC) {
Expand Down Expand Up @@ -435,6 +439,8 @@ impl EventLoop {
None => return Err(Error::watch_not_found().add_path(path)),
Some((w, _, is_recursive)) => {
if let Some(ref mut inotify) = self.inotify {
log::trace!("removing inotify watch: {}", path.display());

inotify
.rm_watch(w.clone())
.map_err(|e| Error::io(e).add_path(path.clone()))?;
Expand Down
6 changes: 6 additions & 0 deletions notify/src/kqueue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ impl EventLoop {
let mut remove_watches = Vec::new();

while let Some(event) = self.kqueue.poll(None) {
log::trace!("kqueue event: {event:?}");

match event {
kqueue::Event {
data: EventData::Vnode(data),
Expand Down Expand Up @@ -315,6 +317,8 @@ impl EventLoop {
| FilterFlag::NOTE_RENAME
| FilterFlag::NOTE_REVOKE;

log::trace!("adding kqueue watch: {}", path.display());

self.kqueue
.add_filename(&path, event_filter, filter_flags)
.map_err(|e| Error::io(e).add_path(path.clone()))?;
Expand All @@ -324,6 +328,8 @@ impl EventLoop {
}

fn remove_watch(&mut self, path: PathBuf, remove_recursive: bool) -> Result<()> {
log::trace!("removing kqueue watch: {}", path.display());

match self.watches.remove(&path) {
None => return Err(Error::watch_not_found()),
Some(is_recursive) => {
Expand Down
2 changes: 2 additions & 0 deletions notify/src/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ unsafe extern "system" fn handle_event(
};

if !skip {
log::trace!("Event: path = `{}`, action = {:?}", path.display(), (*cur_entry).Action);

let newe = Event::new(EventKind::Any).add_path(path);

fn emit_event(event_handler: &Mutex<dyn EventHandler>, res: Result<Event>) {
Expand Down