-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamer.go
151 lines (122 loc) · 2.87 KB
/
streamer.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package hqgolog
import (
"bytes"
"io"
"os"
"strings"
)
type Streamer struct { //nolint:govet // To be refactored.
Logger *Logger
buf *bytes.Buffer
// If prefix == stdout, colors green
// If prefix == stderr, colors red
// Else, prefix is taken as-is, and prepended to anything
// you throw at Write()
prefix string
// if true, saves output in memory
record bool
persist string
// Adds color to stdout & stderr if terminal supports it
colorOkay string
colorFail string
colorReset string
}
// func NewLogstreamerForWriter(prefix string, writer io.Writer) *Streamer {
// logger := log.New(writer, prefix, 0)
// return NewLogstreamer(logger, "", false)
// }
// func NewLogstreamerForStdout(prefix string) *Streamer {
// // logger := log.New(os.Stdout, prefix, log.Ldate|log.Ltime)
// logger := log.New(os.Stdout, prefix, 0)
// return NewLogstreamer(logger, "", false)
// }
// func NewLogstreamerForStderr(prefix string) *Streamer {
// logger := log.New(os.Stderr, prefix, 0)
// return NewLogstreamer(logger, "", false)
// }
func NewLogstreamer(logger *Logger, prefix string, record bool) *Streamer {
streamer := &Streamer{
Logger: logger,
buf: bytes.NewBuffer([]byte("")),
prefix: prefix,
record: record,
persist: "",
colorOkay: "",
colorFail: "",
colorReset: "",
}
if strings.HasPrefix(os.Getenv("TERM"), "xterm") {
streamer.colorOkay = "\x1b[32m"
streamer.colorFail = "\x1b[31m"
streamer.colorReset = "\x1b[0m"
}
return streamer
}
func (l *Streamer) Write(p []byte) (n int, err error) {
if n, err = l.buf.Write(p); err != nil {
return
}
err = l.OutputLines()
return
}
func (l *Streamer) Close() error {
if err := l.Flush(); err != nil {
return err
}
l.buf = bytes.NewBuffer([]byte(""))
return nil
}
func (l *Streamer) Flush() error {
p := make([]byte, l.buf.Len())
if _, err := l.buf.Read(p); err != nil {
return err
}
l.out(string(p))
return nil
}
func (l *Streamer) OutputLines() error {
for {
line, err := l.buf.ReadString('\n')
if len(line) > 0 {
if strings.HasSuffix(line, "\n") {
l.out(line)
} else {
// put back into buffer, it's not a complete line yet
// Close() or Flush() have to be used to flush out
// the last remaining line if it does not end with a newline
if _, err = l.buf.WriteString(line); err != nil {
return err
}
}
}
if err == io.EOF {
break
}
if err != nil {
return err
}
}
return nil
}
func (l *Streamer) FlushRecord() string {
buffer := l.persist
l.persist = ""
return buffer
}
func (l *Streamer) out(str string) {
if len(str) < 1 {
return
}
if l.record {
l.persist += str
}
switch l.prefix {
case "stdout":
str = l.colorOkay + l.prefix + l.colorReset + " " + str
case "stderr":
str = l.colorFail + l.prefix + l.colorReset + " " + str
default:
str = l.prefix + str
}
l.Logger.Print().Msg(str)
}