forked from keybase/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_logger.go
133 lines (106 loc) · 3.74 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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package logger
import (
"fmt"
"runtime"
"strings"
keybase1 "github.com/keybase/client/go/protocol"
"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{})
}
// 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
}
func NewTestLogger(log TestLogBackend) *TestLogger {
return &TestLogger{log: log}
}
// Verify TestLogger fully implements the Logger interface.
var _ Logger = (*TestLogger)(nil)
func prefixCaller(fmts string) string {
// The testing library doesn't let us control the stack depth, so
// just print the file and line number ourselves.
_, file, line, _ := runtime.Caller(2)
elements := strings.Split(file, "/")
return fmt.Sprintf("%s:%d %s", elements[len(elements)-1], line, fmts)
}
func (log *TestLogger) Debug(fmts string, arg ...interface{}) {
log.log.Logf(prefixCaller(fmts), arg...)
}
func (log *TestLogger) CDebugf(ctx context.Context, fmts string,
arg ...interface{}) {
log.log.Logf(prefixCaller(fmts), arg...)
}
func (log *TestLogger) Info(fmts string, arg ...interface{}) {
log.log.Logf(prefixCaller(fmts), arg...)
}
func (log *TestLogger) CInfof(ctx context.Context, fmts string,
arg ...interface{}) {
log.log.Logf(prefixCaller(fmts), arg...)
}
func (log *TestLogger) Notice(fmts string, arg ...interface{}) {
log.log.Logf(prefixCaller(fmts), arg...)
}
func (log *TestLogger) CNoticef(ctx context.Context, fmts string,
arg ...interface{}) {
log.log.Logf(prefixCaller(fmts), arg...)
}
func (log *TestLogger) Warning(fmts string, arg ...interface{}) {
log.log.Logf(prefixCaller(fmts), arg...)
}
func (log *TestLogger) CWarningf(ctx context.Context, fmts string,
arg ...interface{}) {
log.log.Logf(prefixCaller(fmts), arg...)
}
func (log *TestLogger) Error(fmts string, arg ...interface{}) {
log.log.Logf(prefixCaller(fmts), arg...)
}
func (log *TestLogger) Errorf(fmts string, arg ...interface{}) {
log.log.Logf(prefixCaller(fmts), arg...)
}
func (log *TestLogger) CErrorf(ctx context.Context, fmts string,
arg ...interface{}) {
log.log.Logf(prefixCaller(fmts), arg...)
}
func (log *TestLogger) Critical(fmts string, arg ...interface{}) {
log.log.Logf(prefixCaller(fmts), arg...)
}
func (log *TestLogger) CCriticalf(ctx context.Context, fmts string,
arg ...interface{}) {
log.log.Logf(prefixCaller(fmts), arg...)
}
func (log *TestLogger) Fatalf(fmts string, arg ...interface{}) {
log.log.Fatalf(prefixCaller(fmts), arg...)
}
func (log *TestLogger) CFatalf(ctx context.Context, fmts string,
arg ...interface{}) {
log.log.Fatalf(prefixCaller(fmts), arg...)
}
func (log *TestLogger) Profile(fmts string, arg ...interface{}) {
log.log.Logf(prefixCaller(fmts), arg...)
}
func (log *TestLogger) Configure(style string, debug bool, filename string) {
// no-op
}
func (log *TestLogger) RotateLogFile() error {
// no-op
return nil
}
// no-op stubs to fulfill the Logger interface
func (log *TestLogger) AddExternalLogger(externalLogger ExternalLogger) uint64 { return 0 }
func (log *TestLogger) RemoveExternalLogger(handle uint64) {}
func (log *TestLogger) SetExternalLogLevel(level keybase1.LogLevel) {}