aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMartin Polden <mpolden@mpolden.no>2021-09-11 10:26:39 +0200
committerMartin Polden <mpolden@mpolden.no>2021-09-11 10:26:39 +0200
commit056dd7b3803c370b71396c63b2ef45719cdc88ed (patch)
tree9f7f9cf11af356f9be78b7089a48bd71169a44da
parent44d4b2545bd472bbe0abd3eab286417b9f795cf0 (diff)
watcher: Simplify logger configuration
-rw-r--r--cmd/unp/main.go19
-rw-r--r--watcher/watcher.go25
-rw-r--r--watcher/watcher_test.go4
3 files changed, 25 insertions, 23 deletions
diff --git a/cmd/unp/main.go b/cmd/unp/main.go
index 2adcdd8..0b08820 100644
--- a/cmd/unp/main.go
+++ b/cmd/unp/main.go
@@ -4,22 +4,28 @@ import (
"flag"
"fmt"
"log"
- "os"
"github.com/mpolden/unp/watcher"
)
+func init() {
+ log.SetPrefix("unp: ")
+ log.SetFlags(0)
+}
+
func main() {
- configFile := flag.String("f", "~/.unprc", "Path to config file")
- test := flag.Bool("t", false, "Test and print config")
+ var test bool
+ var configFile string
+ flag.StringVar(&configFile, "f", "~/.unprc", "Path to config file")
+ flag.BoolVar(&test, "t", false, "Test and print config")
flag.Parse()
- cfg, err := watcher.ReadConfig(*configFile)
+ cfg, err := watcher.ReadConfig(configFile)
if err != nil {
log.Fatal(err)
}
- if *test {
+ if test {
json, err := cfg.JSON()
if err != nil {
log.Fatal(err)
@@ -28,7 +34,6 @@ func main() {
return
}
- log := log.New(os.Stderr, "unp: ", 0)
- w := watcher.New(cfg, log)
+ w := watcher.New(cfg)
w.Start()
}
diff --git a/watcher/watcher.go b/watcher/watcher.go
index 0d62030..8f837e1 100644
--- a/watcher/watcher.go
+++ b/watcher/watcher.go
@@ -2,6 +2,7 @@ package watcher
import (
"fmt"
+ "log"
"os"
"os/signal"
"sync"
@@ -9,8 +10,6 @@ import (
"path/filepath"
- "log"
-
"github.com/mpolden/unp/executil"
"github.com/mpolden/unp/pathutil"
"github.com/rjeczalik/notify"
@@ -38,7 +37,6 @@ type Watcher struct {
events chan notify.EventInfo
signal chan os.Signal
done chan bool
- log *log.Logger
mu sync.Mutex
wg sync.WaitGroup
}
@@ -69,9 +67,9 @@ func (w *Watcher) watch() {
for _, path := range w.config.Paths {
rpath := filepath.Join(path.Name, "...")
if err := notify.Watch(rpath, w.events, notifyFlag); err != nil {
- w.log.Printf("failed to watch %s: %s", rpath, err)
+ log.Printf("failed to watch %s: %s", rpath, err)
} else {
- w.log.Printf("watching %s recursively", path.Name)
+ log.Printf("watching %s recursively", path.Name)
}
}
}
@@ -83,7 +81,7 @@ func (w *Watcher) reload() {
w.config = cfg
w.watch()
} else {
- w.log.Printf("failed to read config: %s", err)
+ log.Printf("failed to read config: %s", err)
}
}
@@ -97,12 +95,12 @@ func (w *Watcher) rescan() {
return nil
}
if err := w.handle(path); err != nil {
- w.log.Print(err)
+ log.Print(err)
}
return nil
})
if err != nil {
- w.log.Printf("failed to rescan %s: %s", p.Name, err)
+ log.Printf("failed to rescan %s: %s", p.Name, err)
}
}
}
@@ -116,13 +114,13 @@ func (w *Watcher) readSignal() {
w.mu.Lock()
switch s {
case syscall.SIGUSR1:
- w.log.Printf("received %s: rescanning watched directories", s)
+ log.Printf("received %s: rescanning watched directories", s)
w.rescan()
case syscall.SIGUSR2:
- w.log.Printf("received %s: reloading configuration", s)
+ log.Printf("received %s: reloading configuration", s)
w.reload()
case syscall.SIGTERM, syscall.SIGINT:
- w.log.Printf("received %s: shutting down", s)
+ log.Printf("received %s: shutting down", s)
w.Stop()
}
w.mu.Unlock()
@@ -138,7 +136,7 @@ func (w *Watcher) readEvent() {
case ev := <-w.events:
w.mu.Lock()
if err := w.handle(ev.Path()); err != nil {
- w.log.Print(err)
+ log.Print(err)
}
w.mu.Unlock()
}
@@ -169,7 +167,7 @@ func (w *Watcher) Stop() {
w.done <- true
}
-func New(cfg Config, log *log.Logger) *Watcher {
+func New(cfg Config) *Watcher {
// Buffer events so that we don't miss any
events := make(chan notify.EventInfo, cfg.BufferSize)
sig := make(chan os.Signal, 1)
@@ -178,7 +176,6 @@ func New(cfg Config, log *log.Logger) *Watcher {
return &Watcher{
config: cfg,
events: events,
- log: log,
signal: sig,
done: done,
}
diff --git a/watcher/watcher_test.go b/watcher/watcher_test.go
index 6920231..ba88902 100644
--- a/watcher/watcher_test.go
+++ b/watcher/watcher_test.go
@@ -42,8 +42,8 @@ func testWatcher(dir string, handler Handler) *Watcher {
BufferSize: 10,
Paths: []Path{{handler: handler, Name: dir, MaxDepth: 100, Patterns: []string{"*"}}},
}
- log := log.New(io.Discard, "", 0)
- return New(cfg, log)
+ log.SetOutput(io.Discard)
+ return New(cfg)
}
func TestWatching(t *testing.T) {