/
debug.go
293 lines (230 loc) · 5.68 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
287
288
289
290
291
292
293
package debug
import (
"fmt"
"io"
"log"
"os"
"path"
"path/filepath"
"runtime"
"strconv"
"strings"
)
var (
// Stdout is exported for tests.
Stdout io.Writer = os.Stdout
// Stderr is exported for tests.
Stderr io.Writer = os.Stderr
// LogWriter is exposed for consuming extra command output, if needed.
LogWriter io.Writer = io.Discard
)
var opts struct {
logger *log.Logger
funcs map[string]bool
files map[string]bool
logFile *os.File
logSecrets bool
verbosity int
}
// v is a verbosity level.
type v int
// V returns a logger at the given verbosity level.
func V(n int) v {
return v(n)
}
var logFn = doNotLog
// make sure all initializations happens before the init func.
var enabled = initDebug()
func initDebug() bool {
if opts.logFile != nil {
_ = opts.logFile.Close()
}
if l := os.Getenv("GOPASS_DEBUG_VERBOSE"); l != "" {
if iv, err := strconv.Atoi(l); err == nil {
opts.verbosity = iv
}
}
if os.Getenv("GOPASS_DEBUG") == "" && os.Getenv("GOPASS_DEBUG_LOG") == "" {
logFn = doNotLog
return false
}
// we need to explicitly set logSecrets to false in case tests run under an environment
// where GOPASS_DEBUG_LOG_SECRETS is true. Otherwise setting it to false in the test
// context won't have any effect.
opts.logSecrets = false
if sv := os.Getenv("GOPASS_DEBUG_LOG_SECRETS"); sv != "" && sv != "false" {
opts.logSecrets = true
}
initDebugLogger()
initDebugTags()
logFn = doLog
return true
}
func initDebugLogger() {
debugfile := os.Getenv("GOPASS_DEBUG_LOG")
if debugfile == "" {
opts.logger = log.New(os.Stderr, "", log.Ldate|log.Lmicroseconds)
LogWriter = os.Stderr
return
}
f, err := os.OpenFile(debugfile, os.O_WRONLY|os.O_APPEND, 0o600)
if err == nil {
// seek to the end of the file (offset, whence [2 = end])
_, err := f.Seek(0, 2)
if err != nil {
_ = f.Close()
fmt.Fprintf(Stderr, "unable to seek to end of %v: %v\n", debugfile, err)
os.Exit(3)
}
}
if err != nil && os.IsNotExist(err) {
f, err = os.OpenFile(debugfile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
}
if err != nil {
fmt.Fprintf(Stderr, "unable to open debug log file %v: %v\n", debugfile, err)
os.Exit(2)
}
opts.logFile = f
opts.logger = log.New(f, "", log.Ldate|log.Lmicroseconds)
LogWriter = f
}
func parseFilter(envname string, pad func(string) string) map[string]bool {
filter := make(map[string]bool)
env := os.Getenv(envname)
if env == "" {
return filter
}
for _, fn := range strings.Split(env, ",") {
t := pad(strings.TrimSpace(fn))
val := true
if t[0] == '-' {
val = false
t = t[1:]
} else if t[0] == '+' {
val = true
t = t[1:]
}
// test pattern
_, err := path.Match(t, "")
if err != nil {
fmt.Fprintf(Stderr, "error: invalid pattern %q: %v\n", t, err)
os.Exit(5)
}
filter[t] = val
}
return filter
}
func padFunc(s string) string {
if s == "all" {
return s
}
return s
}
func padFile(s string) string {
if s == "all" {
return s
}
if !strings.Contains(s, "/") {
s = "*/" + s
}
if !strings.Contains(s, ":") {
s += ":*"
}
return s
}
func initDebugTags() {
opts.funcs = parseFilter("GOPASS_DEBUG_FUNCS", padFunc)
opts.files = parseFilter("GOPASS_DEBUG_FILES", padFile)
}
func getPosition(offset int) (fn, dir, file string, line int) { //nolint:nonamedreturns
pc, file, line, ok := runtime.Caller(3 + offset)
if !ok {
return "", "", "", 0
}
dirname := filepath.Base(filepath.Dir(file))
filename := filepath.Base(file)
f := runtime.FuncForPC(pc)
return path.Base(f.Name()), dirname, filename, line
}
func checkFilter(filter map[string]bool, key string) bool {
// check if exact match
if v, ok := filter[key]; ok {
return v
}
// check globbing
for k, v := range filter {
if m, _ := path.Match(k, key); m {
return v
}
}
// check if tag "all" is enabled
if v, ok := filter["all"]; ok && v {
return true
}
return false
}
// Log logs a statement to Stderr (unless filtered) and the
// debug log file (if enabled), but only if the verbosity
// level is greater or equal to the given level.
func (n v) Log(f string, args ...any) {
logFn(int(n), 0, f, args...)
}
// Log logs a statement to Stderr (unless filtered) and the
// debug log file (if enabled).
func Log(f string, args ...any) {
logFn(0, 0, f, args...)
}
// LogN logs a statement to Stderr (unless filtered) and the
// debug log file (if enabled). The offset will be applied to
// the runtime position.
func LogN(offset int, f string, args ...any) {
logFn(0, offset, f, args...)
}
func doNotLog(verbosity, offset int, f string, args ...any) {}
func doLog(verbosity, offset int, f string, args ...any) {
// if the log message is too verbose for the requested verbosity level, skip it
if verbosity > opts.verbosity {
return
}
fn, dir, file, line := getPosition(offset)
if len(f) == 0 || f[len(f)-1] != '\n' {
f += "\n"
}
type Shortener interface {
Str() string
}
type Safer interface {
SafeStr() string
}
argsi := make([]any, len(args))
for i, item := range args {
argsi[i] = item
if secreter, ok := item.(Safer); ok && !opts.logSecrets {
argsi[i] = secreter.SafeStr()
continue
}
if shortener, ok := item.(Shortener); ok {
argsi[i] = shortener.Str()
}
}
pos := fmt.Sprintf("%s/%s:%d", dir, file, line)
formatString := fmt.Sprintf("%s\t%s\t%s", pos, fn, f)
dbgprint := func() {
fmt.Fprintf(Stderr, formatString, argsi...)
}
if opts.logger != nil {
opts.logger.Printf(formatString, argsi...)
}
filename := fmt.Sprintf("%s/%s:%d", dir, file, line)
if checkFilter(opts.files, filename) {
dbgprint()
return
}
if checkFilter(opts.funcs, fn) {
dbgprint()
}
}
// IsEnabled returns true if debug logging was enabled.
func IsEnabled() bool {
return enabled
}