-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.go
167 lines (145 loc) · 2.88 KB
/
log.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
package building
import (
"flag"
"fmt"
"io"
"log"
"os"
"runtime"
"strings"
"time"
)
var (
Quiet = flag.Bool("q", false, "quiet output")
Verbose = flag.Bool("v", false, "verbose output")
)
func init() {
log.SetFlags(0)
// Manual flags parsing to disable logging before calling the target
// functions unless -v is passed.
*Quiet = true
for _, arg := range os.Args {
if arg == "-v" {
*Verbose = true
*Quiet = false
return
}
}
}
type failure struct {
}
func CatchFailure(start time.Time) {
if e := recover(); e != nil {
if _, ok := e.(failure); ok {
b.Debugf("build failed (took %s)", time.Since(start))
os.Exit(1)
}
panic(e)
}
b.Debugf("build finished (took %s)", time.Since(start))
}
func (b *B) Helper() {
var pc [2]uintptr
n := runtime.Callers(2, pc[:])
frames := runtime.CallersFrames(pc[:n])
frame, _ := frames.Next()
b.mutex.Lock()
defer b.mutex.Unlock()
if b.helpers == nil {
b.helpers = make(map[string]bool)
}
b.helpers[frame.Function] = true
}
func (b *B) location(suffix string) string {
var pc [10]uintptr
n := runtime.Callers(1, pc[:])
frames := runtime.CallersFrames(pc[:n])
for {
frame, more := frames.Next()
if frame.Function == "runtime.main" {
return ""
}
if !b.skip(frame) {
return fmt.Sprintf("%s:%d:%s", frame.File, frame.Line, suffix)
}
if !more {
return ""
}
}
}
func (b *B) skip(frame runtime.Frame) bool {
if strings.Contains(frame.Function, "github.com/mat007/brique") {
return true
}
if b == nil {
return false
}
b.mutex.Lock()
defer b.mutex.Unlock()
return b.helpers[frame.Function]
}
func (b *B) Fatal(v ...interface{}) {
log.Print(append([]interface{}{b.location(" ")}, v...)...)
panic(failure{})
}
func (b *B) Fatalf(format string, v ...interface{}) {
log.Printf(b.location(" ")+format, v...)
panic(failure{})
}
func (b *B) Fatalln(v ...interface{}) {
log.Println(append([]interface{}{b.location("")}, v...)...)
panic(failure{})
}
func (b *B) Print(v ...interface{}) {
if isInfo() {
log.Print(v...)
}
}
func (b *B) Printf(format string, v ...interface{}) {
if isInfo() {
log.Printf(format, v...)
}
}
func (b *B) Println(v ...interface{}) {
if isInfo() {
log.Println(v...)
}
}
func (b *B) Debug(v ...interface{}) {
if isDebug() {
log.Print(v...)
}
}
func (b *B) Debugf(format string, v ...interface{}) {
if isDebug() {
log.Printf(format, v...)
}
}
func (b *B) Debugln(v ...interface{}) {
if isDebug() {
log.Println(v...)
}
}
func (b *B) Assert(err error) {
if err != nil {
log.Println([]interface{}{b.location(""), err}...)
panic(failure{})
}
}
func (b *B) Check(err error) {
if err != nil {
log.Println([]interface{}{b.location(""), err}...)
}
}
func (b *B) Close(c io.Closer) {
err := c.Close()
if err != nil {
log.Println([]interface{}{b.location(""), err}...)
}
}
func isInfo() bool {
return !*Quiet
}
func isDebug() bool {
return isInfo() && *Verbose
}