-
Notifications
You must be signed in to change notification settings - Fork 2
/
output.go
123 lines (98 loc) · 2.47 KB
/
output.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
package main
import (
"bytes"
"fmt"
"io"
"os"
"time"
)
type Output interface {
io.Writer // for data
Error(fmt string, args ...any)
Info(fmt string, args ...any)
Debug(fmt string, args ...any)
Exit(status int)
}
type DefaultOutput struct {
EnableDebug bool
DisableInfo bool
DataOutput io.Writer
DoTimestamp bool
}
var _ Output = DefaultOutput{}
func datestr() string {
return time.Now().Format(time.RFC3339)
}
func (d DefaultOutput) Error(ff string, args ...any) {
fmt.Fprintf(os.Stderr, d.Timestamp("E ")+ff, args...)
}
func (d DefaultOutput) Info(ff string, args ...any) {
if !d.DisableInfo {
fmt.Fprintf(os.Stderr, d.Timestamp("I ")+ff, args...)
}
}
func (d DefaultOutput) Debug(ff string, args ...any) {
if d.EnableDebug {
fmt.Fprintf(os.Stderr, d.Timestamp("D ")+ff, args...)
}
}
func (t DefaultOutput) Write(data []byte) (int, error) {
return t.DataOutput.Write(data)
}
func (d DefaultOutput) Timestamp(kind string) string {
if !d.DoTimestamp {
return ""
}
return kind + datestr() + " "
}
func (t DefaultOutput) Exit(i int) {
OsExit(i)
}
type TaggedOutput struct {
Chain Output
Prefix string
}
var _ Output = TaggedOutput{}
func (t TaggedOutput) Error(ff string, args ...any) {
t.Chain.Error("%s: "+ff, append([]any{t.Prefix}, args...)...)
}
func (t TaggedOutput) Info(ff string, args ...any) {
t.Chain.Info("%s: "+ff, append([]any{t.Prefix}, args...)...)
}
func (t TaggedOutput) Debug(ff string, args ...any) {
t.Chain.Debug("%s: "+ff, append([]any{t.Prefix}, args...)...)
}
func (t TaggedOutput) Write(data []byte) (int, error) {
return t.Chain.Write(data)
}
func (t TaggedOutput) Exit(i int) {
t.Chain.Exit(i)
}
type CaptureOutput struct {
DebugBuf bytes.Buffer
InfoBuf bytes.Buffer
ErrorBuf bytes.Buffer
OutputBuf bytes.Buffer
}
func NewCaptureOutput() *CaptureOutput {
return &CaptureOutput{}
}
func (c *CaptureOutput) Debug(ff string, args ...any) {
fmt.Fprintf(&c.DebugBuf, ff, args...)
}
func (c *CaptureOutput) Info(ff string, args ...any) {
fmt.Fprintf(&c.InfoBuf, ff, args...)
}
func (c *CaptureOutput) Error(ff string, args ...any) {
fmt.Fprintf(&c.ErrorBuf, ff, args...)
}
func (c *CaptureOutput) Write(data []byte) (int, error) {
return c.OutputBuf.Write(data)
}
func (c *CaptureOutput) Exit(status int) {
// really, this is the least bad option for testing ...
// Typically, this will turn into two panics; one for the
// first exit, and one for the exit called in the panic
// handler of RunRecoverWithTag().
panic(status)
}