Make the watch logger less chatty

This commit is contained in:
Bjørn Erik Pedersen 2016-01-28 15:31:25 +01:00
parent 3054a46185
commit 5def6d9aee
3 changed files with 38 additions and 16 deletions

View file

@ -30,6 +30,7 @@ import (
"regexp" "regexp"
"github.com/spf13/afero"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/fsync" "github.com/spf13/fsync"
"github.com/spf13/hugo/helpers" "github.com/spf13/hugo/helpers"
@ -42,7 +43,6 @@ import (
"github.com/spf13/nitro" "github.com/spf13/nitro"
"github.com/spf13/viper" "github.com/spf13/viper"
"gopkg.in/fsnotify.v1" "gopkg.in/fsnotify.v1"
"github.com/spf13/afero"
) )
var mainSite *hugolib.Site var mainSite *hugolib.Site
@ -654,7 +654,7 @@ func NewWatcher(port int) error {
continue continue
} }
walkAdder := func (path string, f os.FileInfo, err error) error { walkAdder := func(path string, f os.FileInfo, err error) error {
if f.IsDir() { if f.IsDir() {
jww.FEEDBACK.Println("adding created directory to watchlist", path) jww.FEEDBACK.Println("adding created directory to watchlist", path)
watcher.Add(path) watcher.Add(path)
@ -687,7 +687,7 @@ func NewWatcher(port int) error {
publishDir = helpers.FilePathSeparator publishDir = helpers.FilePathSeparator
} }
jww.FEEDBACK.Println("\n Static file changes detected") jww.FEEDBACK.Println("\nStatic file changes detected")
const layout = "2006-01-02 15:04 -0700" const layout = "2006-01-02 15:04 -0700"
fmt.Println(time.Now().Format(layout)) fmt.Println(time.Now().Format(layout))
@ -710,6 +710,8 @@ func NewWatcher(port int) error {
syncer.SrcFs = staticSourceFs syncer.SrcFs = staticSourceFs
syncer.DestFs = hugofs.DestinationFS syncer.DestFs = hugofs.DestinationFS
// prevent spamming the log on changes
logger := helpers.NewDistinctFeedbackLogger()
for _, ev := range staticEvents { for _, ev := range staticEvents {
// Due to our approach of layering both directories and the content's rendered output // Due to our approach of layering both directories and the content's rendered output
@ -727,7 +729,6 @@ func NewWatcher(port int) error {
// Hugo assumes that these cases are very rare and will permit this bad behavior // Hugo assumes that these cases are very rare and will permit this bad behavior
// The alternative is to track every single file and which pipeline rendered it // The alternative is to track every single file and which pipeline rendered it
// and then to handle conflict resolution on every event. // and then to handle conflict resolution on every event.
fmt.Println(ev)
fromPath := ev.Name fromPath := ev.Name
@ -750,12 +751,12 @@ func NewWatcher(port int) error {
if ev.Op&fsnotify.Rename == fsnotify.Rename || ev.Op&fsnotify.Remove == fsnotify.Remove { if ev.Op&fsnotify.Rename == fsnotify.Rename || ev.Op&fsnotify.Remove == fsnotify.Remove {
if _, err := staticSourceFs.Stat(relPath); os.IsNotExist(err) { if _, err := staticSourceFs.Stat(relPath); os.IsNotExist(err) {
// If file doesn't exist in any static dir, remove it // If file doesn't exist in any static dir, remove it
toRemove :=filepath.Join(publishDir, relPath) toRemove := filepath.Join(publishDir, relPath)
jww.FEEDBACK.Println("File no longer exists in static dir, removing", toRemove) logger.Println("File no longer exists in static dir, removing", toRemove)
hugofs.DestinationFS.RemoveAll(toRemove) hugofs.DestinationFS.RemoveAll(toRemove)
} else if err == nil { } else if err == nil {
// If file still exists, sync it // If file still exists, sync it
jww.FEEDBACK.Println("Syncing", relPath, "to", publishDir) logger.Println("Syncing", relPath, "to", publishDir)
syncer.Sync(filepath.Join(publishDir, relPath), relPath) syncer.Sync(filepath.Join(publishDir, relPath), relPath)
} else { } else {
jww.ERROR.Println(err) jww.ERROR.Println(err)
@ -765,7 +766,7 @@ func NewWatcher(port int) error {
} }
// For all other event operations Hugo will sync static. // For all other event operations Hugo will sync static.
jww.FEEDBACK.Println("Syncing", relPath, "to", publishDir) logger.Println("Syncing", relPath, "to", publishDir)
syncer.Sync(filepath.Join(publishDir, relPath), relPath) syncer.Sync(filepath.Join(publishDir, relPath), relPath)
} }
} }

View file

@ -182,6 +182,7 @@ func ThemeSet() bool {
} }
type logPrinter interface { type logPrinter interface {
// Println is the only common method that works in all of JWWs loggers.
Println(a ...interface{}) Println(a ...interface{})
} }
@ -192,10 +193,23 @@ type DistinctLogger struct {
m map[string]bool m map[string]bool
} }
// Println will log the string returned from fmt.Sprintln given the arguments,
// but not if it has been logged before.
func (l *DistinctLogger) Println(v ...interface{}) {
// fmt.Sprint doesn't add space between string arguments
logStatement := strings.TrimSpace(fmt.Sprintln(v...))
l.print(logStatement)
}
// Printf will log the string returned from fmt.Sprintf given the arguments, // Printf will log the string returned from fmt.Sprintf given the arguments,
// but not if it has been logged before. // but not if it has been logged before.
// Note: A newline is appended.
func (l *DistinctLogger) Printf(format string, v ...interface{}) { func (l *DistinctLogger) Printf(format string, v ...interface{}) {
logStatement := fmt.Sprintf(format, v...) logStatement := fmt.Sprintf(format, v...)
l.print(logStatement)
}
func (l *DistinctLogger) print(logStatement string) {
l.RLock() l.RLock()
if l.m[logStatement] { if l.m[logStatement] {
l.RUnlock() l.RUnlock()
@ -207,7 +221,6 @@ func (l *DistinctLogger) Printf(format string, v ...interface{}) {
if !l.m[logStatement] { if !l.m[logStatement] {
l.logger.Println(logStatement) l.logger.Println(logStatement)
l.m[logStatement] = true l.m[logStatement] = true
fmt.Println()
} }
l.Unlock() l.Unlock()
} }
@ -217,6 +230,12 @@ func NewDistinctErrorLogger() *DistinctLogger {
return &DistinctLogger{m: make(map[string]bool), logger: jww.ERROR} return &DistinctLogger{m: make(map[string]bool), logger: jww.ERROR}
} }
// NewDistinctErrorLogger creates a new DistinctLogger that can be used
// to give feedback to the user while not spamming with duplicates.
func NewDistinctFeedbackLogger() *DistinctLogger {
return &DistinctLogger{m: make(map[string]bool), logger: &jww.FEEDBACK}
}
// Avoid spamming the logs with errors // Avoid spamming the logs with errors
var DistinctErrorLog = NewDistinctErrorLogger() var DistinctErrorLog = NewDistinctErrorLogger()

View file

@ -31,6 +31,7 @@ import (
"sync/atomic" "sync/atomic"
"bitbucket.org/pkg/inflect" "bitbucket.org/pkg/inflect"
"github.com/spf13/afero"
"github.com/spf13/cast" "github.com/spf13/cast"
bp "github.com/spf13/hugo/bufferpool" bp "github.com/spf13/hugo/bufferpool"
"github.com/spf13/hugo/helpers" "github.com/spf13/hugo/helpers"
@ -44,7 +45,6 @@ import (
"github.com/spf13/nitro" "github.com/spf13/nitro"
"github.com/spf13/viper" "github.com/spf13/viper"
"gopkg.in/fsnotify.v1" "gopkg.in/fsnotify.v1"
"github.com/spf13/afero"
) )
var _ = transform.AbsURL var _ = transform.AbsURL
@ -438,19 +438,22 @@ func (s *Site) ReBuild(events []fsnotify.Event) error {
var err error var err error
// prevent spamming the log on changes
logger := helpers.NewDistinctFeedbackLogger()
for _, ev := range events { for _, ev := range events {
// Need to re-read source // Need to re-read source
if strings.HasPrefix(ev.Name, s.absContentDir()) { if strings.HasPrefix(ev.Name, s.absContentDir()) {
fmt.Println("Source changed", ev) logger.Println("Source changed", ev.Name)
sourceChanged = append(sourceChanged, ev) sourceChanged = append(sourceChanged, ev)
} }
if strings.HasPrefix(ev.Name, s.absLayoutDir()) || strings.HasPrefix(ev.Name, s.absThemeDir()) { if strings.HasPrefix(ev.Name, s.absLayoutDir()) || strings.HasPrefix(ev.Name, s.absThemeDir()) {
fmt.Println("Template changed", ev) logger.Println("Template changed", ev.Name)
tmplChanged = append(tmplChanged, ev) tmplChanged = append(tmplChanged, ev)
} }
if strings.HasPrefix(ev.Name, s.absDataDir()) { if strings.HasPrefix(ev.Name, s.absDataDir()) {
fmt.Println("Data changed", ev) logger.Println("Data changed", ev.Name)
dataChanged = append(dataChanged,ev) dataChanged = append(dataChanged, ev)
} }
} }
@ -502,7 +505,7 @@ func (s *Site) ReBuild(events []fsnotify.Event) error {
for _, ev := range sourceChanged { for _, ev := range sourceChanged {
if ev.Op&fsnotify.Remove == fsnotify.Remove { if ev.Op&fsnotify.Remove == fsnotify.Remove {
//remove the file & a create will follow //remove the file & a create will follow
path, _ := helpers.GetRelativePath(ev.Name, s.absContentDir()) path, _ := helpers.GetRelativePath(ev.Name, s.absContentDir())
s.RemovePageByPath(path) s.RemovePageByPath(path)
@ -1027,7 +1030,6 @@ func (s *Site) AddPage(page *Page) {
} }
} }
func (s *Site) RemovePageByPath(path string) { func (s *Site) RemovePageByPath(path string) {
if i := s.Pages.FindPagePosByFilePath(path); i >= 0 { if i := s.Pages.FindPagePosByFilePath(path); i >= 0 {
page := s.Pages[i] page := s.Pages[i]