2019-08-14 10:51:42 -06:00
|
|
|
// 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"
|
2020-04-03 19:52:20 -06:00
|
|
|
"sync"
|
2019-08-14 10:51:42 -06:00
|
|
|
|
2020-04-17 07:32:56 -06:00
|
|
|
"golang.org/x/tools/internal/event"
|
|
|
|
"golang.org/x/tools/internal/event/core"
|
2020-04-20 13:44:34 -06:00
|
|
|
"golang.org/x/tools/internal/event/label"
|
2019-08-14 10:51:42 -06:00
|
|
|
)
|
|
|
|
|
2020-03-07 16:02:27 -07:00
|
|
|
// LogWriter returns an Exporter that logs events to the supplied writer.
|
2019-08-14 10:51:42 -06:00
|
|
|
// 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.
|
2020-03-07 16:02:27 -07:00
|
|
|
func LogWriter(w io.Writer, onlyErrors bool) event.Exporter {
|
2020-03-18 21:28:57 -06:00
|
|
|
lw := &logWriter{writer: w, onlyErrors: onlyErrors}
|
|
|
|
return lw.ProcessEvent
|
2019-08-14 10:51:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type logWriter struct {
|
2020-04-03 19:52:20 -06:00
|
|
|
mu sync.Mutex
|
|
|
|
buffer [128]byte
|
2019-08-14 10:51:42 -06:00
|
|
|
writer io.Writer
|
|
|
|
onlyErrors bool
|
|
|
|
}
|
|
|
|
|
2020-04-20 13:44:34 -06:00
|
|
|
func (w *logWriter) ProcessEvent(ctx context.Context, ev core.Event, lm label.Map) context.Context {
|
2020-03-07 16:02:27 -07:00
|
|
|
switch {
|
|
|
|
case ev.IsLog():
|
2020-04-20 13:44:34 -06:00
|
|
|
if w.onlyErrors && core.Err.Get(lm) == nil {
|
2020-03-20 06:29:48 -06:00
|
|
|
return ctx
|
2020-03-01 16:35:55 -07:00
|
|
|
}
|
2020-04-03 19:52:20 -06:00
|
|
|
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 "))
|
|
|
|
}
|
2020-04-20 13:44:34 -06:00
|
|
|
msg := core.Msg.Get(lm)
|
2020-04-03 19:52:20 -06:00
|
|
|
io.WriteString(w.writer, msg)
|
2020-04-20 13:44:34 -06:00
|
|
|
if err := core.Err.Get(lm); err != nil {
|
2020-04-03 19:52:20 -06:00
|
|
|
io.WriteString(w.writer, ": ")
|
|
|
|
io.WriteString(w.writer, err.Error())
|
|
|
|
}
|
|
|
|
for index := 0; ev.Valid(index); index++ {
|
2020-04-20 13:44:34 -06:00
|
|
|
l := ev.Label(index)
|
|
|
|
if !l.Valid() || l.Key() == core.Msg || l.Key() == core.Err {
|
2020-04-03 19:52:20 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
io.WriteString(w.writer, "\n\t")
|
2020-04-20 13:44:34 -06:00
|
|
|
io.WriteString(w.writer, l.Key().Name())
|
2020-04-03 19:52:20 -06:00
|
|
|
io.WriteString(w.writer, "=")
|
2020-04-20 13:44:34 -06:00
|
|
|
l.Key().Format(w.writer, buf, l)
|
2020-04-03 19:52:20 -06:00
|
|
|
}
|
|
|
|
io.WriteString(w.writer, "\n")
|
|
|
|
|
2020-03-07 16:02:27 -07:00
|
|
|
case ev.IsStartSpan():
|
2020-03-01 16:35:55 -07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2020-03-07 16:02:27 -07:00
|
|
|
case ev.IsEndSpan():
|
2020-03-01 16:35:55 -07:00
|
|
|
if span := GetSpan(ctx); span != nil {
|
|
|
|
fmt.Fprintf(w.writer, "finish: %v %v", span.Name, span.ID)
|
|
|
|
}
|
2019-08-14 10:51:42 -06:00
|
|
|
}
|
2020-03-20 06:29:48 -06:00
|
|
|
return ctx
|
2019-08-14 10:51:42 -06:00
|
|
|
}
|