-
Notifications
You must be signed in to change notification settings - Fork 388
/
writersub.go
50 lines (43 loc) · 1.08 KB
/
writersub.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
package writersub
import (
"io"
"sync"
"github.com/earthly/cloud-api/logstream"
)
// WriterSub is a bus subscriber that can print formatted logs to a writer.
type WriterSub struct {
w io.Writer
targetIDFilter string
mu sync.Mutex
errors []error
}
// New creates a new WriterSub.
func New(w io.Writer, targetIDFilter string) *WriterSub {
return &WriterSub{
w: w,
targetIDFilter: targetIDFilter,
}
}
// Write writes the given delta to the writer, if it is a formatted log delta.
func (ws *WriterSub) Write(delta *logstream.Delta) {
ws.mu.Lock()
defer ws.mu.Unlock()
switch d := delta.DeltaTypeOneof.(type) {
case *logstream.Delta_DeltaFormattedLog:
if ws.targetIDFilter != "" && d.DeltaFormattedLog.TargetId != ws.targetIDFilter {
return
}
_, err := ws.w.Write(d.DeltaFormattedLog.Data)
if err != nil {
ws.errors = append(ws.errors, err)
return
}
default:
}
}
// Errors returns any errors that occurred while writing to the writer.
func (ws *WriterSub) Errors() []error {
ws.mu.Lock()
defer ws.mu.Unlock()
return ws.errors
}