-
Notifications
You must be signed in to change notification settings - Fork 42
/
common.go
44 lines (39 loc) · 837 Bytes
/
common.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
package main
import (
"bytes"
"fmt"
"os"
"runtime"
"time"
)
type StopWatch struct {
start time.Time
Elapsed time.Duration
}
func (s *StopWatch) Start() {
s.start = time.Now()
}
func (s *StopWatch) Stop() {
s.Elapsed = time.Now().Sub(s.start)
}
func TraceException(msg interface{}) {
switch {
case Verbosity > 1:
// print recovered error and stacktrace
var buffer bytes.Buffer
buffer.WriteString(fmt.Sprintf("recover: %v\n", msg))
for skip := 1; ; skip++ {
pc, file, line, ok := runtime.Caller(skip)
if !ok {
break
}
f := runtime.FuncForPC(pc)
buffer.WriteString(fmt.Sprintf("\t%s:%d %s()\n", file, line, f.Name()))
}
buffer.WriteString("\n")
fmt.Fprint(os.Stderr, buffer.String())
case Verbosity > 0:
// print recovered error only
fmt.Fprintf(os.Stderr, "recover: %v\n", msg)
}
}