-
Notifications
You must be signed in to change notification settings - Fork 394
/
conslogging.go
632 lines (560 loc) · 15.8 KB
/
conslogging.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
package conslogging
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"sync"
"unicode/utf8"
"github.com/earthly/earthly/cleanup"
"github.com/fatih/color"
)
// ColorMode is the mode in which colors are represented in the output.
type ColorMode int
const (
// AutoColor automatically detects the presence of a TTY to decide if
// color should be used.
AutoColor ColorMode = iota
// NoColor disables use of color.
NoColor
// ForceColor forces use of color.
ForceColor
)
const (
// NoPadding means the old behavior of printing the full target only.
NoPadding int = -1
// DefaultPadding always prints 20 characters for the target, right
// justified. If it is longer, it prints the right 20 characters.
DefaultPadding int = 20
)
// LogLevel defines which types of log messages are displayed (e.g. warning, info, verbose)
type LogLevel int
const (
// Silent silences logging
Silent LogLevel = iota
// Warn only display warning log messages
Warn
// Info displays info and higher priority log messages
Info
// Verbose displays verbose and higher priority log messages
Verbose
// Debug displays all log messages
Debug
)
const barWidth = 80
var currentConsoleMutex sync.Mutex
// ConsoleLogger is a writer for consoles.
type ConsoleLogger struct {
prefix string
// metadataMode are printed in a different color.
metadataMode bool
// isLocal has a special prefix *local* added.
isLocal bool
// salt is a salt used for color consistency
// (the same salt will get the same color).
salt string
colorMode ColorMode
isCached bool
isFailed bool
githubAnnotations bool
logLevel LogLevel
// The following are shared between instances and are protected by the mutex.
mu *sync.Mutex
saltColors map[string]*color.Color
nextColorIndex *int
errW io.Writer
consoleErrW io.Writer
prefixWriter PrefixWriter
trailingLine bool
prefixPadding int
bb *BundleBuilder
}
// Current returns the current console.
func Current(colorMode ColorMode, prefixPadding int, logLevel LogLevel, githubAnnotations bool) ConsoleLogger {
return New(getCompatibleStderr(), ¤tConsoleMutex, colorMode, prefixPadding, logLevel, githubAnnotations)
}
// New returns a new ConsoleLogger with a predefined target writer.
func New(w io.Writer, mu *sync.Mutex, colorMode ColorMode, prefixPadding int, logLevel LogLevel, githubAnnotations bool) ConsoleLogger {
if mu == nil {
mu = &sync.Mutex{}
}
return ConsoleLogger{
consoleErrW: w,
errW: w,
colorMode: colorMode,
saltColors: make(map[string]*color.Color),
nextColorIndex: new(int),
prefixPadding: prefixPadding,
mu: mu,
logLevel: logLevel,
githubAnnotations: githubAnnotations,
}
}
func (cl ConsoleLogger) clone() ConsoleLogger {
return ConsoleLogger{
consoleErrW: cl.consoleErrW,
errW: cl.errW,
prefixWriter: cl.prefixWriter,
prefix: cl.prefix,
metadataMode: cl.metadataMode,
isLocal: cl.isLocal,
logLevel: cl.logLevel,
salt: cl.salt,
isCached: cl.isCached,
isFailed: cl.isFailed,
githubAnnotations: cl.githubAnnotations,
saltColors: cl.saltColors,
colorMode: cl.colorMode,
nextColorIndex: cl.nextColorIndex,
prefixPadding: cl.prefixPadding,
mu: cl.mu,
bb: cl.bb,
}
}
// WithPrefix returns a ConsoleLogger with a prefix added.
func (cl ConsoleLogger) WithPrefix(prefix string) ConsoleLogger {
return cl.WithPrefixAndSalt(prefix, prefix)
}
// WithPrefixAndSalt returns a ConsoleLogger with a prefix and a seed added.
func (cl ConsoleLogger) WithPrefixAndSalt(prefix string, salt string) ConsoleLogger {
ret := cl.clone()
if cl.bb != nil {
ret.errW = io.MultiWriter(cl.consoleErrW, cl.bb.PrefixWriter(prefix))
}
ret.prefix = prefix
ret.salt = salt
if ret.prefixWriter != nil {
ret.prefixWriter = ret.prefixWriter.WithPrefix(prefix)
ret.errW = ret.prefixWriter
}
return ret
}
// WithMetadataMode returns a ConsoleLogger with metadata printing mode set.
func (cl ConsoleLogger) WithMetadataMode(metadataMode bool) ConsoleLogger {
ret := cl.clone()
ret.metadataMode = metadataMode
return ret
}
// WithLocal returns a ConsoleLogger with local set.
func (cl ConsoleLogger) WithLocal(isLocal bool) ConsoleLogger {
ret := cl.clone()
ret.isLocal = isLocal
return ret
}
// Prefix returns the console's prefix.
func (cl ConsoleLogger) Prefix() string {
return cl.prefix
}
// Salt returns the console's salt.
func (cl ConsoleLogger) Salt() string {
return cl.salt
}
// WithCached returns a ConsoleLogger with isCached flag set accordingly.
func (cl ConsoleLogger) WithCached(isCached bool) ConsoleLogger {
ret := cl.clone()
ret.isCached = isCached
return ret
}
// WithFailed returns a ConsoleLogger with isFailed flag set accordingly.
func (cl ConsoleLogger) WithFailed(isFailed bool) ConsoleLogger {
ret := cl.clone()
ret.isFailed = isFailed
return ret
}
// WithWriter returns a ConsoleLogger with stderr pointed at the provided io.Writer.
func (cl ConsoleLogger) WithWriter(w io.Writer) ConsoleLogger {
ret := cl.clone()
ret.errW = w
return ret
}
// WithPrefixWriter returns a ConsoleLogger with a prefix writer.
func (cl ConsoleLogger) WithPrefixWriter(w PrefixWriter) ConsoleLogger {
ret := cl.clone()
ret.prefixWriter = w
ret.errW = w
return ret
}
// WithLogBundleWriter returns a ConsoleLogger with a BundleWriter attached to capture output into a log bundle, for upload to log sharing.
func (cl ConsoleLogger) WithLogBundleWriter(entrypoint string, collection *cleanup.Collection) ConsoleLogger {
ret := cl.clone()
ret.bb = NewBundleBuilder(entrypoint, collection)
fullW := ret.bb.PrefixWriter(fullLog)
ret.consoleErrW = io.MultiWriter(ret.consoleErrW, fullW)
ret.errW = ret.consoleErrW
return ret
}
// PrintPhaseHeader prints the phase header.
func (cl ConsoleLogger) PrintPhaseHeader(phase string, disabled bool, special string) {
w := new(bytes.Buffer)
cl.mu.Lock()
defer func() {
_, _ = w.WriteTo(cl.errW)
cl.mu.Unlock()
}()
msg := phase
c := cl.color(phaseColor)
if disabled {
c = cl.color(disabledPhaseColor)
msg = fmt.Sprintf("%s (disabled)", msg)
} else if special != "" {
c = cl.color(specialPhaseColor)
msg = fmt.Sprintf("%s (%s)", msg, special)
}
underlineLength := utf8.RuneCountInString(msg) + 2
if underlineLength < barWidth {
underlineLength = barWidth
}
cl.printGithubActionsControl(groupCommand, msg)
c.Fprintf(w, " %s", msg)
fmt.Fprintf(w, "\n")
c.Fprintf(w, "%s", strings.Repeat("—", underlineLength))
fmt.Fprintf(w, "\n\n")
}
// PrintPhaseFooter prints the phase footer.
func (cl ConsoleLogger) PrintPhaseFooter(phase string, disabled bool, special string) {
w := new(bytes.Buffer)
cl.mu.Lock()
defer func() {
_, _ = w.WriteTo(cl.errW)
cl.mu.Unlock()
}()
c := cl.color(noColor)
cl.printGithubActionsControl(endGroupCommand, phase)
c.Fprintf(w, "\n")
}
// PrintSuccess prints the success message.
func (cl ConsoleLogger) PrintSuccess() {
cl.PrintBar(successColor, "🌍 Earthly Build ✅ SUCCESS", "")
}
// PrintFailure prints the failure message.
func (cl ConsoleLogger) PrintFailure(phase string) {
cl.PrintBar(warnColor, "❌ FAILURE", phase)
}
// PrefixColor returns the color used for the prefix.
func (cl ConsoleLogger) PrefixColor() *color.Color {
c, found := cl.saltColors[cl.salt]
if !found {
c = availablePrefixColors[*cl.nextColorIndex]
cl.saltColors[cl.salt] = c
*cl.nextColorIndex = (*cl.nextColorIndex + 1) % len(availablePrefixColors)
}
return cl.color(c)
}
// Prints a GitHub Actions summary message to GITHUB_STEP_SUMMARY
func (cl *ConsoleLogger) PrintGHASummary(message string) {
if !cl.githubAnnotations {
return
}
path := os.Getenv("GITHUB_STEP_SUMMARY")
if path == "" {
w := new(bytes.Buffer)
defer func() {
_, _ = w.WriteTo(cl.errW)
}()
fmt.Print(w, message)
return
}
file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return
}
defer file.Close()
_, _ = file.WriteString(message + "\n")
}
type GHAError struct {
message string
file string
line int32
col int32
}
func (e *GHAError) FormatedMessage() string {
if e.file != "" {
return fmt.Sprintf("file=%s,line=%d,col=%d,title=Error::%s", e.file, e.line, e.col, e.message)
} else {
return fmt.Sprintf("title=Error::%s", e.message)
}
}
type GHAErrorOpt func(*GHAError)
func WithGHASourceLocation(file string, line, col int32) GHAErrorOpt {
return func(cfg *GHAError) {
cfg.file = file
cfg.line = line
cfg.col = col
}
}
// PrintGHAError constructs a GitHub Actions error message.
func (cl *ConsoleLogger) PrintGHAError(message string, fns ...GHAErrorOpt) {
cl.mu.Lock()
defer cl.mu.Unlock()
ge := GHAError{
message: message,
}
for _, fn := range fns {
fn(&ge)
}
cl.printGithubActionsControl(errorCommand, ge.FormatedMessage())
}
type ghHeader string
const (
errorCommand ghHeader = "::error"
groupCommand ghHeader = "::group::"
endGroupCommand ghHeader = "::endgroup::"
)
// Print GHA control messages like ::group and ::error
func (cl ConsoleLogger) printGithubActionsControl(header ghHeader, format string, a ...any) {
if !cl.githubAnnotations {
return
}
// Assumes mu locked.
w := new(bytes.Buffer)
defer func() {
_, _ = w.WriteTo(cl.errW)
}()
if !strings.HasSuffix(format, "\n") {
format += "\n"
}
fullFormat := string(header) + " " + format
fmt.Fprintf(w, fullFormat, a...)
}
// PrintBar prints an earthly message bar.
func (cl ConsoleLogger) PrintBar(c *color.Color, msg, phase string) {
w := new(bytes.Buffer)
cl.mu.Lock()
defer func() {
_, _ = w.WriteTo(cl.errW)
cl.mu.Unlock()
}()
c = cl.color(c)
center := msg
if phase != "" {
center = fmt.Sprintf("%s [%s]", msg, phase)
}
center = fmt.Sprintf(" %s ", center)
sideWidth := (barWidth - utf8.RuneCountInString(center)) / 2
if sideWidth < 0 {
sideWidth = 0
}
eqBar := strings.Repeat("=", sideWidth)
leftBar := eqBar
rightBar := eqBar
if utf8.RuneCountInString(center)%2 == 1 && sideWidth > 0 {
// Ensure the width is always barWidth
rightBar += "="
}
fmt.Fprintf(w, "\n")
c.Fprintf(w, "%s%s%s", leftBar, center, rightBar)
fmt.Fprintf(w, "\n\n")
}
// Warnf prints a warning message in red to errWriter.
func (cl ConsoleLogger) Warnf(format string, args ...interface{}) {
c := cl.color(warnColor)
cl.colorPrintf(Warn, c, format, args...)
}
// VerboseWarnf prints a warning message in red to errWriter when verbose flag is set.
func (cl ConsoleLogger) VerboseWarnf(format string, args ...interface{}) {
if cl.logLevel < Verbose {
return
}
cl.Warnf(format, args...)
}
// HelpPrintf prints formatted text to the console with `Help:` prefix in a specific color
func (cl ConsoleLogger) HelpPrintf(format string, args ...interface{}) {
cl.ColorPrintf(cl.color(helpColor), fmt.Sprintf("\nHelp: %s\n", format), args...)
}
// Printf prints formatted text to the console.
func (cl ConsoleLogger) Printf(format string, args ...interface{}) {
c := cl.color(noColor)
if cl.metadataMode {
c = cl.color(metadataModeColor)
}
cl.ColorPrintf(c, format, args...)
}
func (cl ConsoleLogger) colorPrintf(level LogLevel, c *color.Color, format string, args ...interface{}) {
if cl.logLevel < level {
return
}
w := new(bytes.Buffer)
cl.mu.Lock()
defer func() {
_, _ = w.WriteTo(cl.errW)
cl.mu.Unlock()
}()
text := fmt.Sprintf(format, args...)
text = strings.TrimSuffix(text, "\n")
for _, line := range strings.Split(text, "\n") {
cl.printPrefix(w)
c.Fprintf(w, "%s", line)
// Don't use a background color for \n.
noColor.Fprintf(w, "\n")
}
}
func (cl ConsoleLogger) ColorPrintf(c *color.Color, format string, args ...interface{}) {
cl.colorPrintf(Info, c, format, args...)
}
// PrintBytes prints bytes directly to the console.
func (cl ConsoleLogger) PrintBytes(data []byte) {
w := new(bytes.Buffer)
w.Grow(len(data) + len(data)/4)
cl.mu.Lock()
defer func() {
_, _ = w.WriteTo(cl.errW)
cl.mu.Unlock()
}()
c := cl.color(noColor)
if cl.metadataMode {
c = cl.color(metadataModeColor)
}
output := make([]byte, 0, len(data))
for len(data) > 0 {
r, size := utf8.DecodeRune(data)
ch := data[:size]
data = data[size:]
switch r {
case '\r':
output = append(output, ch...)
cl.trailingLine = false
case '\n':
output = append(output, ch...)
cl.trailingLine = false
default:
if !cl.trailingLine {
if len(output) > 0 {
c.Fprintf(w, "%s", string(output))
output = output[:0]
}
cl.printPrefix(w)
cl.trailingLine = true
}
output = append(output, ch...)
}
}
if len(output) > 0 {
c.Fprintf(w, "%s", string(output))
// output = output[:0] // needed if output is used further in the future
}
}
// VerbosePrintf prints formatted text to the console when verbose flag is set.
func (cl ConsoleLogger) VerbosePrintf(format string, args ...interface{}) {
if cl.logLevel < Verbose {
return
}
cl.WithMetadataMode(true).Printf(format, args...)
}
// VerboseBytes prints bytes directly to the console when verbose flag is set.
func (cl ConsoleLogger) VerboseBytes(data []byte) {
if cl.logLevel < Verbose {
return
}
cl.WithMetadataMode(true).PrintBytes(data)
}
// DebugPrintf prints formatted text to the console when debug flag is set.
func (cl ConsoleLogger) DebugPrintf(format string, args ...interface{}) {
if cl.logLevel < Debug {
return
}
cl.WithMetadataMode(true).Printf(format, args...)
}
// DebugBytes prints bytes directly to the console when debug flag is set.
func (cl ConsoleLogger) DebugBytes(data []byte) {
if cl.logLevel < Debug {
return
}
cl.WithMetadataMode(true).PrintBytes(data)
}
func (cl ConsoleLogger) printPrefix(w io.Writer) {
// Assumes mu locked.
if cl.prefixWriter != nil {
// When the prefix writer is in use, we don't need to print the prefix.
return
}
if cl.prefix == "" {
return
}
c := cl.PrefixColor()
c.Fprintf(w, "%s", prettyPrefix(cl.prefixPadding, cl.prefix))
if cl.isLocal {
fmt.Fprintf(w, " *")
cl.color(localColor).Fprintf(w, "local")
fmt.Fprintf(w, "*")
}
if cl.isFailed {
fmt.Fprintf(w, " *")
cl.color(warnColor).Fprintf(w, "failed")
fmt.Fprintf(w, "*")
}
fmt.Fprintf(w, " | ")
if cl.isCached {
fmt.Fprintf(w, "*")
cl.color(cachedColor).Fprintf(w, "cached")
fmt.Fprintf(w, "* ")
}
}
func (cl ConsoleLogger) color(c *color.Color) *color.Color {
switch cl.colorMode {
case NoColor:
return noColor
case ForceColor:
return c
case AutoColor:
if color.NoColor {
return noColor
}
return c
}
return noColor
}
func prettyPrefix(prefixPadding int, prefix string) string {
return formatter.Format(prefix, prefixPadding)
}
// WithLogLevel changes the log level
func (cl ConsoleLogger) WithLogLevel(logLevel LogLevel) ConsoleLogger {
ret := cl.clone()
ret.logLevel = logLevel
return ret
}
// WriteBundleToDisk makes an attached bundle writer (if any) write the collected bundle to disk.
func (cl ConsoleLogger) WriteBundleToDisk() (string, error) {
if cl.bb == nil {
return "", nil
}
return cl.bb.WriteToDisk()
}
// MarkBundleBuilderResult marks the current targets result in a log bundle for a given prefix with the current result.
func (cl ConsoleLogger) MarkBundleBuilderResult(isError, isCanceled bool) {
if cl.bb == nil {
return
}
var result string
if isCanceled {
result = ResultCancelled
} else {
if isError {
result = ResultFailure
} else {
result = ResultSuccess
}
}
cl.bb.PrefixResult(cl.Prefix(), result)
}
// MarkBundleBuilderStatus marks the current targets status in a log bundle for a given prefix with the current status.
func (cl ConsoleLogger) MarkBundleBuilderStatus(isStarted, isFinished, isCanceled bool) {
if cl.bb == nil {
return
}
var status string
if isCanceled {
status = StatusCancelled
} else {
if isStarted {
if isFinished {
status = StatusComplete
} else {
status = StatusInProgress
}
} else {
status = StatusWaiting
}
}
cl.bb.PrefixStatus(cl.Prefix(), status)
}