diff options
author | bep <bjorn.erik.pedersen@gmail.com> | 2015-03-31 21:33:24 +0100 |
---|---|---|
committer | bep <bjorn.erik.pedersen@gmail.com> | 2015-03-31 22:33:17 +0200 |
commit | bec4bdae992841f011239dac8c685e13470a90f3 (patch) | |
tree | 416c00cd413a5a3109005cdf76e813ce16363216 /helpers/general.go | |
parent | bec22f8981c251f88594689c65ad7b8822fe1b09 (diff) |
Return error on wrong use of the Paginator
`Paginate`now returns error when
1) `.Paginate` is called after `.Paginator`
2) `.Paginate` is repeatedly called with different arguments
This should help remove some confusion.
This commit also introduces DistinctErrorLogger, to prevent spamming the log for duplicate rendering errors from the pagers.
Fixes #993
Diffstat (limited to 'helpers/general.go')
-rw-r--r-- | helpers/general.go | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/helpers/general.go b/helpers/general.go index 14cce5cf0..cf87c04b1 100644 --- a/helpers/general.go +++ b/helpers/general.go @@ -153,26 +153,37 @@ func ThemeSet() bool { return viper.GetString("theme") != "" } -// Avoid spamming the logs with errors -var deprecatedLogs = struct { +// DistinctErrorLogger ignores duplicate log statements. +type DistinctErrorLogger struct { sync.RWMutex m map[string]bool -}{m: make(map[string]bool)} +} -func Deprecated(object, item, alternative string) { - key := object + item + alternative - deprecatedLogs.RLock() - logged := deprecatedLogs.m[key] - deprecatedLogs.RUnlock() +func (l *DistinctErrorLogger) Printf(format string, v ...interface{}) { + logStatement := fmt.Sprintf(format, v...) + l.RLock() + logged := l.m[logStatement] + l.RUnlock() if logged { return } - deprecatedLogs.Lock() - if !deprecatedLogs.m[key] { - jww.ERROR.Printf("%s's %s is deprecated and will be removed in Hugo %s. Use %s instead.", object, item, NextHugoReleaseVersion(), alternative) - deprecatedLogs.m[key] = true + l.Lock() + if !l.m[logStatement] { + jww.ERROR.Print(logStatement) + l.m[logStatement] = true } - deprecatedLogs.Unlock() + l.Unlock() +} + +func NewDistinctErrorLogger() *DistinctErrorLogger { + return &DistinctErrorLogger{m: make(map[string]bool)} +} + +// Avoid spamming the logs with errors +var deprecatedLogger = NewDistinctErrorLogger() + +func Deprecated(object, item, alternative string) { + deprecatedLogger.Printf("%s's %s is deprecated and will be removed in Hugo %s. Use %s instead.", object, item, NextHugoReleaseVersion(), alternative) } // SliceToLower goes through the source slice and lowers all values. |