-
Notifications
You must be signed in to change notification settings - Fork 0
/
printer.go
577 lines (498 loc) · 13.6 KB
/
printer.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
package pio
import (
"bufio"
"bytes"
"io"
"io/ioutil"
"strconv"
"sync"
"sync/atomic"
"github.com/hidevopsio/pio/terminal"
)
type (
// Handler is the signature implemented by callers
// that want to be notified about the results
// that are being printed to the Printer's output.
//
// Look `Printer#Handle` for more.
Handler func(PrintResult)
)
// Printer is responsible to print the end result.
type Printer struct {
Name string
IsTerminal bool
priority int // higher means try to print first from this printer, from `Registry#Print`
// if Chained is true then the parent `Registry#Print`
// will continue to search for a compatible printer
// even if this printer succeed to print the contents.
Chained bool
Output io.Writer
mu sync.Mutex
marshal MarshalerFunc
hijack Hijacker
handlers []Handler
// these three will complete the interface of the:
// https://golang.org/pkg/io/#ReadWriteCloser
// in order to make possible to use everything inside the `io` package.
// i.e
// https://golang.org/pkg/io/#example_MultiWriter
// https://golang.org/pkg/io/#example_TeeReader (piping)
io.Reader
io.Writer
io.Closer
// DirectOutput will output the contents and flush them as fast as possible,
// without storing them to the buffer to complete the `ReadWriteCloser` std interface.
// Enable this if you need performance and you don't use the standard functions like `TeeReader`.
DirectOutput bool
}
var (
// TotalPrinters holds the number of
// the total printers created, either by
// `NewPrinter`, `NewTextPrinter`, `Register` or `RegisterPrinter`
TotalPrinters int32
)
// NewPrinter returns a new named printer
// if "output" is nil then it doesn't prints anywhere.
//
// If "name" is empty then it will be filled with
// "printer_$printers.len".
//
// If the marshaler is nil, meaning that this writer's
// result will never being proceed, caller should
// add a marshaler using the `Marshal` function.
//
// Look `OutputFrom` too.
func NewPrinter(name string, output io.Writer) *Printer {
if output == nil {
output = NopOutput()
}
atomic.AddInt32(&TotalPrinters, 1)
if name == "" {
totalPrinters := atomic.LoadInt32(&TotalPrinters)
lens := strconv.Itoa(int(totalPrinters))
name = "printer_" + lens
}
buf := &bytes.Buffer{}
isOuputTerminal := isTerminal(output)
p := &Printer{
Name: name,
Output: output,
Writer: buf,
Reader: buf,
Closer: NopCloser(),
IsTerminal: isOuputTerminal,
}
// If "output" is terminal then a text marshaler will be
// added to the Printer's marshalers.
//
// if p.IsTerminal {
// p.Marshal(Text)
// }
//
// let's think of it
// if a user don't want it we can't force this printer
// to print texts too, the end-developer
// may have split his logic about logging
// so don't do it automatically, instead
// create a new function which will return a text printer
// and allow this printer to accept more than one marshalers.
return p
}
// NewTextPrinter same as NewPrinter but registers
// a text marshaler, no matter what kind of "output",
// which converts string type
// to a compatible form of slice of bytes.
//
// If "name" is empty then it will be filled with
// "printer_$printers.len".
//
// Look `OutputFrom` too.
func NewTextPrinter(name string, output io.Writer) *Printer {
p := NewPrinter(name, output)
p.Marshal(Text)
return p
}
// Priority changes the order of this printer.
// Higher value means that the `Registry#Print`
// will try to print first from this printer.
// Default order is 0 for all printers.
//
// Returns it self.
func (p *Printer) Priority(prio int) *Printer {
p.mu.Lock()
p.priority = prio
p.mu.Unlock()
return p
}
// EnableNewLine adds a new line when needed, defaults to false
// you should turn it to on if you use a custom marshaler in a printer
// which prints to a terminal.
// var EnableNewLine = false
// func (p *Printer) addNewLineInneed(b []byte) []byte {
// if !EnableNewLine {
// return b
// }
// if l := len(b); l > 2 {
// // if is terminal add new line and hasn't \n already
// if p.IsTerminal && !bytes.Equal(b[l-1:], newLine) {
// b = append(b, newLine...)
// }
// }
// return b
// }
// Marshal adds a "marshaler" to the printer.
// Returns itself.
func (p *Printer) Marshal(marshaler Marshaler) *Printer {
return p.MarshalFunc(marshaler.Marshal)
}
// MarshalFunc adds a "marshaler" to the printer.
// Returns itself.
func (p *Printer) MarshalFunc(marshaler func(v interface{}) ([]byte, error)) *Printer {
p.mu.Lock()
defer p.mu.Unlock()
if p.marshal == nil {
p.marshal = marshaler
return p
}
oldM := p.marshal
newM := marshaler
// false on first failure
p.marshal = func(v interface{}) ([]byte, error) {
b, err := oldM(v)
// check if we can continue to the next marshal func
if err != nil && err.Error() == ErrMarshalNotResponsible.Error() {
b, err = newM(v)
}
// if no data return but err is nil, then something went wrong
if len(b) <= 0 && err == nil {
return b, ErrSkipped
}
return b, err // p.addNewLineInneed(b), err
}
return p
}
// WithMarshalers same as `Marshal` but accepts more than one marshalers
// and returns the Printer itself in order to be used side by side with the creational
// function.
func (p *Printer) WithMarshalers(marshalers ...Marshaler) *Printer {
if len(marshalers) == 0 {
return p
}
for _, marshaler := range marshalers {
p.Marshal(marshaler)
}
return p
}
// AddOutput adds one or more io.Writer to the Printer.
// Returns itself.
//
// Look `OutputFrom` and `Wrap` too.
func (p *Printer) AddOutput(writers ...io.Writer) *Printer {
p.mu.Lock()
defer p.mu.Unlock()
for _, w := range writers {
// set is terminal to false
// if at least one of the writers
// is not a terminal-based.
if !terminal.IsTerminal(w) {
p.IsTerminal = false
break
}
}
w := io.MultiWriter(append(writers, p.Output)...)
p.Output = w
return p
// p.mu.Lock()
// oldW := p.Output
// newW := io.MultiWriter(writers...)
// p.Output = writerFunc(func(p []byte) (n int, err error) {
// n, err = oldW.Write(p)
// if err != nil {
// return
// }
// if n != len(p) {
// err = io.ErrShortWrite
// return
// }
// return newW.Write(p)
// })
// p.mu.Unlock()
}
// SetOutput sets accepts one or more io.Writer
// and set a multi-writter instance to the Printer's Output.
// Returns itself.
//
// Look `OutputFrom` too.
func (p *Printer) SetOutput(writers ...io.Writer) *Printer {
var w io.Writer
if l := len(writers); l == 0 {
return p
} else if l == 1 {
w = writers[0]
} else {
w = io.MultiWriter(writers...)
}
p.mu.Lock()
p.Output = w
p.IsTerminal = terminal.IsTerminal(w)
p.mu.Unlock()
return p
}
// EnableDirectOutput will output the contents and flush them as fast as possible,
// without storing them to the buffer to complete the `ReadWriteCloser` std interface.
// Enable this if you need performance and you don't use the standard functions like `TeeReader`.
// Returns itself.
func (p *Printer) EnableDirectOutput() *Printer {
p.mu.Lock()
p.DirectOutput = true
p.mu.Unlock()
return p
}
// Print of a Printer accepts a value of "v",
// tries to marshal its contents and flushes the result
// to the Printer's output.
//
// If "v" implements the `Marshaler` type, then this marshaler
// is called automatically, first.
//
// Print -> Store[Marshal -> err != nil && result -> Hijack(result) -> Write(result)] -> Flush[Printer.Write(buf) and Handle(buf)]
//
// Returns how much written and an error on failure.
func (p *Printer) Print(v interface{}) (int, error) {
return p.print(v, false)
}
// Println accepts a value of "v",
// tries to marshal its contents and flushes the result
// to this "p" Printer, it adds a new line at the ending,
// the result doesn't contain this new line, therefore result's contents kept as expected.
func (p *Printer) Println(v interface{}) (int, error) {
return p.print(v, true)
}
func (p *Printer) print(v interface{}, appendNewLine bool) (int, error) {
var (
b []byte
err error
)
if p.DirectOutput {
b, err = p.WriteTo(v, p.Output, appendNewLine)
} else {
err = p.Store(v, appendNewLine) // write to the buffer
if err != nil {
return -1, err
}
b, err = p.Flush()
}
// flush error return last,
// we should call handlers even if the result is a failure.
if len(p.handlers) > 0 {
// create the print result instance
// only when printer uses handlers, so we can reduce the factory calls.
res := withValue(v).withErr(err).withContents(b)
for _, h := range p.handlers {
// do NOT run each handler on its own goroutine because we need sync with the messages.
// let end-developer decide the pattern.
h(res)
}
}
return len(b), err
}
func (p *Printer) readAndConsume() ([]byte, error) {
b, err := ioutil.ReadAll(p.Reader)
if err != nil && err != io.EOF {
return b, err
}
return b, nil
}
// Flush will consume and flush the Printer's current contents.
func (p *Printer) Flush() ([]byte, error) {
p.mu.Lock()
defer p.mu.Unlock()
b, err := p.readAndConsume()
if err != nil {
return nil, err
}
_, err = p.Output.Write(b)
return b, err
}
// Store will store-only the contents of "v".
// Returns a PrintResult type in order to the final contents
// be accessible by third-party tools.
//
// If you want to Print and Flush to the Printer's Output use `Print` instead.
//
// If "appendNewLine" is true then it writes a new line to the
// Printer's output. Note that it doesn't concat it to the
// returning PrintResult, therefore the "appendNewLine" it is not affect the rest
// of the implementation like custom hijackers and handlers.
func (p *Printer) Store(v interface{}, appendNewLine bool) error {
_, err := p.WriteTo(v, p.Writer, appendNewLine)
return err
}
// WriteTo marshals and writes the "v" to the "w" writer.
//
// Returns this WriteTo's result information such as error, written.
func (p *Printer) WriteTo(v interface{}, w io.Writer, appendNewLine bool) ([]byte, error) {
var marshaler Marshaler
// check if implements the Marshaled
if m, ok := v.(Marshaled); ok {
marshaler = fromMarshaled(m)
// check if implements the Marshaler
} else if m, ok := v.(Marshaler); ok {
marshaler = m
// otherwise make check if printer has a marshaler
// if not skip this WriteTo operation,
// else set the marshaler to that (most common).
} else {
if p.marshal != nil {
marshaler = p.marshal
}
}
var (
b []byte
err error
)
if hijack := p.hijack; hijack != nil {
ctx := acquireCtx(v, p)
defer releaseCtx(ctx)
hijack(ctx)
if ctx.canceled {
return nil, ErrCanceled
}
b, err = ctx.marshalResult.b, ctx.marshalResult.err
if err != nil {
return b, err
}
}
// needs marshal
if len(b) == 0 {
if marshaler == nil {
return nil, ErrSkipped
}
b, err = marshaler.Marshal(v)
if err != nil {
return b, err
}
}
_, err = w.Write(b)
if appendNewLine && err == nil {
w.Write(NewLine) // we don't care about this error.
}
return b, err
}
// Hijack registers a callback which is executed
// when ever `Print` or `WriteTo` is called,
// this callback can intercept the final result
// which will be written or be printed.
//
// Returns itself.
func (p *Printer) Hijack(cb func(ctx *Ctx)) *Printer {
p.mu.Lock()
defer p.mu.Unlock()
if p.hijack == nil {
p.hijack = cb
return p
}
oldCb := p.hijack
newCb := cb
// return the first failure
p.hijack = func(ctx *Ctx) {
oldCb(ctx)
if ctx.continueToNext {
newCb(ctx)
}
}
return p
}
// PrintResult contains some useful information for a `Print` or `WriteTo` action that
// are available inside handlers.
type PrintResult struct {
Written int
Error error
Contents []byte
Value interface{}
}
// IsOK returns true if result's content is available,
// otherwise false.
func (p PrintResult) IsOK() bool {
return p.Error == nil && len(p.Contents) > 0
}
// IsFailure returns true if result's content is not safe to read or it's available,
// otherwise false.
func (p PrintResult) IsFailure() bool {
return !p.IsOK()
}
var printResult = PrintResult{}
func withValue(v interface{}) PrintResult {
printResult.Value = v
return printResult
}
func (p PrintResult) withErr(err error) PrintResult {
if err != nil {
p.Written = -1
}
p.Error = err
return p
}
func (p PrintResult) withContents(b []byte) PrintResult {
if p.Error != nil {
p.Written = -1
} else {
p.Written = len(b)
p.Contents = b
}
return p
}
// Handle adds a callback which is called
// whenever a `Print` is successfully executed, it's being executed
// after the contents are written to its output.
//
// The callback accepts the final result,
// can be used as an easy, pluggable, access to all the logs passed to the `Print`.
// i.e: `Handle(func(result PrintResult){ fmt.Printf("%s\n", result.Contents)})`
//
// Returns itself.
func (p *Printer) Handle(h func(PrintResult)) *Printer {
p.mu.Lock()
p.handlers = append(p.handlers, h)
p.mu.Unlock()
return p
}
func (p *Printer) restore(b []byte) {
p.Writer.Write(b)
}
// Scan scans everything from "r" and prints
// its new contents to the "p" Printer,
// forever or until the returning "cancel" is fired, once.
func (p *Printer) Scan(r io.Reader, addNewLine bool) (cancel func()) {
var canceled uint32
shouldCancel := func() bool {
return atomic.LoadUint32(&canceled) > 0
}
cancel = func() {
atomic.StoreUint32(&canceled, 1)
}
go func() {
scanner := bufio.NewScanner(r)
for {
if shouldCancel() {
break
}
if scanner.Scan() {
if shouldCancel() {
// re-store the bytes?
p.restore(scanner.Bytes())
break
}
text := scanner.Bytes()
if addNewLine {
text = append(text, NewLine...)
}
p.Print(text)
}
if err := scanner.Err(); err != nil {
// TODO: do something with that or ignore it.
}
}
}()
return cancel
}