-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
test_logger.go
205 lines (171 loc) · 5.98 KB
/
test_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package logger
import (
"fmt"
"os"
"runtime"
"strings"
"sync"
"time"
logging "github.com/keybase/go-logging"
"golang.org/x/net/context"
)
// TestLogBackend is an interface for logging to a test object (i.e.,
// a *testing.T). We define this in order to avoid pulling in the
// "testing" package in exported code.
type TestLogBackend interface {
Error(args ...interface{})
Errorf(format string, args ...interface{})
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
Log(args ...interface{})
Logf(format string, args ...interface{})
Failed() bool
Name() string
}
// TestLogger is a Logger that writes to a TestLogBackend. All
// messages except Fatal are printed using Logf, to avoid failing a
// test that is trying to test an error condition. No context tags
// are logged.
type TestLogger struct {
log TestLogBackend
extraDepth int
sync.Mutex
}
func NewTestLogger(log TestLogBackend) *TestLogger {
return &TestLogger{log: log}
}
// Verify TestLogger fully implements the Logger interface.
var _ Logger = (*TestLogger)(nil)
// Whether "TEST FAILED" has output for a particular test is stored globally.
// Because some tests use multiple instances of TestLogger so storing
// it there would result in multiple "TEST FAILED" per test.
// This way has the drawback that when two tests in different packages
// share a name, only one of their "TEST FAILED" will print.
var globalFailReportedLock sync.Mutex
var globalFailReported = make(map[string]struct{})
// ctx can be `nil`
func (log *TestLogger) common(ctx context.Context, lvl logging.Level, useFatal bool, fmts string, arg ...interface{}) {
if log.log.Failed() {
globalFailReportedLock.Lock()
name := log.log.Name()
if _, reported := globalFailReported[name]; !reported {
log.log.Logf("TEST FAILED: %s", name)
globalFailReported[name] = struct{}{}
}
globalFailReportedLock.Unlock()
if stringListContains(strings.ToLower(os.Getenv("KEYBASE_TEST_LOG_AFTER_FAIL")), []string{"0", "false", "n", "no"}) {
return
}
}
if os.Getenv("KEYBASE_TEST_DUP_LOG_TO_STDOUT") != "" {
fmt.Printf(prepareString(ctx,
log.prefixCaller(log.extraDepth, lvl, fmts))+"\n", arg...)
}
if ctx != nil {
if useFatal {
log.log.Fatalf(prepareString(ctx,
log.prefixCaller(log.extraDepth, lvl, fmts)), arg...)
} else {
log.log.Logf(prepareString(ctx,
log.prefixCaller(log.extraDepth, lvl, fmts)), arg...)
}
} else {
if useFatal {
log.log.Fatalf(log.prefixCaller(log.extraDepth, lvl, fmts), arg...)
} else {
log.log.Logf(log.prefixCaller(log.extraDepth, lvl, fmts), arg...)
}
}
}
func (log *TestLogger) prefixCaller(extraDepth int, lvl logging.Level, fmts string) string {
// The testing library doesn't let us control the stack depth,
// and it always prints out its own prefix, so use \r to clear
// it out (at least on a terminal) and do our own formatting.
_, file, line, _ := runtime.Caller(3 + extraDepth)
elements := strings.Split(file, "/")
failed := ""
if log.log.Failed() {
failed = "[X] "
}
fileLine := fmt.Sprintf("%s:%d", elements[len(elements)-1], line)
return fmt.Sprintf("\r%s %s%-23s: [%.1s] %s", time.Now().Format("2006-01-02 15:04:05.00000"),
failed, fileLine, lvl, fmts)
}
func (log *TestLogger) Debug(fmts string, arg ...interface{}) {
log.common(context.TODO(), logging.DEBUG, false, fmts, arg...)
}
func (log *TestLogger) CDebugf(ctx context.Context, fmts string,
arg ...interface{}) {
log.common(ctx, logging.DEBUG, false, fmts, arg...)
}
func (log *TestLogger) Info(fmts string, arg ...interface{}) {
log.common(context.TODO(), logging.INFO, false, fmts, arg...)
}
func (log *TestLogger) CInfof(ctx context.Context, fmts string,
arg ...interface{}) {
log.common(ctx, logging.INFO, false, fmts, arg...)
}
func (log *TestLogger) Notice(fmts string, arg ...interface{}) {
log.common(context.TODO(), logging.NOTICE, false, fmts, arg...)
}
func (log *TestLogger) CNoticef(ctx context.Context, fmts string,
arg ...interface{}) {
log.common(ctx, logging.NOTICE, false, fmts, arg...)
}
func (log *TestLogger) Warning(fmts string, arg ...interface{}) {
log.common(context.TODO(), logging.WARNING, false, fmts, arg...)
}
func (log *TestLogger) CWarningf(ctx context.Context, fmts string,
arg ...interface{}) {
log.common(ctx, logging.WARNING, false, fmts, arg...)
}
func (log *TestLogger) Error(fmts string, arg ...interface{}) {
log.common(context.TODO(), logging.ERROR, false, fmts, arg...)
}
func (log *TestLogger) Errorf(fmts string, arg ...interface{}) {
log.common(context.TODO(), logging.ERROR, false, fmts, arg...)
}
func (log *TestLogger) CErrorf(ctx context.Context, fmts string,
arg ...interface{}) {
log.common(ctx, logging.ERROR, false, fmts, arg...)
}
func (log *TestLogger) Critical(fmts string, arg ...interface{}) {
log.common(context.TODO(), logging.CRITICAL, false, fmts, arg...)
}
func (log *TestLogger) CCriticalf(ctx context.Context, fmts string,
arg ...interface{}) {
log.common(ctx, logging.CRITICAL, false, fmts, arg...)
}
func (log *TestLogger) Fatalf(fmts string, arg ...interface{}) {
log.common(context.TODO(), logging.CRITICAL, true, fmts, arg...)
}
func (log *TestLogger) CFatalf(ctx context.Context, fmts string,
arg ...interface{}) {
log.common(ctx, logging.CRITICAL, true, fmts, arg...)
}
func (log *TestLogger) Profile(fmts string, arg ...interface{}) {
log.common(context.TODO(), logging.CRITICAL, false, fmts, arg...)
}
func (log *TestLogger) Configure(style string, debug bool, filename string) {
// no-op
}
func (log *TestLogger) CloneWithAddedDepth(depth int) Logger {
log.Lock()
defer log.Unlock()
var clone TestLogger
clone.log = log.log
clone.extraDepth = log.extraDepth + depth
return &clone
}
// no-op stubs to fulfill the Logger interface
func (log *TestLogger) SetExternalHandler(_ ExternalHandler) {}
func stringListContains(s string, a []string) bool {
for _, t := range a {
if s == t {
return true
}
}
return false
}