forked from jaytaylor/logserver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
entry.go
74 lines (65 loc) · 1.38 KB
/
entry.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
package log
import (
"io"
"regexp"
"time"
)
type (
Entry struct {
Time time.Time
Application string
Process string
Data []byte
}
EntryFilter struct {
Application string
Process string
Data *regexp.Regexp
}
)
func (this *Entry) Line() []byte {
return append([]byte(this.Time.String()+" "+this.Application+"["+this.Process+"]: "), this.Data...)
}
func (this EntryFilter) Include(entry Entry) bool {
if !(this.Application == "" || this.Application == entry.Application) {
return false
}
if !(this.Process == "" || this.Process == entry.Process) {
return false
}
if !(this.Data == nil || this.Data.Match(entry.Data)) {
return false
}
return true
}
func ReadEntry(reader io.Reader) (Entry, error) {
var entry Entry
return entry, Read(reader, &entry.Time, &entry.Application, &entry.Process, &entry.Data)
}
func (this Entry) Write(writer io.Writer) error {
return Write(writer, this.Time, this.Application, this.Process, this.Data)
}
// Throttle a channel so that it dequeues quickly
// and drops old messages
func Throttle(c <-chan Entry, sz int) <-chan Entry {
buffer := make(chan Entry, sz)
go func() {
for entry := range c {
select {
case buffer <- entry:
continue
default:
}
select {
case <-buffer:
default:
}
select {
case buffer <- entry:
default:
}
}
close(buffer)
}()
return buffer
}