Skip to content

Commit 5def6d9

Browse files
committed
Make the watch logger less chatty
1 parent 3054a46 commit 5def6d9

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

commands/hugo.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
"regexp"
3232

33+
"github.com/spf13/afero"
3334
"github.com/spf13/cobra"
3435
"github.com/spf13/fsync"
3536
"github.com/spf13/hugo/helpers"
@@ -42,7 +43,6 @@ import (
4243
"github.com/spf13/nitro"
4344
"github.com/spf13/viper"
4445
"gopkg.in/fsnotify.v1"
45-
"github.com/spf13/afero"
4646
)
4747

4848
var mainSite *hugolib.Site
@@ -654,7 +654,7 @@ func NewWatcher(port int) error {
654654
continue
655655
}
656656

657-
walkAdder := func (path string, f os.FileInfo, err error) error {
657+
walkAdder := func(path string, f os.FileInfo, err error) error {
658658
if f.IsDir() {
659659
jww.FEEDBACK.Println("adding created directory to watchlist", path)
660660
watcher.Add(path)
@@ -687,7 +687,7 @@ func NewWatcher(port int) error {
687687
publishDir = helpers.FilePathSeparator
688688
}
689689

690-
jww.FEEDBACK.Println("\n Static file changes detected")
690+
jww.FEEDBACK.Println("\nStatic file changes detected")
691691
const layout = "2006-01-02 15:04 -0700"
692692
fmt.Println(time.Now().Format(layout))
693693

@@ -710,6 +710,8 @@ func NewWatcher(port int) error {
710710
syncer.SrcFs = staticSourceFs
711711
syncer.DestFs = hugofs.DestinationFS
712712

713+
// prevent spamming the log on changes
714+
logger := helpers.NewDistinctFeedbackLogger()
713715

714716
for _, ev := range staticEvents {
715717
// Due to our approach of layering both directories and the content's rendered output
@@ -727,7 +729,6 @@ func NewWatcher(port int) error {
727729
// Hugo assumes that these cases are very rare and will permit this bad behavior
728730
// The alternative is to track every single file and which pipeline rendered it
729731
// and then to handle conflict resolution on every event.
730-
fmt.Println(ev)
731732

732733
fromPath := ev.Name
733734

@@ -750,12 +751,12 @@ func NewWatcher(port int) error {
750751
if ev.Op&fsnotify.Rename == fsnotify.Rename || ev.Op&fsnotify.Remove == fsnotify.Remove {
751752
if _, err := staticSourceFs.Stat(relPath); os.IsNotExist(err) {
752753
// If file doesn't exist in any static dir, remove it
753-
toRemove :=filepath.Join(publishDir, relPath)
754-
jww.FEEDBACK.Println("File no longer exists in static dir, removing", toRemove)
754+
toRemove := filepath.Join(publishDir, relPath)
755+
logger.Println("File no longer exists in static dir, removing", toRemove)
755756
hugofs.DestinationFS.RemoveAll(toRemove)
756757
} else if err == nil {
757758
// If file still exists, sync it
758-
jww.FEEDBACK.Println("Syncing", relPath, "to", publishDir)
759+
logger.Println("Syncing", relPath, "to", publishDir)
759760
syncer.Sync(filepath.Join(publishDir, relPath), relPath)
760761
} else {
761762
jww.ERROR.Println(err)
@@ -765,7 +766,7 @@ func NewWatcher(port int) error {
765766
}
766767

767768
// For all other event operations Hugo will sync static.
768-
jww.FEEDBACK.Println("Syncing", relPath, "to", publishDir)
769+
logger.Println("Syncing", relPath, "to", publishDir)
769770
syncer.Sync(filepath.Join(publishDir, relPath), relPath)
770771
}
771772
}

helpers/general.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ func ThemeSet() bool {
182182
}
183183

184184
type logPrinter interface {
185+
// Println is the only common method that works in all of JWWs loggers.
185186
Println(a ...interface{})
186187
}
187188

@@ -192,10 +193,23 @@ type DistinctLogger struct {
192193
m map[string]bool
193194
}
194195

196+
// Println will log the string returned from fmt.Sprintln given the arguments,
197+
// but not if it has been logged before.
198+
func (l *DistinctLogger) Println(v ...interface{}) {
199+
// fmt.Sprint doesn't add space between string arguments
200+
logStatement := strings.TrimSpace(fmt.Sprintln(v...))
201+
l.print(logStatement)
202+
}
203+
195204
// Printf will log the string returned from fmt.Sprintf given the arguments,
196205
// but not if it has been logged before.
206+
// Note: A newline is appended.
197207
func (l *DistinctLogger) Printf(format string, v ...interface{}) {
198208
logStatement := fmt.Sprintf(format, v...)
209+
l.print(logStatement)
210+
}
211+
212+
func (l *DistinctLogger) print(logStatement string) {
199213
l.RLock()
200214
if l.m[logStatement] {
201215
l.RUnlock()
@@ -207,7 +221,6 @@ func (l *DistinctLogger) Printf(format string, v ...interface{}) {
207221
if !l.m[logStatement] {
208222
l.logger.Println(logStatement)
209223
l.m[logStatement] = true
210-
fmt.Println()
211224
}
212225
l.Unlock()
213226
}
@@ -217,6 +230,12 @@ func NewDistinctErrorLogger() *DistinctLogger {
217230
return &DistinctLogger{m: make(map[string]bool), logger: jww.ERROR}
218231
}
219232

233+
// NewDistinctErrorLogger creates a new DistinctLogger that can be used
234+
// to give feedback to the user while not spamming with duplicates.
235+
func NewDistinctFeedbackLogger() *DistinctLogger {
236+
return &DistinctLogger{m: make(map[string]bool), logger: &jww.FEEDBACK}
237+
}
238+
220239
// Avoid spamming the logs with errors
221240
var DistinctErrorLog = NewDistinctErrorLogger()
222241

hugolib/site.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"sync/atomic"
3232

3333
"bitbucket.org/pkg/inflect"
34+
"github.com/spf13/afero"
3435
"github.com/spf13/cast"
3536
bp "github.com/spf13/hugo/bufferpool"
3637
"github.com/spf13/hugo/helpers"
@@ -44,7 +45,6 @@ import (
4445
"github.com/spf13/nitro"
4546
"github.com/spf13/viper"
4647
"gopkg.in/fsnotify.v1"
47-
"github.com/spf13/afero"
4848
)
4949

5050
var _ = transform.AbsURL
@@ -438,19 +438,22 @@ func (s *Site) ReBuild(events []fsnotify.Event) error {
438438

439439
var err error
440440

441+
// prevent spamming the log on changes
442+
logger := helpers.NewDistinctFeedbackLogger()
443+
441444
for _, ev := range events {
442445
// Need to re-read source
443446
if strings.HasPrefix(ev.Name, s.absContentDir()) {
444-
fmt.Println("Source changed", ev)
447+
logger.Println("Source changed", ev.Name)
445448
sourceChanged = append(sourceChanged, ev)
446449
}
447450
if strings.HasPrefix(ev.Name, s.absLayoutDir()) || strings.HasPrefix(ev.Name, s.absThemeDir()) {
448-
fmt.Println("Template changed", ev)
451+
logger.Println("Template changed", ev.Name)
449452
tmplChanged = append(tmplChanged, ev)
450453
}
451454
if strings.HasPrefix(ev.Name, s.absDataDir()) {
452-
fmt.Println("Data changed", ev)
453-
dataChanged = append(dataChanged,ev)
455+
logger.Println("Data changed", ev.Name)
456+
dataChanged = append(dataChanged, ev)
454457
}
455458
}
456459

@@ -502,7 +505,7 @@ func (s *Site) ReBuild(events []fsnotify.Event) error {
502505

503506
for _, ev := range sourceChanged {
504507

505-
if ev.Op&fsnotify.Remove == fsnotify.Remove {
508+
if ev.Op&fsnotify.Remove == fsnotify.Remove {
506509
//remove the file & a create will follow
507510
path, _ := helpers.GetRelativePath(ev.Name, s.absContentDir())
508511
s.RemovePageByPath(path)
@@ -1027,7 +1030,6 @@ func (s *Site) AddPage(page *Page) {
10271030
}
10281031
}
10291032

1030-
10311033
func (s *Site) RemovePageByPath(path string) {
10321034
if i := s.Pages.FindPagePosByFilePath(path); i >= 0 {
10331035
page := s.Pages[i]

0 commit comments

Comments
 (0)