From 3054a461850485bad3293907720f4c5a9d76cab0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Thu, 28 Jan 2016 14:00:03 +0100 Subject: [PATCH] Make the DistinctErrorLogger more generic --- helpers/general.go | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/helpers/general.go b/helpers/general.go index 333cbfa3c..3126bf5ca 100644 --- a/helpers/general.go +++ b/helpers/general.go @@ -181,15 +181,20 @@ func ThemeSet() bool { return viper.GetString("theme") != "" } -// DistinctErrorLogger ignores duplicate log statements. -type DistinctErrorLogger struct { - sync.RWMutex - m map[string]bool +type logPrinter interface { + Println(a ...interface{}) } -// Printf will ERROR log the string returned from fmt.Sprintf given the arguments, +// DistinctLogger ignores duplicate log statements. +type DistinctLogger struct { + sync.RWMutex + logger logPrinter + m map[string]bool +} + +// Printf will log the string returned from fmt.Sprintf given the arguments, // but not if it has been logged before. -func (l *DistinctErrorLogger) Printf(format string, v ...interface{}) { +func (l *DistinctLogger) Printf(format string, v ...interface{}) { logStatement := fmt.Sprintf(format, v...) l.RLock() if l.m[logStatement] { @@ -200,15 +205,16 @@ func (l *DistinctErrorLogger) Printf(format string, v ...interface{}) { l.Lock() if !l.m[logStatement] { - jww.ERROR.Print(logStatement) + l.logger.Println(logStatement) l.m[logStatement] = true + fmt.Println() } l.Unlock() } -// NewDistinctErrorLogger creates a new DistinctErrorLogger -func NewDistinctErrorLogger() *DistinctErrorLogger { - return &DistinctErrorLogger{m: make(map[string]bool)} +// NewDistinctErrorLogger creates a new DistinctLogger that logs ERRORs +func NewDistinctErrorLogger() *DistinctLogger { + return &DistinctLogger{m: make(map[string]bool), logger: jww.ERROR} } // Avoid spamming the logs with errors