-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathwarn.go
48 lines (39 loc) · 887 Bytes
/
warn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"fmt"
"log"
"sync"
)
type warningCollector struct {
mu sync.Mutex
warnings []string
}
func (w *warningCollector) warn(s string) {
w.mu.Lock()
w.warnings = append(w.warnings, s)
w.mu.Unlock()
log.Printf("WARNING: %s", s)
}
func (w *warningCollector) Warnf(format string, a ...interface{}) {
w.warn(fmt.Sprintf(format, a...))
}
func (w *warningCollector) Warn(a ...interface{}) {
w.warn(fmt.Sprint(a...))
}
func (w *warningCollector) Warns() []string {
var l []string
w.mu.Lock()
l = append(l, w.warnings...)
w.mu.Unlock()
return l
}
// WarningCollector is the default warning collector
var WarningCollector = &warningCollector{}
// Warnf logs a warning
func Warnf(format string, a ...interface{}) {
WarningCollector.Warnf(format, a...)
}
// Warns returns the logged warnings
func Warns() []string {
return WarningCollector.Warns()
}