shithub: hugo

Download patch

ref: 5def6d9aee659160d921bcbc1c9d98007a428d54
parent: 3054a461850485bad3293907720f4c5a9d76cab0
author: Bjørn Erik Pedersen <[email protected]>
date: Thu Jan 28 10:31:25 EST 2016

Make the watch logger less chatty

--- a/commands/hugo.go
+++ b/commands/hugo.go
@@ -30,6 +30,7 @@
 
 	"regexp"
 
+	"github.com/spf13/afero"
 	"github.com/spf13/cobra"
 	"github.com/spf13/fsync"
 	"github.com/spf13/hugo/helpers"
@@ -42,7 +43,6 @@
 	"github.com/spf13/nitro"
 	"github.com/spf13/viper"
 	"gopkg.in/fsnotify.v1"
-	"github.com/spf13/afero"
 )
 
 var mainSite *hugolib.Site
@@ -654,7 +654,7 @@
 						continue
 					}
 
-					walkAdder := func (path string, f os.FileInfo, err error) error {
+					walkAdder := func(path string, f os.FileInfo, err error) error {
 						if f.IsDir() {
 							jww.FEEDBACK.Println("adding created directory to watchlist", path)
 							watcher.Add(path)
@@ -687,7 +687,7 @@
 						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"
 					fmt.Println(time.Now().Format(layout))
 
@@ -710,6 +710,8 @@
 						syncer.SrcFs = staticSourceFs
 						syncer.DestFs = hugofs.DestinationFS
 
+						// prevent spamming the log on changes
+						logger := helpers.NewDistinctFeedbackLogger()
 
 						for _, ev := range staticEvents {
 							// Due to our approach of layering both directories and the content's rendered output
@@ -727,7 +729,6 @@
 							// 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
 							// and then to handle conflict resolution on every event.
-							fmt.Println(ev)
 
 							fromPath := ev.Name
 
@@ -750,12 +751,12 @@
 							if ev.Op&fsnotify.Rename == fsnotify.Rename || ev.Op&fsnotify.Remove == fsnotify.Remove {
 								if _, err := staticSourceFs.Stat(relPath); os.IsNotExist(err) {
 									// If file doesn't exist in any static dir, remove it
-									toRemove :=filepath.Join(publishDir, relPath)
-									jww.FEEDBACK.Println("File no longer exists in static dir, removing", toRemove)
+									toRemove := filepath.Join(publishDir, relPath)
+									logger.Println("File no longer exists in static dir, removing", toRemove)
 									hugofs.DestinationFS.RemoveAll(toRemove)
 								} else if err == nil {
 									// 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)
 								} else {
 									jww.ERROR.Println(err)
@@ -765,7 +766,7 @@
 							}
 
 							// 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)
 						}
 					}
--- a/helpers/general.go
+++ b/helpers/general.go
@@ -182,6 +182,7 @@
 }
 
 type logPrinter interface {
+	// Println is the only common method that works in all of JWWs loggers.
 	Println(a ...interface{})
 }
 
@@ -192,10 +193,23 @@
 	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,
 // but not if it has been logged before.
+// Note: A newline is appended.
 func (l *DistinctLogger) Printf(format string, v ...interface{}) {
 	logStatement := fmt.Sprintf(format, v...)
+	l.print(logStatement)
+}
+
+func (l *DistinctLogger) print(logStatement string) {
 	l.RLock()
 	if l.m[logStatement] {
 		l.RUnlock()
@@ -207,7 +221,6 @@
 	if !l.m[logStatement] {
 		l.logger.Println(logStatement)
 		l.m[logStatement] = true
-		fmt.Println()
 	}
 	l.Unlock()
 }
@@ -215,6 +228,12 @@
 // NewDistinctErrorLogger creates a new DistinctLogger that logs ERRORs
 func NewDistinctErrorLogger() *DistinctLogger {
 	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
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -31,6 +31,7 @@
 	"sync/atomic"
 
 	"bitbucket.org/pkg/inflect"
+	"github.com/spf13/afero"
 	"github.com/spf13/cast"
 	bp "github.com/spf13/hugo/bufferpool"
 	"github.com/spf13/hugo/helpers"
@@ -44,7 +45,6 @@
 	"github.com/spf13/nitro"
 	"github.com/spf13/viper"
 	"gopkg.in/fsnotify.v1"
-	"github.com/spf13/afero"
 )
 
 var _ = transform.AbsURL
@@ -438,19 +438,22 @@
 
 	var err error
 
+	// prevent spamming the log on changes
+	logger := helpers.NewDistinctFeedbackLogger()
+
 	for _, ev := range events {
 		// Need to re-read source
 		if strings.HasPrefix(ev.Name, s.absContentDir()) {
-			fmt.Println("Source changed", ev)
+			logger.Println("Source changed", ev.Name)
 			sourceChanged = append(sourceChanged, ev)
 		}
 		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)
 		}
 		if strings.HasPrefix(ev.Name, s.absDataDir()) {
-			fmt.Println("Data changed", ev)
-			dataChanged = append(dataChanged,ev)
+			logger.Println("Data changed", ev.Name)
+			dataChanged = append(dataChanged, ev)
 		}
 	}
 
@@ -502,7 +505,7 @@
 
 		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
 				path, _ := helpers.GetRelativePath(ev.Name, s.absContentDir())
 				s.RemovePageByPath(path)
@@ -1026,7 +1029,6 @@
 		s.futureCount++
 	}
 }
-
 
 func (s *Site) RemovePageByPath(path string) {
 	if i := s.Pages.FindPagePosByFilePath(path); i >= 0 {