-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace.go
126 lines (97 loc) · 2.74 KB
/
trace.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
package flate
import (
"github.com/chronos-tachyon/assert"
"github.com/rs/zerolog"
)
// Tracer is an interface which callers can implement in order to receive
// Events. Events provide feedback on the progress of the compression or
// decompression operation.
type Tracer interface {
OnEvent(Event)
}
// Event is a collection of fields that provide feedback on the progress of the
// compression or decompression operation in progress. Events are provided to
// Tracers registered with a Reader or Writer.
type Event struct {
Type EventType
InputBytesTotal uint64
InputBytesStream uint64
OutputBytesTotal uint64
OutputBytesStream uint64
NumStreams uint
Format Format
Header *Header
Block *BlockEvent
Trees *TreesEvent
Footer *FooterEvent
}
// BlockEvent is a sub-struct that is only present for BlockFooEvent.
type BlockEvent struct {
Type BlockType
IsFinal bool
}
// TreesEvent is a sub-struct that is only present for BlockTreesEvent.
type TreesEvent struct {
CodeCount uint16
LiteralLengthCount uint16
DistanceCount uint16
CodeSizes SizeList
LiteralLengthSizes SizeList
DistanceSizes SizeList
}
// FooterEvent is a sub-struct that is only present for StreamEndEvent.
type FooterEvent struct {
Adler32 Checksum32
CRC32 Checksum32
}
// type NoOpTracer {{{
// NoOpTracer is an implementation of Tracer that does nothing.
type NoOpTracer struct{}
// OnEvent fulfills Tracer.
func (NoOpTracer) OnEvent(event Event) {}
var _ Tracer = NoOpTracer{}
// }}}
// type TracerFunc {{{
// TracerFunc is an implementation of Tracer that calls a function.
type TracerFunc func(Event)
// OnEvent fulfills Tracer.
func (tr TracerFunc) OnEvent(event Event) {
tr(event)
}
var _ Tracer = TracerFunc(nil)
// }}}
// type captureHeaderTracer {{{
// CaptureHeader returns a Tracer implementation which will fill the pointed-to
// Header object when StreamHeaderEvent is encountered.
func CaptureHeader(ptr *Header) Tracer {
assert.NotNil(&ptr)
return captureHeaderTracer{ptr: ptr}
}
type captureHeaderTracer struct {
ptr *Header
}
// OnEvent fulfills Tracer.
func (tr captureHeaderTracer) OnEvent(event Event) {
if event.Type == StreamHeaderEvent && event.Header != nil {
*tr.ptr = *event.Header
}
}
var _ Tracer = captureHeaderTracer{}
// }}}
// type logTracer {{{
// Log returns a Tracer implementation which will log each Event at Trace
// priority.
func Log(logger zerolog.Logger) Tracer {
return logTracer{logger: logger}
}
type logTracer struct {
logger zerolog.Logger
}
// OnEvent fulfills Tracer.
func (tr logTracer) OnEvent(event Event) {
tr.logger.Trace().
Interface("event", event).
Msg("OnEvent")
}
var _ Tracer = logTracer{}
// }}}