-
Notifications
You must be signed in to change notification settings - Fork 0
/
delegating.go
42 lines (35 loc) · 926 Bytes
/
delegating.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
package zapappender
import (
"go.uber.org/zap/zapcore"
)
var _ SynchronizationAwareAppender = &Delegating{}
// Delegating delegates Write and Sync to functions
type Delegating struct {
WriteFn func(p []byte, ent zapcore.Entry) (n int, err error)
SyncFn func() error
SynchronizedValue bool
}
func NewDelegating(writeFn func(p []byte, ent zapcore.Entry) (n int, err error), syncFn func() error, synchronized bool) *Delegating {
return &Delegating{
WriteFn: writeFn,
SyncFn: syncFn,
SynchronizedValue: synchronized,
}
}
func (a *Delegating) Write(p []byte, ent zapcore.Entry) (int, error) {
writeFn := a.WriteFn
if writeFn == nil {
return len(p), nil
}
return writeFn(p, ent)
}
func (a *Delegating) Sync() error {
syncFn := a.SyncFn
if syncFn == nil {
return nil
}
return syncFn()
}
func (a *Delegating) Synchronized() bool {
return a.SynchronizedValue
}