-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvprint.go
233 lines (198 loc) · 4.61 KB
/
vprint.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
package rpc25519
import (
"fmt"
"io"
"os"
"path"
"reflect"
"runtime"
"runtime/debug"
"strconv"
"sync"
"time"
"4d63.com/tz"
)
// for tons of debug output
var verbose bool = false
var verboseVerbose bool = false
var gtz *time.Location
var chicago *time.Location
var utcTz *time.Location
var nyc *time.Location
var londonTz *time.Location
var frankfurt *time.Location
func init() {
// do this is ~/.bashrc so we get the default.
os.Setenv("TZ", "America/chicago")
var err error
chicago, err = tz.LoadLocation("America/Chicago")
panicOn(err)
utcTz, err = tz.LoadLocation("UTC")
panicOn(err)
nyc, err = tz.LoadLocation("America/New_York")
panicOn(err)
frankfurt, err = tz.LoadLocation("Europe/Berlin")
panicOn(err)
londonTz, err = tz.LoadLocation("Europe/London")
panicOn(err)
gtz = chicago
}
const rfc3339MsecTz0 = "2006-01-02T15:04:05.000Z07:00"
var myPid = os.Getpid()
var showPid bool
func pp(format string, a ...interface{}) {
if verboseVerbose {
tsPrintf(format, a...)
}
}
func zz(format string, a ...interface{}) {}
// useful during git bisect
var forceQuiet = false
func vv(format string, a ...interface{}) {
if !forceQuiet {
tsPrintf(format, a...)
}
}
func alwaysPrintf(format string, a ...interface{}) {
tsPrintf(format, a...)
}
var tsPrintfMut sync.Mutex
// time-stamped printf
func tsPrintf(format string, a ...interface{}) {
tsPrintfMut.Lock()
if showPid {
printf("\n%s [pid %v] %s ", fileLine(3), myPid, ts())
} else {
printf("\n%s %s ", fileLine(3), ts())
}
printf(format+"\n", a...)
tsPrintfMut.Unlock()
}
// get timestamp for logging purposes
func ts() string {
return time.Now().In(chicago).Format("2006-01-02 15:04:05.999 -0700 MST")
//return time.Now().In(nyc).Format("2006-01-02 15:04:05.999 -0700 MST")
}
// so we can multi write easily, use our own printf
var ourStdout io.Writer = os.Stdout
// Printf formats according to a format specifier and writes to standard output.
// It returns the number of bytes written and any write error encountered.
func printf(format string, a ...interface{}) (n int, err error) {
return fmt.Fprintf(ourStdout, format, a...)
}
func fileLine(depth int) string {
_, fileName, fileLine, ok := runtime.Caller(depth)
var s string
if ok {
s = fmt.Sprintf("%s:%d", path.Base(fileName), fileLine)
} else {
s = ""
}
return s
}
func p(format string, a ...interface{}) {
if verbose {
tsPrintf(format, a...)
}
}
func caller(upStack int) string {
// elide ourself and runtime.Callers
target := upStack + 2
pc := make([]uintptr, target+2)
n := runtime.Callers(0, pc)
f := runtime.Frame{Function: "unknown"}
if n > 0 {
frames := runtime.CallersFrames(pc[:n])
for i := 0; i <= target; i++ {
contender, more := frames.Next()
if i == target {
f = contender
}
if !more {
break
}
}
}
return f.Function
}
func panicOn(err error) {
if err != nil {
panic(err)
}
}
// return stack dump for calling goroutine.
func stack() string {
return string(debug.Stack())
}
// return stack dump for all goroutines
func allstacks() string {
buf := make([]byte, 8192)
for {
n := runtime.Stack(buf, true)
if n < len(buf) {
return string(buf[:n])
}
buf = make([]byte, 2*len(buf))
}
}
// IsNil uses reflect to to return true iff the face
// contains a nil pointer, map, array, slice, or channel.
func IsNil(face interface{}) bool {
if face == nil {
return true
}
switch reflect.TypeOf(face).Kind() {
case reflect.Ptr, reflect.Array, reflect.Map, reflect.Slice, reflect.Chan:
return reflect.ValueOf(face).IsNil()
}
return false
}
func thisStack() []byte {
buf := make([]byte, 8192)
nw := runtime.Stack(buf, false) // false => just us, no other goro.
buf = buf[:nw]
return buf
}
// GoroNumber returns the calling goroutine's number.
func GoroNumber() int {
buf := make([]byte, 48)
nw := runtime.Stack(buf, false) // false => just us, no other goro.
buf = buf[:nw]
// prefix "goroutine " is len 10.
i := 10
for buf[i] != ' ' && i < 30 {
i++
}
n, err := strconv.Atoi(string(buf[10:i]))
panicOn(err)
return n
}
// abort the program with error code 1 after printing msg to Stderr.
func stop(msg interface{}) {
switch e := msg.(type) {
case error:
fmt.Fprintf(os.Stderr, "%s: %s\n", fileLine(2), e.Error())
os.Exit(1)
default:
fmt.Fprintf(os.Stderr, "%s: %v\n", fileLine(2), msg)
os.Exit(1)
}
}
func stopOn(err error) {
if err == nil {
return
}
fmt.Fprintf(os.Stderr, "%s: %v\n", fileLine(2), err.Error())
os.Exit(1)
}
/*func fileLine(depth int) string {
_, fileName, fileLine, ok := runtime.Caller(depth)
var s string
if ok {
s = fmt.Sprintf("%s:%d", path.Base(fileName), fileLine)
} else {
s = ""
}
return s
}
*/