@@ -14,9 +14,9 @@ import scala.util.Properties.isWin
1414class WatchServiceWatcher (
1515 roots : Seq [os.Path ],
1616 onEvent : Set [os.Path ] => Unit ,
17- logger : (String , Any ) => Unit = (_, _) => ()
17+ filter : os.Path => Boolean ,
18+ logger : (String , Any ) => Unit
1819) extends Watcher {
19-
2020 val nioWatchService = FileSystems .getDefault.newWatchService()
2121 val currentlyWatchedPaths = mutable.Map .empty[os.Path , WatchKey ]
2222 val newlyWatchedPaths = mutable.Buffer .empty[os.Path ]
@@ -46,7 +46,7 @@ class WatchServiceWatcher(
4646 modifiers : _*
4747 )
4848 )
49- newlyWatchedPaths.append(p)
49+ if (filter(p)) newlyWatchedPaths.append(p)
5050 }
5151 bufferedEvents.add(p)
5252 }
@@ -61,12 +61,33 @@ class WatchServiceWatcher(
6161
6262 logger(" WATCH KINDS" , events.map(_.kind()))
6363
64+ def logWarning (msg : String ): Unit = {
65+ System .err.println(s " [oslib.watch] (path= $p) $msg" )
66+ }
67+
68+ def logWarningContextNull (e : WatchEvent [_]): Unit = {
69+ logWarning(
70+ s " Context is null for event kind=' ${e.kind().name()}' of class ${e.kind().`type`().getName}, " +
71+ s " this should never happen. "
72+ )
73+ }
74+
6475 for (e <- events) {
65- bufferedEvents.add(p / e.context().toString)
76+ if (e.kind() == OVERFLOW ) {
77+ logWarning(" Overflow detected, some filesystem changes may not be registered." )
78+ } else {
79+ contextSafe(e) match {
80+ case Some (ctx) => bufferedEvents.add(p / ctx.toString)
81+ case None => logWarningContextNull(e)
82+ }
83+ }
6684 }
6785
6886 for (e <- events if e.kind() == ENTRY_CREATE ) {
69- watchSinglePath(p / e.context().toString)
87+ contextSafe(e) match {
88+ case Some (ctx) => watchSinglePath(p / ctx.toString)
89+ case None => logWarningContextNull(e)
90+ }
7091 }
7192
7293 watchKey.reset()
@@ -83,7 +104,8 @@ class WatchServiceWatcher(
83104 val listing =
84105 try os.list(top)
85106 catch {
86- case e : java.nio.file.NotDirectoryException => Nil
107+ case _ : java.nio.file.NotDirectoryException | _ : java.nio.file.NoSuchFileException =>
108+ Nil
87109 }
88110 for (p <- listing) watchSinglePath(p)
89111 bufferedEvents.add(top)
@@ -124,10 +146,10 @@ class WatchServiceWatcher(
124146
125147 } catch {
126148 case e : InterruptedException =>
127- println (" Interrupted, exiting: " + e)
149+ logger (" Interrupted, exiting. " , e)
128150 isRunning.set(false )
129151 case e : ClosedWatchServiceException =>
130- println (" Watcher closed, exiting: " + e)
152+ logger (" Watcher closed, exiting. " , e)
131153 isRunning.set(false )
132154 }
133155 }
@@ -137,7 +159,7 @@ class WatchServiceWatcher(
137159 isRunning.set(false )
138160 nioWatchService.close()
139161 } catch {
140- case e : IOException => println (" Error closing watcher: " + e)
162+ case e : IOException => logger (" Error closing watcher. " , e)
141163 }
142164 }
143165
@@ -146,4 +168,6 @@ class WatchServiceWatcher(
146168 onEvent(bufferedEvents.toSet)
147169 bufferedEvents.clear()
148170 }
171+
172+ def contextSafe [A ](e : WatchEvent [A ]): Option [A ] = Option (e.context())
149173}
0 commit comments