summaryrefslogtreecommitdiffstats
path: root/helpers/general.go
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2016-01-28 15:31:25 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2016-01-28 15:33:41 +0100
commit5def6d9aee659160d921bcbc1c9d98007a428d54 (patch)
tree35beb4fd7055af2e7fbfb28f3c21e519578b2412 /helpers/general.go
parent3054a461850485bad3293907720f4c5a9d76cab0 (diff)
Make the watch logger less chatty
Diffstat (limited to 'helpers/general.go')
-rw-r--r--helpers/general.go21
1 files changed, 20 insertions, 1 deletions
diff --git a/helpers/general.go b/helpers/general.go
index 3126bf5ca..6ed95f8f4 100644
--- a/helpers/general.go
+++ b/helpers/general.go
@@ -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()