1
0
mirror of https://github.com/golang/go synced 2024-11-05 18:36:10 -07:00
go/internal/event/export/log.go
Ian Cottrell a90b7300be internal/event: remove the event.eventType type
Instead of tagging events with their type, instead we infer the type from
the label pattern.
The standard event creators all have a matching test that returns true
if the the labels pattern matches the ones that would be built by the
creator.
Spans and logs already have a unique label pattern, other event types
required a special label marker.
This makes the system much more extensible, and also cleans up some
the API.

Change-Id: I1fbc9ec07aa84ead6c12bbd5ca65b13b605bfa4a
Reviewed-on: https://go-review.googlesource.com/c/tools/+/229242
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
2020-04-27 15:30:19 +00:00

80 lines
2.0 KiB
Go

// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package export
import (
"context"
"fmt"
"io"
"sync"
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/event/core"
"golang.org/x/tools/internal/event/keys"
"golang.org/x/tools/internal/event/label"
)
// LogWriter returns an Exporter that logs events to the supplied writer.
// If onlyErrors is true it does not log any event that did not have an
// associated error.
// It ignores all telemetry other than log events.
func LogWriter(w io.Writer, onlyErrors bool) event.Exporter {
lw := &logWriter{writer: w, onlyErrors: onlyErrors}
return lw.ProcessEvent
}
type logWriter struct {
mu sync.Mutex
buffer [128]byte
writer io.Writer
onlyErrors bool
}
func (w *logWriter) ProcessEvent(ctx context.Context, ev core.Event, lm label.Map) context.Context {
switch {
case event.IsLog(ev):
if w.onlyErrors && !event.IsError(ev) {
return ctx
}
w.mu.Lock()
defer w.mu.Unlock()
buf := w.buffer[:0]
if !ev.At().IsZero() {
w.writer.Write(ev.At().AppendFormat(buf, "2006/01/02 15:04:05 "))
}
msg := keys.Msg.Get(lm)
io.WriteString(w.writer, msg)
if err := keys.Err.Get(lm); err != nil {
io.WriteString(w.writer, ": ")
io.WriteString(w.writer, err.Error())
}
for index := 0; ev.Valid(index); index++ {
l := ev.Label(index)
if !l.Valid() || l.Key() == keys.Msg || l.Key() == keys.Err {
continue
}
io.WriteString(w.writer, "\n\t")
io.WriteString(w.writer, l.Key().Name())
io.WriteString(w.writer, "=")
l.Key().Format(w.writer, buf, l)
}
io.WriteString(w.writer, "\n")
case event.IsStart(ev):
if span := GetSpan(ctx); span != nil {
fmt.Fprintf(w.writer, "start: %v %v", span.Name, span.ID)
if span.ParentID.IsValid() {
fmt.Fprintf(w.writer, "[%v]", span.ParentID)
}
}
case event.IsEnd(ev):
if span := GetSpan(ctx); span != nil {
fmt.Fprintf(w.writer, "finish: %v %v", span.Name, span.ID)
}
}
return ctx
}