Skip to content

Commit da20205

Browse files
authored
feat: add tracing logs for kqueue backend (#46)
Part of #37
1 parent 2a21427 commit da20205

File tree

1 file changed

+52
-7
lines changed

1 file changed

+52
-7
lines changed

notify/src/kqueue.rs

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,12 @@ impl EventLoop {
8989

9090
// Run the event loop.
9191
pub fn run(self) {
92-
let _ = thread::Builder::new()
92+
let result = thread::Builder::new()
9393
.name("notify-rs kqueue loop".to_string())
9494
.spawn(|| self.event_loop_thread());
95+
if let Err(e) = result {
96+
tracing::error!(?e, "failed to start kqueue event loop thread");
97+
}
9598
}
9699

97100
fn event_loop_thread(mut self) {
@@ -138,13 +141,22 @@ impl EventLoop {
138141
while let Ok(msg) = self.event_loop_rx.try_recv() {
139142
match msg {
140143
EventLoopMsg::AddWatch(path, watch_mode, tx) => {
141-
let _ = tx.send(self.add_watch(path, watch_mode));
144+
let result = tx.send(self.add_watch(path, watch_mode));
145+
if let Err(e) = result {
146+
tracing::error!(?e, "failed to send AddWatch result");
147+
}
142148
}
143149
EventLoopMsg::AddWatchMultiple(paths, tx) => {
144-
let _ = tx.send(self.add_watch_multiple(paths));
150+
let result = tx.send(self.add_watch_multiple(paths));
151+
if let Err(e) = result {
152+
tracing::error!(?e, "failed to send AddWatchMultiple result");
153+
}
145154
}
146155
EventLoopMsg::RemoveWatch(path, tx) => {
147-
let _ = tx.send(self.remove_watch(path));
156+
let result = tx.send(self.remove_watch(path));
157+
if let Err(e) = result {
158+
tracing::error!(?e, "failed to send RemoveWatch result");
159+
}
148160
}
149161
EventLoopMsg::Shutdown => {
150162
self.running = false;
@@ -188,7 +200,7 @@ impl EventLoop {
188200
let mut remove_watches = Vec::new();
189201

190202
while let Some(event) = self.kqueue.poll(None) {
191-
tracing::trace!("kqueue event: {event:?}");
203+
tracing::trace!(?event, "kqueue event received");
192204

193205
match event {
194206
kqueue::Event {
@@ -216,6 +228,7 @@ impl EventLoop {
216228
// in that case, emit a create event for the new file
217229
let is_dir = metadata.is_dir();
218230
add_watches.push((path.clone(), is_dir));
231+
tracing::trace!("overwrite detected: {}", path.display());
219232
self.event_handler.handle_event(Ok(remove_event));
220233
Some(Ok(Event::new(EventKind::Create(if is_dir {
221234
CreateKind::Folder
@@ -245,6 +258,10 @@ impl EventLoop {
245258
.find(|f| !self.watch_handles.contains(f))
246259
})
247260
.map(|file| {
261+
tracing::trace!(
262+
"new file detected: {:?}",
263+
file.as_ref().map(|f| f.display())
264+
);
248265
if let Some(file) = file {
249266
let metadata = file.metadata();
250267
let is_dir = metadata
@@ -385,6 +402,12 @@ impl EventLoop {
385402
}
386403
}
387404

405+
tracing::trace!(
406+
?add_watches,
407+
?remove_watches,
408+
"processing kqueue watch changes"
409+
);
410+
388411
for path in remove_watches {
389412
if self
390413
.watches
@@ -412,6 +435,7 @@ impl EventLoop {
412435
self.kqueue.watch().unwrap();
413436
}
414437

438+
#[tracing::instrument(level = "trace", skip(self))]
415439
fn add_watch(&mut self, path: PathBuf, watch_mode: WatchMode) -> Result<()> {
416440
self.add_watch_inner(path, watch_mode)?;
417441

@@ -421,6 +445,7 @@ impl EventLoop {
421445
Ok(())
422446
}
423447

448+
#[tracing::instrument(level = "trace", skip(self))]
424449
fn add_watch_multiple(&mut self, paths: Vec<(PathBuf, WatchMode)>) -> Result<()> {
425450
for (path, watch_mode) in paths {
426451
self.add_watch_inner(path, watch_mode)?;
@@ -433,6 +458,7 @@ impl EventLoop {
433458
}
434459

435460
/// The caller of this function must call `self.kqueue.watch()` afterwards to register the new watch.
461+
#[tracing::instrument(level = "trace", skip(self))]
436462
fn add_watch_inner(&mut self, path: PathBuf, watch_mode: WatchMode) -> Result<()> {
437463
if let Some(existing) = self.watches.get(&path) {
438464
let need_upgrade_to_recursive = match existing.recursive_mode {
@@ -445,6 +471,12 @@ impl EventLoop {
445471
TargetMode::TrackPath => false,
446472
TargetMode::NoTrack => watch_mode.target_mode == TargetMode::TrackPath,
447473
};
474+
tracing::trace!(
475+
?need_to_watch_parent_newly,
476+
?need_upgrade_to_recursive,
477+
"upgrading existing watch for path: {}",
478+
path.display()
479+
);
448480
if need_to_watch_parent_newly && let Some(parent) = path.parent() {
449481
self.add_single_watch(parent.to_path_buf())?;
450482
}
@@ -495,6 +527,7 @@ impl EventLoop {
495527
}
496528

497529
/// The caller of this function must call `self.kqueue.watch()` afterwards to register the new watch.
530+
#[tracing::instrument(level = "trace", skip(self))]
498531
fn add_maybe_recursive_watch(
499532
&mut self,
500533
path: PathBuf,
@@ -525,8 +558,10 @@ impl EventLoop {
525558
/// Adds a single watch to the kqueue.
526559
///
527560
/// The caller of this function must call `self.kqueue.watch()` afterwards to register the new watch.
561+
#[tracing::instrument(level = "trace", skip(self))]
528562
fn add_single_watch(&mut self, path: PathBuf) -> Result<()> {
529563
if self.watch_handles.contains(&path) {
564+
tracing::trace!("watch handle already exists: {}", path.display());
530565
return Ok(());
531566
}
532567

@@ -549,9 +584,8 @@ impl EventLoop {
549584
Ok(())
550585
}
551586

587+
#[tracing::instrument(level = "trace", skip(self))]
552588
fn remove_watch(&mut self, path: PathBuf) -> Result<()> {
553-
tracing::trace!("removing kqueue watch: {}", path.display());
554-
555589
match self.watches.remove(&path) {
556590
None => return Err(Error::watch_not_found()),
557591
Some(watch_mode) => {
@@ -564,6 +598,7 @@ impl EventLoop {
564598
}
565599

566600
/// The caller of this function must call `self.kqueue.watch()` afterwards to register the new watch.
601+
#[tracing::instrument(level = "trace", skip(self))]
567602
fn remove_maybe_recursive_watch(&mut self, path: PathBuf, is_recursive: bool) -> Result<()> {
568603
if is_recursive {
569604
self.remove_single_watch(path.clone())?;
@@ -586,7 +621,10 @@ impl EventLoop {
586621
/// Removes a single watch from the kqueue.
587622
///
588623
/// The caller of this function must call `self.kqueue.watch()` afterwards to unregister the old watch.
624+
#[tracing::instrument(level = "trace", skip(self))]
589625
fn remove_single_watch(&mut self, path: PathBuf) -> Result<()> {
626+
tracing::trace!("removing kqueue watch: {}", path.display());
627+
590628
self.kqueue
591629
.remove_filename(&path, EventFilter::EVFILT_VNODE)
592630
.map_err(|e| Error::io(e).add_path(path.clone()))?;
@@ -622,15 +660,18 @@ impl<'a> KqueuePathsMut<'a> {
622660
}
623661
}
624662
impl PathsMut for KqueuePathsMut<'_> {
663+
#[tracing::instrument(level = "debug", skip(self))]
625664
fn add(&mut self, path: &Path, watch_mode: WatchMode) -> Result<()> {
626665
self.add_paths.push((path.to_owned(), watch_mode));
627666
Ok(())
628667
}
629668

669+
#[tracing::instrument(level = "debug", skip(self))]
630670
fn remove(&mut self, path: &Path) -> Result<()> {
631671
self.inner.unwatch_inner(path)
632672
}
633673

674+
#[tracing::instrument(level = "debug", skip(self))]
634675
fn commit(self: Box<Self>) -> Result<()> {
635676
let paths = self.add_paths;
636677
self.inner.watch_multiple_inner(paths)
@@ -719,18 +760,22 @@ impl KqueueWatcher {
719760

720761
impl Watcher for KqueueWatcher {
721762
/// Create a new watcher.
763+
#[tracing::instrument(level = "debug", skip(event_handler))]
722764
fn new<F: EventHandler>(event_handler: F, config: Config) -> Result<Self> {
723765
Self::from_event_handler(Box::new(event_handler), config.follow_symlinks())
724766
}
725767

768+
#[tracing::instrument(level = "debug", skip(self))]
726769
fn watch(&mut self, path: &Path, watch_mode: WatchMode) -> Result<()> {
727770
self.watch_inner(path, watch_mode)
728771
}
729772

773+
#[tracing::instrument(level = "debug", skip(self))]
730774
fn paths_mut<'me>(&'me mut self) -> Box<dyn PathsMut + 'me> {
731775
Box::new(KqueuePathsMut::new(self))
732776
}
733777

778+
#[tracing::instrument(level = "debug", skip(self))]
734779
fn unwatch(&mut self, path: &Path) -> Result<()> {
735780
self.unwatch_inner(path)
736781
}

0 commit comments

Comments
 (0)