forked from heroku/lumbermill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problog.go
92 lines (72 loc) · 1.34 KB
/
problog.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
package lpxgen
import (
"fmt"
"math/rand"
"sync"
)
type ProbLog struct {
logs map[Log]float32
sum float32
mutex sync.Mutex
currentLog Log
}
func NewProbLog(logs map[Log]float32) *ProbLog {
var sum float32
for _, v := range logs {
sum += v
}
return &ProbLog{
logs: logs,
currentLog: DefaultLog{},
}
}
func (p *ProbLog) Add(l Log, prob float32) {
defer p.mutex.Unlock()
p.mutex.Lock()
p.logs[l] = prob
p.sum += prob
}
func (p *ProbLog) PrivalVersion() string {
return p.currentLog.PrivalVersion()
}
func (p *ProbLog) Time() string {
return p.currentLog.Time()
}
func (p *ProbLog) Hostname() string {
return p.currentLog.Hostname()
}
func (p *ProbLog) Name() string {
return p.currentLog.Name()
}
func (p *ProbLog) Procid() string {
return p.currentLog.Procid()
}
func (p *ProbLog) Msgid() string {
return p.currentLog.Msgid()
}
func (p *ProbLog) Msg() string {
return p.currentLog.Msg()
}
func (p *ProbLog) String() string {
defer p.mutex.Unlock()
p.mutex.Lock()
p.currentLog = p.nextLog()
s := FormatSyslog(p.currentLog)
fmt.Println(s)
return fmt.Sprintf("%d %s", len(s), s)
}
func (p *ProbLog) nextLog() Log {
var l Log
var v float32
if len(p.logs) == 0 {
return p.currentLog
}
value := rand.Float32() * p.sum
for l, v = range p.logs {
value -= v
if v < 0 {
return l
}
}
return l
}