-
Notifications
You must be signed in to change notification settings - Fork 2
/
custom.go
312 lines (262 loc) · 8.49 KB
/
custom.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
package custom
import (
"bytes"
"encoding/json"
"fmt"
"io"
"strconv"
"strings"
"time"
"github.com/fatih/color"
"github.com/influx6/faux/metrics"
)
var (
red = color.New(color.FgRed)
green = color.New(color.FgGreen)
white = color.New(color.FgWhite)
yellow = color.New(color.FgHiYellow)
magenta = color.New(color.FgHiMagenta)
)
// FlatDisplay writes giving Entries as seperated blocks of contents where the each content is
// converted within a block like below:
//
// Message: We must create new standard behaviour Function: BuildPack | display: red, words: 20,
//
// Message: We must create new standard behaviour Function: BuildPack | display: red, words: 20,
//
func FlatDisplay(w io.Writer) metrics.Processors {
return FlatDisplayWith(w, "Message:", nil)
}
// FlatDisplayWith writes giving Entries as seperated blocks of contents where the each content is
// converted within a block like below:
//
// [Header]: We must create new standard behaviour Function: BuildPack | display: red, words: 20,
//
// [Header]: We must create new standard behaviour Function: BuildPack | display: red, words: 20,
//
func FlatDisplayWith(w io.Writer, header string, filterFn func(metrics.Entry) bool) metrics.Processors {
return NewEmitter(w, func(en metrics.Entry) []byte {
if filterFn != nil && !filterFn(en) {
return nil
}
var bu bytes.Buffer
bu.WriteString("\n")
if header != "" {
fmt.Fprintf(&bu, "%s %+s", green.Sprint(header), printAtLevel(en.Level, en.Message))
} else {
fmt.Fprintf(&bu, "%+s", printAtLevel(en.Level, en.Message))
}
if en.ID != "" {
fmt.Fprintf(&bu, "ID: %+s\n", printAtLevel(en.Level, en.ID))
}
fmt.Fprint(&bu, printSpaceLine(2))
if en.Function != "" {
fmt.Fprintf(&bu, "%s: %+s\n", green.Sprint("Function"), en.Function)
fmt.Fprint(&bu, printSpaceLine(2))
fmt.Fprintf(&bu, "%s: %+s:%d", green.Sprint("File"), en.File, en.Line)
fmt.Fprint(&bu, printSpaceLine(2))
}
fmt.Fprint(&bu, printSpaceLine(2))
for key, value := range en.Field {
fmt.Fprintf(&bu, "%+s: %+s", green.Sprint(key), printItem(value))
fmt.Fprint(&bu, printSpaceLine(2))
}
bu.WriteString("\n")
return bu.Bytes()
})
}
//=====================================================================================
// BlockDisplay writes giving Entries as seperated blocks of contents where the each content is
// converted within a block like below:
//
// Message: We must create new standard behaviour
// Function: BuildPack
// +-----------------------------+------------------------------+
// | displayrange.address.bolder | "No 20 tokura flag" |
// +-----------------------------+------------------------------+
// +--------------------------+----------+
// | displayrange.bolder.size | 20 |
// +--------------------------+----------+
//
func BlockDisplay(w io.Writer) metrics.Processors {
return BlockDisplayWith(w, "Message:", nil)
}
// BlockDisplayWith writes giving Entries as seperated blocks of contents where the each content is
// converted within a block like below:
//
// Message: We must create new standard behaviour
// Function: BuildPack
// +-----------------------------+------------------------------+
// | displayrange.address.bolder | "No 20 tokura flag" |
// +-----------------------------+------------------------------+
// +--------------------------+----------+
// | displayrange.bolder.size | 20 |
// +--------------------------+----------+
//
func BlockDisplayWith(w io.Writer, header string, filterFn func(metrics.Entry) bool) metrics.Processors {
return NewEmitter(w, func(en metrics.Entry) []byte {
if filterFn != nil && !filterFn(en) {
return nil
}
var bu bytes.Buffer
if header != "" {
fmt.Fprintf(&bu, "%s %+s\n", green.Sprint(header), printAtLevel(en.Level, en.Message))
} else {
fmt.Fprintf(&bu, "%+s\n", printAtLevel(en.Level, en.Message))
}
if en.ID != "" {
fmt.Fprintf(&bu, "ID: %+s\n", printAtLevel(en.Level, en.ID))
}
if en.Function != "" {
fmt.Fprintf(&bu, "%s: %+s\n", green.Sprint("Function"), en.Function)
fmt.Fprintf(&bu, "%s: %+s:%d\n", green.Sprint("File"), en.File, en.Line)
}
for key, val := range en.Field {
value := printItem(val)
keyLength := len(key) + 2
valLength := len(value) + 2
keyLines := printBlockLine(keyLength)
valLines := printBlockLine(valLength)
spaceLines := printSpaceLine(1)
fmt.Fprintf(&bu, "+%s+%s+\n", keyLines, valLines)
fmt.Fprintf(&bu, "|%s%s%s|%s%s%s|\n", spaceLines, green.Sprint(key), spaceLines, spaceLines, value, spaceLines)
fmt.Fprintf(&bu, "+%s+%s+", keyLines, valLines)
fmt.Fprintf(&bu, "\n")
}
bu.WriteString("\n")
return bu.Bytes()
})
}
//=====================================================================================
// StackDisplay writes giving Entries as seperated blocks of contents where the each content is
// converted within a block like below:
//
// Message: We must create new standard behaviour
// Function: BuildPack
// - displayrange.address.bolder: "No 20 tokura flag"
// - displayrange.bolder.size: 20
//
func StackDisplay(w io.Writer) metrics.Processors {
return StackDisplayWith(w, "Message:", "-", nil)
}
// StackDisplayWith writes giving Entries as seperated blocks of contents where the each content is
// converted within a block like below:
//
// [Header]: We must create new standard behaviour
// Function: BuildPack
// [tag] displayrange.address.bolder: "No 20 tokura flag"
// [tag] displayrange.bolder.size: 20
//
func StackDisplayWith(w io.Writer, header string, tag string, filterFn func(metrics.Entry) bool) metrics.Processors {
return NewEmitter(w, func(en metrics.Entry) []byte {
if filterFn != nil && !filterFn(en) {
return nil
}
var bu bytes.Buffer
if header != "" {
fmt.Fprintf(&bu, "%s %+s\n", green.Sprint(header), printAtLevel(en.Level, en.Message))
} else {
fmt.Fprintf(&bu, "%+s\n", printAtLevel(en.Level, en.Message))
}
if en.ID != "" {
fmt.Fprintf(&bu, "ID: %+s\n", printAtLevel(en.Level, en.ID))
}
if tag == "" {
tag = "-"
}
if en.Function != "" {
fmt.Fprintf(&bu, "%s: %+s\n", green.Sprint("Function"), en.Function)
fmt.Fprintf(&bu, "%s: %+s:%d\n", green.Sprint("File"), en.File, en.Line)
}
for key, value := range en.Field {
fmt.Fprintf(&bu, "%s %s: %+s\n", tag, green.Sprintf(key), printItem(value))
}
bu.WriteString("\n")
return bu.Bytes()
})
}
//=====================================================================================
// Emitter emits all entries into the entries into a sink io.writer after
// transformation from giving transformer function..
type Emitter struct {
Sink io.Writer
Transform func(metrics.Entry) []byte
}
// NewEmitter returns a new instance of Emitter.
func NewEmitter(w io.Writer, transform func(metrics.Entry) []byte) *Emitter {
return &Emitter{
Sink: w,
Transform: transform,
}
}
// Handle implements the metrics.metrics interface.
func (ce *Emitter) Handle(e metrics.Entry) error {
_, err := ce.Sink.Write(ce.Transform(e))
return err
}
//=====================================================================================
func printAtLevel(lvl metrics.Level, message string) string {
switch lvl {
case metrics.ErrorLvl:
return red.Sprint(message)
case metrics.InfoLvl:
return white.Sprint(message)
case metrics.RedAlertLvl:
return magenta.Sprint(message)
case metrics.YellowAlertLvl:
return yellow.Sprint(message)
}
return message
}
func printSpaceLine(length int) string {
var lines []string
for i := 0; i < length; i++ {
lines = append(lines, " ")
}
return strings.Join(lines, "")
}
func printBlockLine(length int) string {
var lines []string
for i := 0; i < length; i++ {
lines = append(lines, "-")
}
return strings.Join(lines, "")
}
type stringer interface {
String() string
}
func printItem(item interface{}) string {
switch bo := item.(type) {
case stringer:
return bo.String()
case string:
return `"` + bo + `"`
case error:
return bo.Error()
case int:
return strconv.Itoa(bo)
case int8:
return strconv.Itoa(int(bo))
case int16:
return strconv.Itoa(int(bo))
case int64:
return strconv.Itoa(int(bo))
case time.Time:
return bo.UTC().String()
case rune:
return strconv.QuoteRune(bo)
case bool:
return strconv.FormatBool(bo)
case byte:
return strconv.QuoteRune(rune(bo))
case float64:
return strconv.FormatFloat(bo, 'f', 4, 64)
case float32:
return strconv.FormatFloat(float64(bo), 'f', 4, 64)
}
data, err := json.Marshal(item)
if err != nil {
return fmt.Sprintf("%#v", item)
}
return string(data)
}