-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdout_writer.go
82 lines (72 loc) · 1.58 KB
/
stdout_writer.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
package main
import (
"bytes"
"io"
"os"
"strings"
"time"
"github.com/mgutz/ansi"
)
type StdoutWriter struct {
out io.Writer
noColor bool
noGroup bool
noStream bool
timestamp bool
ingestionTime bool
}
func NewStdoutWriter(noColor, noGroup, noStream, timestamp, ingestion bool) (*StdoutWriter, error) {
w := &StdoutWriter{
out: os.Stdout,
noColor: noColor,
noGroup: noGroup,
noStream: noStream,
timestamp: timestamp,
ingestionTime: ingestion,
}
return w, nil
}
// <TODO> buffering
// <TODO> condition optimization
func (w *StdoutWriter) Write(stream chan Event) error {
buf := &bytes.Buffer{}
for {
event, ok := <-stream
if !ok {
return nil
}
buf.Reset()
if !w.noGroup {
if w.noColor {
buf.WriteString(event.Group + " ")
} else {
buf.WriteString(ansi.Color(event.Group, "green") + " ")
}
}
if !w.noStream {
if w.noColor {
buf.WriteString(event.Stream + " ")
} else {
buf.WriteString(ansi.Color(event.Stream, "cyan") + " ")
}
}
if w.timestamp {
if w.noColor {
buf.WriteString(event.Timestamp.Format(time.RFC3339) + " ")
} else {
buf.WriteString(ansi.Color(event.Timestamp.Format(time.RFC3339), "yellow") + " ")
}
}
if w.ingestionTime {
if w.noColor {
buf.WriteString(event.IngestionTime.Format(time.RFC3339) + " ")
} else {
buf.WriteString(ansi.Color(event.IngestionTime.Format(time.RFC3339), "blue") + " ")
}
}
buf.WriteString(strings.TrimSpace(event.Message))
buf.WriteString("\n")
buf.WriteTo(w.out)
}
return nil
}