forked from buildkite/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
199 lines (161 loc) · 4.41 KB
/
logger.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package shell
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"regexp"
"runtime"
"testing"
)
// Logger represents a logger that outputs to a buildkite shell.
type Logger interface {
io.Writer
// Printf prints a line of output
Printf(format string, v ...interface{})
// Headerf prints a Buildkite formatted header
Headerf(format string, v ...interface{})
// Commentf prints a comment line, e.g `# my comment goes here`
Commentf(format string, v ...interface{})
// Errorf shows a Buildkite formatted error expands the previous group
Errorf(format string, v ...interface{})
// Warningf shows a buildkite bootstrap warning
Warningf(format string, v ...interface{})
// Promptf prints a shell prompt
Promptf(format string, v ...interface{})
}
// StderrLogger is a Logger that writes to Stderr
var StderrLogger = &WriterLogger{
Writer: os.Stderr,
Ansi: true,
}
// DiscardLogger discards all log messages
var DiscardLogger = &WriterLogger{
Writer: ioutil.Discard,
}
// WriterLogger provides a logger that writes to an io.Writer
type WriterLogger struct {
Writer io.Writer
Ansi bool
}
func (wl *WriterLogger) Write(b []byte) (int, error) {
wl.Printf("%s", b)
return len(b), nil
}
func (wl *WriterLogger) Printf(format string, v ...interface{}) {
fmt.Fprintf(wl.Writer, "%s", fmt.Sprintf(format, v...))
fmt.Fprintln(wl.Writer)
}
func (wl *WriterLogger) Headerf(format string, v ...interface{}) {
fmt.Fprintf(wl.Writer, "~~~ %s", fmt.Sprintf(format, v...))
fmt.Fprintln(wl.Writer)
}
func (wl *WriterLogger) Commentf(format string, v ...interface{}) {
if wl.Ansi {
wl.Printf(ansiColor("# %s", "90"), fmt.Sprintf(format, v...))
} else {
wl.Printf("# %s", fmt.Sprintf(format, v...))
}
}
func (wl *WriterLogger) Errorf(format string, v ...interface{}) {
if wl.Ansi {
wl.Printf(ansiColor("🚨 Error: %s", "31"), fmt.Sprintf(format, v...))
} else {
wl.Printf("🚨 Error: %s", fmt.Sprintf(format, v...))
}
wl.Printf("^^^ +++")
}
func (wl *WriterLogger) Warningf(format string, v ...interface{}) {
if wl.Ansi {
wl.Printf(ansiColor("⚠️ Warning: %s", "33"), fmt.Sprintf(format, v...))
} else {
wl.Printf("⚠️ Warning: %s", fmt.Sprintf(format, v...))
}
wl.Printf("^^^ +++")
}
func (wl *WriterLogger) Promptf(format string, v ...interface{}) {
prompt := "$"
if runtime.GOOS == "windows" {
prompt = ">"
}
if wl.Ansi {
wl.Printf(ansiColor(prompt, "90")+" %s", fmt.Sprintf(format, v...))
} else {
wl.Printf(prompt+" %s", fmt.Sprintf(format, v...))
}
}
func ansiColor(s, attributes string) string {
return fmt.Sprintf("\033[%sm%s\033[0m", attributes, s)
}
type TestingLogger struct {
*testing.T
}
func (tl TestingLogger) Write(b []byte) (int, error) {
tl.Logf("%s", b)
return len(b), nil
}
func (tl TestingLogger) Printf(format string, v ...interface{}) {
tl.Logf(format, v...)
}
func (tl TestingLogger) Headerf(format string, v ...interface{}) {
tl.Logf("~~~ "+format, v...)
}
func (tl TestingLogger) Commentf(format string, v ...interface{}) {
tl.Logf("# %s", fmt.Sprintf(format, v...))
}
func (tl TestingLogger) Errorf(format string, v ...interface{}) {
tl.Logf("🚨 Error: %s", fmt.Sprintf(format, v...))
}
func (tl TestingLogger) Warningf(format string, v ...interface{}) {
tl.Logf("⚠️ Warning: %s", fmt.Sprintf(format, v...))
}
func (tl TestingLogger) Promptf(format string, v ...interface{}) {
prompt := "$"
if runtime.GOOS == "windows" {
prompt = ">"
}
tl.Logf(prompt+" %s", fmt.Sprintf(format, v...))
}
type LoggerStreamer struct {
Logger Logger
Prefix string
started bool
buf *bytes.Buffer
offset int
}
var lineRegexp = regexp.MustCompile(`(?m:^(.*)\r?\n)`)
func NewLoggerStreamer(logger Logger) *LoggerStreamer {
return &LoggerStreamer{
Logger: logger,
buf: bytes.NewBuffer([]byte("")),
}
}
func (l *LoggerStreamer) Write(p []byte) (n int, err error) {
if bytes.ContainsRune(p, '\n') {
l.started = true
}
if n, err = l.buf.Write(p); err != nil {
return
}
err = l.Output()
return
}
func (l *LoggerStreamer) Close() error {
if remaining := l.buf.String()[l.offset:]; len(remaining) > 0 {
l.Logger.Printf("%s%s", l.Prefix, remaining)
}
l.buf = bytes.NewBuffer([]byte(""))
return nil
}
func (l *LoggerStreamer) Output() error {
if !l.started {
return nil
}
matches := lineRegexp.FindAllStringSubmatch(l.buf.String()[l.offset:], -1)
for _, match := range matches {
l.Logger.Printf("%s%s", l.Prefix, match[1])
l.offset += len(match[0])
}
return nil
}