-
Notifications
You must be signed in to change notification settings - Fork 1
/
logcounter.go
56 lines (47 loc) · 1013 Bytes
/
logcounter.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
package logger
import "sync"
type logOrigin struct {
File string
Line int
Level Level
Msg string
}
type logCounter struct {
rwMutex sync.RWMutex
data map[logOrigin]uint32
}
func (lc *logCounter) update(lo logOrigin) {
lc.rwMutex.Lock()
defer lc.rwMutex.Unlock()
lc.data[lo]++
}
func (lc *logCounter) Lookup(key logOrigin) (count uint32, found bool) {
lc.rwMutex.RLock()
defer lc.rwMutex.RUnlock()
count, found = lc.data[key]
return count, found
}
func (lc *logCounter) dump(flush bool) map[logOrigin]uint32 {
lc.rwMutex.RLock()
defer lc.rwMutex.RUnlock()
dump := make(map[logOrigin]uint32, len(lc.data))
for k, v := range lc.data {
dump[k] = v
if flush {
delete(lc.data, k)
}
}
return dump
}
func (lc *logCounter) Dump() map[logOrigin]uint32 {
return lc.dump(false)
}
func (lc *logCounter) Flush() map[logOrigin]uint32 {
return lc.dump(true)
}
func newLogCounter() *logCounter {
return &logCounter{
rwMutex: sync.RWMutex{},
data: map[logOrigin]uint32{},
}
}