-
Notifications
You must be signed in to change notification settings - Fork 0
/
record.go
75 lines (65 loc) · 1.21 KB
/
record.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
package iolog
import (
"fmt"
"sync"
"time"
)
type item struct {
rec *Record
next *item
}
var itemPool = sync.Pool{
New: func() interface{} { return new(item) },
}
func newItem(r *Record) *item {
i := itemPool.Get().(*item)
i.rec = r
i.next = nil
return i
}
func (i *item) free() {
i.rec = nil
i.next = nil
itemPool.Put(i)
}
type Record struct {
Tag string
Start time.Time
Stop time.Time
Data interface{}
Error error
}
var recordPool = sync.Pool{
New: func() interface{} { return new(Record) },
}
func newRecord(t string, s, f time.Time, e error) *Record {
r := recordPool.Get().(*Record)
r.Tag = t
r.Start = s
r.Stop = f
r.Error = e
r.Data = nil
return r
}
func (r *Record) free() {
r.Data = nil
r.Error = nil
recordPool.Put(r)
}
func (r *Record) String() string {
stop := r.Stop
if stop.IsZero() {
stop = r.Start
}
var datastr string
switch d := r.Data.(type) {
case []byte:
datastr = fmt.Sprintf("[% X]", d)
case fmt.Stringer:
datastr = d.String()
default:
datastr = fmt.Sprintf("%+v", d)
}
const tf = "2006-01-02T15:04:05.000-0700"
return fmt.Sprintf("%s %s (%s) %s / %s error: %v", r.Tag, datastr, stop.Sub(r.Start), r.Start.Format(tf), r.Start.Format(tf), r.Error)
}