-
Notifications
You must be signed in to change notification settings - Fork 0
/
logOutput.go
123 lines (102 loc) · 3.09 KB
/
logOutput.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
// Copyright 2015 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package log
import (
"context"
"encoding/hex"
"strconv"
"sync"
"github.com/golang/protobuf/proto"
log "go.chromium.org/luci/common/logging"
"go.chromium.org/luci/common/proto/google"
"go.chromium.org/luci/logdog/api/logpb"
"go.chromium.org/luci/logdog/client/butler/output"
"go.chromium.org/luci/logdog/common/types"
)
// logOutput is an Output implementation that logs messages to its contexts'
// Logger instance.
type logOutput struct {
sync.Mutex
ctx context.Context
// bundleSize is the maximum size of the Butler bundle to use.
bundleSize int
stats output.StatsBase
et output.EntryTracker
}
// logOutput implements output.Output.
var _ output.Output = (*logOutput)(nil)
// New instantes a new log output instance.
func New(ctx context.Context, bundleSize int) output.Output {
o := logOutput{
ctx: ctx,
bundleSize: bundleSize,
}
o.ctx = log.SetFields(o.ctx, log.Fields{
"output": &o,
})
return &o
}
func (o *logOutput) String() string { return "log" }
func (o *logOutput) SendBundle(bundle *logpb.ButlerLogBundle) error {
o.et.Track(bundle)
o.Lock()
defer o.Unlock()
for _, e := range bundle.Entries {
path := types.StreamName(bundle.Prefix).Join(types.StreamName(e.Desc.Name))
ctx := log.SetField(o.ctx, "streamPath", path)
log.Fields{
"count": len(e.Logs),
"descriptor": e.Desc.String(),
}.Infof(ctx, "Received stream logs.")
for _, le := range e.Logs {
log.Fields{
"timeOffset": google.DurationFromProto(le.TimeOffset),
"prefixIndex": le.PrefixIndex,
"streamIndex": le.StreamIndex,
"sequence": le.Sequence,
}.Infof(ctx, "Received message.")
if c := le.GetText(); c != nil {
for idx, l := range c.Lines {
log.Infof(ctx, "Line %d) %s (%s)", idx, l.Value, strconv.Quote(l.Delimiter))
}
}
if c := le.GetBinary(); c != nil {
log.Infof(ctx, "Binary) %s", hex.EncodeToString(c.Data))
}
if c := le.GetDatagram(); c != nil {
if cp := c.Partial; cp != nil {
log.Infof(ctx, "Datagram (%#v) (%d bytes): %s", cp, cp.Size, hex.EncodeToString(c.Data))
} else {
log.Infof(ctx, "Datagram (%d bytes): %s", len(c.Data), hex.EncodeToString(c.Data))
}
}
o.stats.F.SentMessages++
}
}
o.stats.F.SentBytes += int64(proto.Size(bundle))
return nil
}
func (o *logOutput) MaxSize() int {
return o.bundleSize
}
func (o *logOutput) Stats() output.Stats {
o.Lock()
defer o.Unlock()
st := o.stats
return &st
}
func (o *logOutput) Record() *output.EntryRecord {
return o.et.Record()
}
func (o *logOutput) Close() {}