-
Notifications
You must be signed in to change notification settings - Fork 21
/
printer.go
69 lines (60 loc) · 1.8 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
package main
import (
"fmt"
"io"
"os"
"sync"
)
// A Printer represents an active printer object that generates lines of
// output to an io.Writer. Each output operation makes a single call to
// the Writer's Write method. A Printer can be used simultaneously from
// multiple goroutines; it guarantees to serialize access to the Writer.
type Printer struct {
mu sync.Mutex // ensures atomic writes; protects the following fields
out io.Writer // destination for output
buf []byte // for accumulating text to write
off bool // should we be quiet?
}
// New creates a new Printer. The out variable sets the
// destination to which log data will be written.
func NewPrinter(out io.Writer) *Printer {
return &Printer{out: out}
}
// Default printer.
var printer = NewPrinter(os.Stdout)
// SetOutput sets the output destination for the printer.
func (p *Printer) SetOutput(w io.Writer) {
p.mu.Lock()
defer p.mu.Unlock()
p.out = w
}
// Output writes to the output destination. A newline is appended
// if the last character of s is not already a newline.
func (p *Printer) Output(s string) error {
if p.off {
return nil
}
p.mu.Lock()
defer p.mu.Unlock()
p.buf = p.buf[:0]
p.buf = append(p.buf, s...)
if len(s) == 0 || s[len(s)-1] != '\n' {
p.buf = append(p.buf, '\n')
}
_, err := p.out.Write(p.buf)
return err
}
// Printf calls p.Output to print to the printer.
// Arguments are handled in the manner of fmt.Printf.
func (p *Printer) Printf(format string, v ...interface{}) {
p.Output(fmt.Sprintf(format, v...))
}
// Println calls p.Output to print to the printer.
// Arguments are handled in the manner of fmt.Println.
func (p *Printer) Println(v ...interface{}) { p.Output(fmt.Sprintln(v...)) }
// Set quiet mode.
func (p *Printer) SetOff(off bool) {
p.mu.Lock()
defer p.mu.Unlock()
p.off = off
}