| OLD | NEW |
| (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 log |
| 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 // logOutput is an Output implementation that logs messages to its contexts' |
| 21 // Logger instance. |
| 22 type logOutput struct { |
| 23 sync.Mutex |
| 24 ctx context.Context |
| 25 |
| 26 // bundleSize is the maximum size of the Butler bundle to use. |
| 27 bundleSize int |
| 28 |
| 29 stats output.StatsBase |
| 30 } |
| 31 |
| 32 // logOutput implements output.Output. |
| 33 var _ output.Output = (*logOutput)(nil) |
| 34 |
| 35 // New instantes a new log output instance. |
| 36 func New(ctx context.Context, bundleSize int) output.Output { |
| 37 o := logOutput{ |
| 38 ctx: ctx, |
| 39 bundleSize: bundleSize, |
| 40 } |
| 41 o.ctx = log.SetFields(o.ctx, log.Fields{ |
| 42 "output": &o, |
| 43 }) |
| 44 return &o |
| 45 } |
| 46 |
| 47 func (o *logOutput) SendBundle(bundle *protocol.ButlerLogBundle) error { |
| 48 o.Lock() |
| 49 defer o.Unlock() |
| 50 |
| 51 for _, e := range bundle.Entries { |
| 52 path := types.StreamName(e.Desc.Prefix).Join(types.StreamName(e.
Desc.Name)) |
| 53 ctx := log.SetField(o.ctx, "streamPath", path) |
| 54 |
| 55 log.Fields{ |
| 56 "count": len(e.Logs), |
| 57 "descriptor": e.Desc.String(), |
| 58 }.Infof(ctx, "Received stream logs.") |
| 59 |
| 60 for _, le := range e.Logs { |
| 61 log.Fields{ |
| 62 "timeOffset": le.TimeOffset.Duration(), |
| 63 "prefixIndex": le.PrefixIndex, |
| 64 "streamIndex": le.StreamIndex, |
| 65 "sequence": le.Sequence, |
| 66 }.Infof(ctx, "Received message.") |
| 67 if c := le.GetText(); c != nil { |
| 68 for idx, l := range c.Lines { |
| 69 log.Infof(ctx, "Line %d) %s (%s)", idx,
l.Value, strconv.Quote(l.Delimiter)) |
| 70 } |
| 71 } |
| 72 if c := le.GetBinary(); c != nil { |
| 73 log.Infof(ctx, "Binary) %s", hex.EncodeToString(
c.Data)) |
| 74 } |
| 75 if c := le.GetDatagram(); c != nil { |
| 76 log.Infof(ctx, "Datagram %d: %s", c.Size, hex.En
codeToString(c.Data)) |
| 77 } |
| 78 |
| 79 o.stats.F.SentMessages++ |
| 80 } |
| 81 } |
| 82 o.stats.F.SentBytes += proto.Size(bundle) |
| 83 |
| 84 return nil |
| 85 } |
| 86 |
| 87 func (o *logOutput) MaxSize() int { |
| 88 return o.bundleSize |
| 89 } |
| 90 |
| 91 func (o *logOutput) Stats() output.Stats { |
| 92 o.Lock() |
| 93 defer o.Unlock() |
| 94 |
| 95 st := o.stats |
| 96 return &st |
| 97 } |
| 98 |
| 99 func (o *logOutput) Close() {} |
| OLD | NEW |