This repository has been archived by the owner on Apr 3, 2023. It is now read-only.
forked from lyft/protoc-gen-star
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
286 lines (226 loc) · 7.12 KB
/
debug.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package pgs
import (
"bytes"
"fmt"
"io"
"log"
"os"
"strings"
)
// DebuggerCommon contains shared features of Debugger and Debugger-like types
// (such as BuildContext).
type DebuggerCommon interface {
// Log writes v to the underlying logging location (typically, os.Stderr). It
// uses the same behavior as log.Print, with all prefixes already attached.
Log(v ...interface{})
// Logf formats v and writes it to the underlying logging location
// (typically, os.Stderr). It uses the same behavior as log.Printf, with all
// prefixes already attached.
Logf(format string, v ...interface{})
// Debug behaves the same as Log, but only writes its output if debugging is
// enabled for this Debugger.
Debug(v ...interface{})
// Debugf behaves the same as Logf, but only writes its output if debugging
// is enabled for this Debugger.
Debugf(format string, v ...interface{})
// Fail behaves the same as Log, but also terminates the process. This method
// should be used if an un-recoverable error is encountered.
Fail(v ...interface{})
// Failf behaves the same as Logf, but also terminates the process. This
// method should be used if an un-recoverable error is encountered.
Failf(format string, v ...interface{})
// CheckErr ensures that err is nil. If err is not nil, Fail is called with
// err and the provided v.
CheckErr(err error, v ...interface{})
// Assert ensures that expr evaluates to true. If expr is false, Fail is
// called with the provided v.
Assert(expr bool, v ...interface{})
// Exit should terminate the current process with the provided code.
Exit(code int)
}
// A Debugger provides utility methods to provide context-aware logging,
// error-checking, and assertions. The Debugger is used extensively within the
// protoc-gen-star generator, and is provided in a module's build context.
type Debugger interface {
DebuggerCommon
// Push returns a new Debugger with the provided prefix. When entering a new
// context, this method should be used.
Push(prefix string) Debugger
// Pop returns the parent for the current Debugger. When exiting a context,
// this method should be used.
Pop() Debugger
}
type logger interface {
Println(...interface{})
Printf(string, ...interface{})
}
type errFunc func(err error, msgs ...interface{})
type failFunc func(msgs ...interface{})
type exitFunc func(code int)
type rootDebugger struct {
err errFunc
fail failFunc
exit exitFunc
l logger
logDebugs bool
}
func initDebugger(d bool, l logger) Debugger {
rd := rootDebugger{
logDebugs: d,
l: l,
exit: os.Exit,
}
rd.fail = failFunc(rd.defaultFail)
rd.err = errFunc(rd.defaultErr)
return rd
}
func (d rootDebugger) defaultErr(err error, msg ...interface{}) {
if err != nil {
d.l.Printf("[error] %s: %v\n", fmt.Sprint(msg...), err)
d.exit(1)
}
}
func (d rootDebugger) defaultFail(msg ...interface{}) {
d.l.Println(msg...)
d.exit(1)
}
func (d rootDebugger) Log(v ...interface{}) { d.l.Println(v...) }
func (d rootDebugger) Logf(format string, v ...interface{}) { d.l.Printf(format, v...) }
func (d rootDebugger) Fail(v ...interface{}) { d.fail(fmt.Sprint(v...)) }
func (d rootDebugger) Failf(format string, v ...interface{}) { d.fail(fmt.Sprintf(format, v...)) }
func (d rootDebugger) Exit(code int) { d.exit(code) }
func (d rootDebugger) Debug(v ...interface{}) {
if d.logDebugs {
d.Log(v...)
}
}
func (d rootDebugger) Debugf(format string, v ...interface{}) {
if d.logDebugs {
d.Logf(format, v...)
}
}
func (d rootDebugger) CheckErr(err error, v ...interface{}) {
if err != nil {
d.err(err, fmt.Sprint(v...))
}
}
func (d rootDebugger) Assert(expr bool, v ...interface{}) {
if !expr {
d.Fail(fmt.Sprint(v...))
}
}
func (d rootDebugger) Push(prefix string) Debugger {
return prefixedDebugger{
parent: d,
prefix: fmt.Sprintf("[%s]", prefix),
}
}
func (d rootDebugger) Pop() Debugger {
d.Fail("attempted to pop the root debugger")
return nil
}
type prefixedDebugger struct {
parent Debugger
prefix string
}
func (d prefixedDebugger) prepend(v []interface{}) []interface{} {
return append([]interface{}{d.prefix}, v...)
}
func (d prefixedDebugger) prependFormat(format string) string {
if strings.HasPrefix(format, "[") {
return d.prefix + format
}
return d.prefix + " " + format
}
func (d prefixedDebugger) Log(v ...interface{}) {
d.parent.Log(d.prepend(v)...)
}
func (d prefixedDebugger) Logf(format string, v ...interface{}) {
d.parent.Logf(d.prependFormat(format), v...)
}
func (d prefixedDebugger) Debug(v ...interface{}) {
d.parent.Debug(d.prepend(v)...)
}
func (d prefixedDebugger) Debugf(format string, v ...interface{}) {
d.parent.Debugf(d.prependFormat(format), v...)
}
func (d prefixedDebugger) Fail(v ...interface{}) {
d.parent.Fail(d.prepend(v)...)
}
func (d prefixedDebugger) Failf(format string, v ...interface{}) {
d.parent.Failf(d.prependFormat(format), v...)
}
func (d prefixedDebugger) CheckErr(err error, v ...interface{}) {
d.parent.CheckErr(err, d.prepend(v)...)
}
func (d prefixedDebugger) Assert(expr bool, v ...interface{}) {
d.parent.Assert(expr, d.prepend(v)...)
}
func (d prefixedDebugger) Exit(code int) { d.parent.Exit(code) }
func (d prefixedDebugger) Push(prefix string) Debugger {
return prefixedDebugger{
parent: d,
prefix: "[" + prefix + "]",
}
}
func (d prefixedDebugger) Pop() Debugger {
return d.parent
}
// MockDebugger serves as a root Debugger instance for usage in tests. Unlike
// an actual Debugger, MockDebugger will not exit the program, but will track
// failures, checked errors, and exit codes.
type MockDebugger interface {
Debugger
// Output returns a reader of all logged data.
Output() io.Reader
// Failed returns true if Fail or Failf has been called on this debugger or a
// descendant of it (via Push).
Failed() bool
// Err returns the error passed to CheckErr.
Err() error
// Exited returns true if this Debugger (or a descendant of it) would have
// called os.Exit.
Exited() bool
// ExitCode returns the code this Debugger (or a descendant of it) passed to
// os.Exit. If Exited() returns false, this value is meaningless.
ExitCode() int
}
type mockDebugger struct {
Debugger
buf bytes.Buffer
failed bool
err error
exited bool
code int
}
// InitMockDebugger creates a new MockDebugger for usage in tests.
func InitMockDebugger() MockDebugger {
md := &mockDebugger{}
d := initDebugger(true, log.New(&md.buf, "", 0)).(rootDebugger)
d.fail = func(msgs ...interface{}) {
md.failed = true
d.defaultFail(msgs...)
}
d.err = func(err error, msgs ...interface{}) {
if err != nil {
md.err = err
}
d.defaultErr(err, msgs...)
}
d.exit = func(code int) {
md.exited = true
md.code = code
}
md.Debugger = d
return md
}
func (d *mockDebugger) Output() io.Reader { return &d.buf }
func (d *mockDebugger) Failed() bool { return d.failed }
func (d *mockDebugger) Err() error { return d.err }
func (d *mockDebugger) Exited() bool { return d.exited }
func (d *mockDebugger) ExitCode() int { return d.code }
var (
_ Debugger = rootDebugger{}
_ Debugger = prefixedDebugger{}
_ MockDebugger = &mockDebugger{}
)