This repository was archived by the owner on Sep 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathlogger.go
More file actions
179 lines (150 loc) · 4.74 KB
/
logger.go
File metadata and controls
179 lines (150 loc) · 4.74 KB
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
package logger
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"github.com/docker/docker/pkg/term"
"github.com/fatih/color"
)
// Logger implements a per-plan logger.
type Logger struct {
output io.Writer
// if recording, will fill this buffer with logger output instead of printing to stdio.
// if not yet recording, this will be nil.
buffer *bytes.Buffer
plan string
}
// New constructs a new per-plan logger.
func New(plan string) *Logger {
return &Logger{plan: plan, output: os.Stdout}
}
// Record starts recording to the output buffer, which will be returned by the
// Output() method.
func (l *Logger) Record() {
l.buffer = new(bytes.Buffer)
l.output, color.Output = l.buffer, l.buffer
}
// Output returns the output buffer.
func (l *Logger) Output() io.Writer {
return l.output
}
// Print is a bare-bones print statement.
func (l *Logger) Print(str string) {
fmt.Fprint(l.output, l.Plan(), str)
}
// Plan gets the plan name specified at construction time
func (l *Logger) Plan() string {
str := color.New(color.Bold, color.FgBlue).SprintFunc()("[")
str += color.New(color.FgBlue).SprintFunc()(l.plan)
str += color.New(color.Bold, color.FgBlue).SprintFunc()("] ")
return str
}
// Good reports a nice status in green to indicate successes.
func (l *Logger) Good(str string) string {
return color.New(color.FgGreen).SprintFunc()(fmt.Sprintf("+++ %s", str))
}
// Notice is an arbitrary message explaining what the heck is going on.
func (l *Logger) Notice(str string) string {
return color.New(color.FgYellow).SprintFunc()(fmt.Sprintf("--- %s", str))
}
// Error prints an error to the terminal all fancy-like.
func (l *Logger) Error(err interface{}) {
line := l.Plan()
line += color.New(color.Bold, color.FgRed).SprintFunc()("!!! ")
line += color.New(color.FgWhite).SprintFunc()(fmt.Sprintf("Error: %v", err))
fmt.Fprintln(l.output, line)
color.Unset()
}
// BuildStep logs a build step.
func (l *Logger) BuildStep(step, command string) {
line := l.Plan()
line += l.Good("")
line += color.New(color.Bold, color.FgWhite).SprintFunc()("Execute: ")
line += color.New(color.FgGreen).SprintFunc()(fmt.Sprintf("%s %s", step, command))
fmt.Fprintln(l.output, line)
color.Unset()
}
// CacheHit logs a cache hit.
func (l *Logger) CacheHit(imageID string) {
line := l.Plan()
line += l.Good("")
line += color.New(color.FgWhite, color.Bold, color.BgRed).SprintFunc()("Cache hit:")
line += color.New(color.FgCyan).SprintFunc()(fmt.Sprintf(" using %q", strings.SplitN(imageID, ":", 2)[1][:12]))
fmt.Fprintln(l.output, line)
color.Unset()
}
// CopyPath logs a copied path
func (l *Logger) CopyPath(file1, file2 string) {
line := l.Plan()
line += l.Notice("")
line += color.New(color.FgRed).SprintFunc()("COPY: ")
line += fmt.Sprintf("%q -> %q\n", file1, file2)
fmt.Fprintln(l.output, line)
color.Unset()
}
// Tag logs a tag
func (l *Logger) Tag(name string) {
line := l.Plan()
line += l.Good("")
line += color.New(color.FgYellow).SprintFunc()("Tagged:")
fmt.Fprintln(l.output, line, name)
}
// EvalResponse logs the eval response
func (l *Logger) EvalResponse(response string) {
line := l.Plan()
line += l.Good("")
line += color.New(color.FgWhite, color.Bold).SprintFunc()("Eval Response:")
fmt.Fprintln(l.output, line, response)
color.Unset()
}
// Finish logs the finish.
func (l *Logger) Finish(response string) {
line := l.Plan()
line += l.Good("")
line += color.New(color.FgRed, color.Bold).SprintFunc()("Finish: ")
fmt.Fprintln(l.output, line, response)
}
// BeginOutput demarcates an output section
func (l *Logger) BeginOutput() {
line := l.Plan()
line += color.New(color.FgRed, color.Bold, color.BgWhite).SprintFunc()("------ BEGIN OUTPUT ------")
fmt.Fprintln(l.output, line)
}
// EndOutput ends an output section
func (l *Logger) EndOutput() {
line := l.Plan()
line += color.New(color.FgRed, color.Bold, color.BgWhite).SprintFunc()("------- END OUTPUT -------")
fmt.Fprintln(l.output, line)
}
// Progress is a representation of a progress meter.
func (l *Logger) Progress(prefix string, count float64) {
out := fmt.Sprint("\r")
wsz, _ := term.GetWinsize(0)
mbs := fmt.Sprintf("%.02fMB", count)
justifiedWidth := int(wsz.Width) - len(mbs) - 5 // ... below
if justifiedWidth < 0 {
return
}
var skip bool
var charCount int
buf := ""
for _, b := range fmt.Sprintf("%s%s %s", l.Plan(), color.New(color.FgWhite, color.Bold).SprintFunc()("+++"), color.New(color.FgRed, color.Bold).SprintfFunc()("%s", prefix)) {
if b == '\033' {
skip = true
}
if !skip {
charCount++
} else if (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') {
skip = false
}
if charCount > justifiedWidth && !skip {
break
}
buf = string(append([]rune(buf), b))
}
out += buf + "...: "
out += color.New(color.FgWhite).SprintFunc()(mbs)
fmt.Fprint(l.output, out)
}