Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(556)

Side by Side Diff: client/internal/logdog/butler/output/null/null.go

Issue 1211053004: LogDog: Add Butler Output package. (Closed) Base URL: https://github.com/luci/luci-go@logdog-review-streamserver
Patch Set: Updated w/ newer protos and stuff. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package null
6
7 import (
8 "encoding/hex"
9 "strconv"
10 "sync"
11
12 "github.com/golang/protobuf/proto"
13 "github.com/luci/luci-go/client/internal/logdog/butler/output"
14 "github.com/luci/luci-go/common/logdog/protocol"
15 "github.com/luci/luci-go/common/logdog/types"
16 log "github.com/luci/luci-go/common/logging"
17 "golang.org/x/net/context"
18 )
19
20 // nullOutput is an Output implementation that logs messages to its contexts'
21 // Logger instance.
22 type nullOutput struct {
23 ctx context.Context
24
25 // bundleSize is the maximum size of the Butler bundle to use.
26 bundleSize int
27
28 stats output.StatsBase
29 statsMu sync.Mutex
30 }
31
32 // nullOutput implements output.Output.
33 var _ output.Output = (*nullOutput)(nil)
34
35 func (o *nullOutput) String() string {
36 return "Null"
37 }
38
39 // New instantes a new null output instance.
40 func New(ctx context.Context, bundleSize int) output.Output {
41 o := nullOutput{
42 ctx: ctx,
43 bundleSize: bundleSize,
44 }
45 o.ctx = log.SetFields(o.ctx, log.Fields{
46 "output": &o,
47 })
48 return &o
49 }
50
51 func (o *nullOutput) SendBundle(bundle *protocol.ButlerLogBundle) error {
52 st := output.StatsBase{}
53 defer o.mergeStats(&st)
54
55 for _, e := range bundle.Entries {
56 path := types.StreamName(e.Desc.Prefix).Join(types.StreamName(e. Desc.Name))
57 ctx := log.SetField(o.ctx, "streamPath", path)
58
59 log.Fields{
60 "count": len(e.Logs),
61 "descriptor": e.Desc.String(),
62 }.Infof(ctx, "Received stream logs.")
63
64 for _, le := range e.Logs {
65 log.Fields{
66 "timeOffset": le.TimeOffset.Duration(),
67 "prefixIndex": le.PrefixIndex,
68 "streamIndex": le.StreamIndex,
69 "sequence": le.Sequence,
70 }.Infof(ctx, "Received message.")
71 if c := le.GetText(); c != nil {
72 for idx, l := range c.Lines {
73 log.Infof(ctx, "Line %d) %s (%s)", idx, l.Value, strconv.Quote(l.Delimiter))
74 }
75 }
76 if c := le.GetBinary(); c != nil {
77 log.Infof(ctx, "Binary) %s", hex.EncodeToString( c.Data))
78 }
79 if c := le.GetDatagram(); c != nil {
80 log.Infof(ctx, "Datagram %d: %s", c.Size, hex.En codeToString(c.Data))
81 }
82
83 st.F.SentMessages++
84 }
85 }
86 st.F.SentBytes += proto.Size(bundle)
87
88 return nil
89 }
90
91 func (o *nullOutput) MaxSize() int {
92 return o.bundleSize
93 }
94
95 func (o *nullOutput) Stats() output.Stats {
96 o.statsMu.Lock()
97 defer o.statsMu.Unlock()
98
99 st := o.stats
100 return &st
101 }
102
103 func (o *nullOutput) Close() {}
104
105 func (o *nullOutput) mergeStats(s output.Stats) {
106 o.statsMu.Lock()
107 defer o.statsMu.Unlock()
108
109 o.stats.Merge(s)
110 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698