-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq.go
54 lines (45 loc) · 1.24 KB
/
q.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
// Copyright 2016 Ryan Boehning. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package q
import (
"fmt"
)
// nolint: gochecknoglobals
var (
// std is the singleton logger.
std logger
)
// Q pretty-prints the given arguments to the $TMPDIR/q log file.
func Q(v ...interface{}) {
std.mu.Lock()
defer std.mu.Unlock()
// Flush the buffered writes to disk.
defer func() {
if err := std.flush(); err != nil {
fmt.Println(err)
}
}()
args := formatArgs(v...)
funcName, file, line, err := getCallerInfo()
if err != nil {
std.output(args...) // no name=value printing
return
}
// Print a header line if this q.Q() call is in a different file or
// function than the previous q.Q() call, or if the 2s timer expired.
// A header line looks like this: [14:00:36 main.go main.main:122].
header := std.header(funcName, file, line)
if header != "" {
fmt.Fprint(&std.buf, "\n", header, "\n")
}
// q.Q(foo, bar, baz) -> []string{"foo", "bar", "baz"}
names, err := argNames(file, line)
if err != nil {
std.output(args...) // no name=value printing
return
}
// Convert the arguments to name=value strings.
args = prependArgName(names, args)
std.output(args...)
}