WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 2a21427

Browse files
authored
feat: add tracing logs for Windows backend (#45)
Part of #37
1 parent 099c3b2 commit 2a21427

File tree

1 file changed

+65
-10
lines changed

1 file changed

+65
-10
lines changed

notify/src/windows.rs

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,12 @@ struct ReadDirectoryRequest {
6363

6464
impl ReadDirectoryRequest {
6565
fn unwatch_raw(&self) {
66-
let _ = self
66+
let result = self
6767
.action_tx
6868
.send(Action::UnwatchRaw(self.data.dir.clone()));
69+
if let Err(e) = result {
70+
tracing::error!(?e, "failed to send UnwatchRaw action");
71+
}
6972
}
7073
}
7174

@@ -103,7 +106,7 @@ impl ReadDirectoryChangesServer {
103106
let (action_tx, action_rx) = unbounded();
104107
// it is, in fact, ok to send the semaphore across threads
105108
let sem_temp = wakeup_sem as u64;
106-
let _ = thread::Builder::new()
109+
let result = thread::Builder::new()
107110
.name("notify-rs windows loop".to_string())
108111
.spawn({
109112
let tx = action_tx.clone();
@@ -121,6 +124,9 @@ impl ReadDirectoryChangesServer {
121124
server.run();
122125
}
123126
});
127+
if let Err(e) = result {
128+
tracing::error!(?e, "failed to spawn ReadDirectoryChangesWatcher thread");
129+
}
124130
action_tx
125131
}
126132

@@ -133,7 +139,10 @@ impl ReadDirectoryChangesServer {
133139
match action {
134140
Action::Watch(path, watch_mode) => {
135141
let res = self.add_watch(path, watch_mode);
136-
let _ = self.cmd_tx.send(res);
142+
let result = self.cmd_tx.send(res);
143+
if let Err(e) = result {
144+
tracing::error!(?e, "failed to send Watch result");
145+
}
137146
}
138147
Action::Unwatch(path) => self.remove_watch(path),
139148
Action::UnwatchRaw(path) => self.remove_watch_raw(path),
@@ -171,6 +180,7 @@ impl ReadDirectoryChangesServer {
171180
}
172181
}
173182

183+
#[tracing::instrument(level = "trace", skip(self))]
174184
fn add_watch(&mut self, path: PathBuf, watch_mode: WatchMode) -> Result<PathBuf> {
175185
let existing_watch_mode = self.watches.borrow().get(&path).cloned();
176186
if let Some(existing) = existing_watch_mode {
@@ -184,6 +194,12 @@ impl ReadDirectoryChangesServer {
184194
TargetMode::TrackPath => false,
185195
TargetMode::NoTrack => watch_mode.target_mode == TargetMode::TrackPath,
186196
};
197+
tracing::trace!(
198+
?need_upgrade_to_recursive,
199+
?need_to_watch_parent_newly,
200+
"upgrading existing watch for path: {}",
201+
path.display()
202+
);
187203
if need_to_watch_parent_newly && let Some(parent) = path.parent() {
188204
self.add_watch_raw(parent.to_path_buf(), false, false)?;
189205
}
@@ -246,6 +262,7 @@ impl ReadDirectoryChangesServer {
246262
Ok(path)
247263
}
248264

265+
#[tracing::instrument(level = "trace", skip(self))]
249266
fn add_watch_raw(
250267
&mut self,
251268
path: PathBuf,
@@ -255,8 +272,13 @@ impl ReadDirectoryChangesServer {
255272
if let Some((ws, was_recursive)) = self.watch_handles.get(&path) {
256273
let need_upgrade_to_recursive = !*was_recursive && is_recursive;
257274
if !need_upgrade_to_recursive {
275+
tracing::trace!(
276+
"watch handle already exists and no need to upgrade: {}",
277+
path.display()
278+
);
258279
return Ok(());
259280
}
281+
tracing::trace!("upgrading watch handle to recursive: {}", path.display());
260282
stop_watch(ws);
261283
}
262284

@@ -309,12 +331,14 @@ impl ReadDirectoryChangesServer {
309331
Ok(())
310332
}
311333

334+
#[tracing::instrument(level = "trace", skip(self))]
312335
fn remove_watch(&mut self, path: PathBuf) {
313336
if self.watches.borrow_mut().remove(&path).is_some() {
314337
self.remove_watch_raw(path);
315338
}
316339
}
317340

341+
#[tracing::instrument(level = "trace", skip(self))]
318342
fn remove_watch_raw(&mut self, path: PathBuf) {
319343
if let Some((ws, _)) = self.watch_handles.remove(&path) {
320344
stop_watch(&ws);
@@ -342,6 +366,7 @@ impl ReadDirectoryChangesServer {
342366
}
343367

344368
fn stop_watch(ws: &WatchState) {
369+
tracing::trace!("removing ReadDirectoryChangesW watch");
345370
unsafe {
346371
let cio = CancelIo(ws.dir_handle);
347372
let ch = CloseHandle(ws.dir_handle);
@@ -361,6 +386,8 @@ fn start_read(
361386
handle: HANDLE,
362387
action_tx: Sender<Action>,
363388
) {
389+
tracing::trace!("starting ReadDirectoryChangesW watch: {}", rd.dir.display());
390+
364391
let request = Box::new(ReadDirectoryRequest {
365392
event_handler,
366393
handle,
@@ -431,6 +458,14 @@ unsafe extern "system" fn handle_event(
431458
}
432459
let event_handler = |res| emit_event(&request.event_handler, res);
433460

461+
if error_code != ERROR_SUCCESS {
462+
tracing::trace!(
463+
path = ?request.data.dir,
464+
is_recursive = request.data.is_recursive,
465+
"ReadDirectoryChangesW handle_event called with error code {error_code}",
466+
);
467+
}
468+
434469
match error_code {
435470
ERROR_OPERATION_ABORTED => {
436471
// received when dir is unwatched or watcher is shutdown; return and let overlapped/request get drop-cleaned
@@ -442,6 +477,11 @@ unsafe extern "system" fn handle_event(
442477
// This could happen when the watched directory is deleted or trashed, first check if it's the case.
443478
// If so, unwatch the directory and return, otherwise, continue to handle the event.
444479
if !dir.exists() {
480+
tracing::debug!(
481+
path = ?request.data.dir,
482+
is_recursive = request.data.is_recursive,
483+
"ReadDirectoryChangesW handle_event: ERROR_ACCESS_DENIED event and directory no longer exists",
484+
);
445485
if request
446486
.data
447487
.watches
@@ -516,13 +556,16 @@ unsafe extern "system" fn handle_event(
516556
.contains_key(&request.data.dir)
517557
|| request.data.watches.borrow().contains_key(&path));
518558

519-
if !skip {
520-
tracing::trace!(
521-
"Event: path = `{}`, action = {:?}",
522-
path.display(),
523-
cur_entry.Action
524-
);
559+
tracing::trace!(
560+
handle_path = ?request.data.dir,
561+
is_recursive = request.data.is_recursive,
562+
?path,
563+
skip,
564+
action = cur_entry.Action,
565+
"ReadDirectoryChangesW handle_event called",
566+
);
525567

568+
if !skip {
526569
let newe = Event::new(EventKind::Any).add_path(path.clone());
527570

528571
match cur_entry.Action {
@@ -564,6 +607,11 @@ unsafe extern "system" fn handle_event(
564607
cur_entry = unsafe { ptr::read_unaligned(cur_offset as *const FILE_NOTIFY_INFORMATION) };
565608
}
566609

610+
tracing::trace!(
611+
?remove_paths,
612+
"processing ReadDirectoryChangesW watch changes",
613+
);
614+
567615
for path in remove_paths {
568616
let is_no_track = {
569617
request
@@ -667,19 +715,23 @@ impl ReadDirectoryChangesWatcher {
667715
}
668716

669717
impl Watcher for ReadDirectoryChangesWatcher {
718+
#[tracing::instrument(level = "debug", skip(event_handler))]
670719
fn new<F: EventHandler>(event_handler: F, _config: Config) -> Result<Self> {
671720
let event_handler = Arc::new(Mutex::new(event_handler));
672721
Self::create(event_handler)
673722
}
674723

724+
#[tracing::instrument(level = "debug", skip(self))]
675725
fn watch(&mut self, path: &Path, watch_mode: WatchMode) -> Result<()> {
676726
self.watch_inner(path, watch_mode)
677727
}
678728

729+
#[tracing::instrument(level = "debug", skip(self))]
679730
fn unwatch(&mut self, path: &Path) -> Result<()> {
680731
self.unwatch_inner(path)
681732
}
682733

734+
#[tracing::instrument(level = "debug", skip(self))]
683735
fn configure(&mut self, config: Config) -> Result<bool> {
684736
let (tx, rx) = bounded(1);
685737
self.tx.send(Action::Configure(config, tx))?;
@@ -700,7 +752,10 @@ impl Watcher for ReadDirectoryChangesWatcher {
700752

701753
impl Drop for ReadDirectoryChangesWatcher {
702754
fn drop(&mut self) {
703-
let _ = self.tx.send(Action::Stop);
755+
let result = self.tx.send(Action::Stop);
756+
if let Err(e) = result {
757+
tracing::error!(?e, "failed to send Stop action");
758+
}
704759
// better wake it up
705760
self.wakeup_server();
706761
}

0 commit comments

Comments
 (0)