-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlogstreamer.go
146 lines (122 loc) · 2.87 KB
/
logstreamer.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
package logstreamer
import (
"bytes"
"io"
"log"
"os"
"strings"
)
type Logstreamer struct {
Logger *log.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) *Logstreamer {
logger := log.New(writer, prefix, 0)
return NewLogstreamer(logger, "", false)
}
func NewLogstreamerForStdout(prefix string) *Logstreamer {
// 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) *Logstreamer {
logger := log.New(os.Stderr, prefix, 0)
return NewLogstreamer(logger, "", false)
}
func NewLogstreamer(logger *log.Logger, prefix string, record bool) *Logstreamer {
streamer := &Logstreamer{
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 *Logstreamer) Write(p []byte) (n int, err error) {
if n, err = l.buf.Write(p); err != nil {
return
}
err = l.OutputLines()
return
}
func (l *Logstreamer) Close() error {
if err := l.Flush(); err != nil {
return err
}
l.buf = bytes.NewBuffer([]byte(""))
return nil
}
func (l *Logstreamer) 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 *Logstreamer) 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 *Logstreamer) FlushRecord() string {
buffer := l.persist
l.persist = ""
return buffer
}
func (l *Logstreamer) out(str string) {
if len(str) < 1 {
return
}
if l.record == true {
l.persist = l.persist + str
}
if l.prefix == "stdout" {
str = l.colorOkay + l.prefix + l.colorReset + " " + str
} else if l.prefix == "stderr" {
str = l.colorFail + l.prefix + l.colorReset + " " + str
} else {
str = l.prefix + str
}
l.Logger.Print(str)
}