-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathtracer.go
58 lines (47 loc) · 976 Bytes
/
tracer.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
package cleisthenes
import (
"fmt"
"strings"
"sync"
"github.com/go-kit/kit/log"
"github.com/DE-labtory/iLogger"
)
type Tracer interface {
Log(keyvals ...string)
Trace()
}
type MemCacheTracer struct {
lock sync.RWMutex
logger log.Logger
traceList []string
}
func NewMemCacheTracer() *MemCacheTracer {
return &MemCacheTracer{
lock: sync.RWMutex{},
traceList: make([]string, 0),
}
}
func (t *MemCacheTracer) Log(keyvals ...string) {
t.lock.Lock()
defer t.lock.Unlock()
if len(keyvals) == 0 {
return
}
if len(keyvals)%2 == 1 {
keyvals = append(keyvals, "")
}
kvs := make([]string, 0)
for i := 0; i < len(keyvals); i += 2 {
k, v := keyvals[i], keyvals[i+1]
kvs = append(kvs, fmt.Sprintf("%s=%s", k, v))
}
trace := strings.Join(kvs, " ")
t.traceList = append(t.traceList, trace)
}
func (t *MemCacheTracer) Trace() {
t.lock.Lock()
defer t.lock.Unlock()
for _, trace := range t.traceList {
iLogger.Info(nil, trace)
}
}