forked from zalando/skipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
144 lines (124 loc) · 3.14 KB
/
logger.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package loggingtest
import (
"errors"
"fmt"
"log"
"strings"
"time"
)
type logSubscription struct {
exp string
n int
notify chan<- struct{}
}
type logWatch struct {
entries []string
reqs []*logSubscription
}
// Logger provides an implementation of the logging.Logger interface
// that can be used to receive notifications about log events.
type Logger struct {
save chan string
notify chan<- logSubscription
clear chan struct{}
quit chan<- struct{}
}
// ErrWaitTimeout is returned when a logging event doesn't happen
// within a timeout.
var ErrWaitTimeout = errors.New("timeout")
func (lw *logWatch) save(e string) {
lw.entries = append(lw.entries, e)
for i := len(lw.reqs) - 1; i >= 0; i-- {
req := lw.reqs[i]
if strings.Contains(e, req.exp) {
req.n--
if req.n <= 0 {
close(req.notify)
lw.reqs = append(lw.reqs[:i], lw.reqs[i+1:]...)
}
}
}
}
func (lw *logWatch) notify(req logSubscription) {
for i := len(lw.entries) - 1; i >= 0; i-- {
if strings.Contains(lw.entries[i], req.exp) {
req.n--
if req.n == 0 {
break
}
}
}
if req.n <= 0 {
close(req.notify)
} else {
lw.reqs = append(lw.reqs, &req)
}
}
func (lw *logWatch) clear() {
lw.entries = nil
lw.reqs = nil
}
// Returns a new, initialized instance of Logger.
func New() *Logger {
lw := &logWatch{}
save := make(chan string)
notify := make(chan logSubscription)
clear := make(chan struct{})
quit := make(chan struct{})
go func() {
for {
select {
case e := <-save:
lw.save(e)
case req := <-notify:
lw.notify(req)
case <-clear:
lw.clear()
case <-quit:
return
}
}
}()
return &Logger{save, notify, clear, quit}
}
func (tl *Logger) logf(f string, a ...interface{}) {
log.Printf(f, a...)
tl.save <- fmt.Sprintf(f, a...)
}
func (tl *Logger) log(a ...interface{}) {
log.Println(a...)
tl.save <- fmt.Sprint(a...)
}
// Returns nil when n logging events matching exp were received or returns
// ErrWaitTimeout when to timeout expired.
func (tl *Logger) WaitForN(exp string, n int, to time.Duration) error {
found := make(chan struct{}, 1)
tl.notify <- logSubscription{exp, n, found}
select {
case <-found:
return nil
case <-time.After(to):
return ErrWaitTimeout
}
}
// Returns nil when a logging event matching exp was received or returns
// ErrWaitTimeout when to timeout expired.
func (tl *Logger) WaitFor(exp string, to time.Duration) error {
return tl.WaitForN(exp, 1, to)
}
// Clears the stored logging events.
func (tl *Logger) Reset() {
tl.clear <- struct{}{}
}
// Closes the logger.
func (tl *Logger) Close() {
close(tl.quit)
}
func (tl *Logger) Error(a ...interface{}) { tl.log(a...) }
func (tl *Logger) Errorf(f string, a ...interface{}) { tl.logf(f, a...) }
func (tl *Logger) Warn(a ...interface{}) { tl.log(a...) }
func (tl *Logger) Warnf(f string, a ...interface{}) { tl.logf(f, a...) }
func (tl *Logger) Info(a ...interface{}) { tl.log(a...) }
func (tl *Logger) Infof(f string, a ...interface{}) { tl.logf(f, a...) }
func (tl *Logger) Debug(a ...interface{}) { tl.log(a...) }
func (tl *Logger) Debugf(f string, a ...interface{}) { tl.logf(f, a...) }