-
Notifications
You must be signed in to change notification settings - Fork 0
/
watcher.go
52 lines (44 loc) · 846 Bytes
/
watcher.go
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
package NeteaseMusicPlaying
import (
"github.com/fsnotify/fsnotify"
"log"
"sync"
)
var PlayingMutex sync.Mutex
func Watch(filePath string) {
//Update()
watch, err := fsnotify.NewWatcher()
if err != nil {
panic(err)
}
defer watch.Close()
done := make(chan struct{})
go func() {
for {
select {
case event, ok := <-watch.Events:
if !ok {
return
}
log.Println("event:", event.Op)
if event.Op == fsnotify.Create {
log.Println("watcher: log file create")
Update()
} else if event.Op == fsnotify.Write {
log.Println("watcher: log file write")
//time.Sleep(time.Second * 1)
Update()
}
case err, ok := <-watch.Errors:
if !ok {
return
}
log.Println("error:", err)
}
}
}()
if err = watch.Add(filePath); err != nil {
log.Fatalln(err)
}
<-done
}