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

@@ -182,6 +182,7 @@ func ThemeSet() bool {
}
type logPrinter interface {
// Println is the only common method that works in all of JWWs loggers.
Println(a ...interface{})
}
@@ -192,10 +193,23 @@ type DistinctLogger struct {
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 @@ func (l *DistinctLogger) Printf(format string, v ...interface{}) {
if !l.m[logStatement] {
l.logger.Println(logStatement)
l.m[logStatement] = true
fmt.Println()
}
l.Unlock()
}
@@ -217,6 +230,12 @@ 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
var DistinctErrorLog = NewDistinctErrorLogger()