1
0
mirror of https://github.com/golang/go synced 2024-11-18 15:44:41 -07:00
go/internal/lsp/protocol/context.go
Ian Cottrell 01e0872ccf internal/event: improve the logging of events
This extracts the printing code from the log writer so it can be re-used
and then changes the loggers in the lsp to use it.
This means that messages that used to look like

  date:
    message=text

now print as

  date: text

which makes the logs a lot easier to read.

Change-Id: I9dfbae47cdc9aeb7d3ca3279e445f39f2e590827
Reviewed-on: https://go-review.googlesource.com/c/tools/+/232989
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
2020-05-11 17:49:55 +00:00

42 lines
882 B
Go

package protocol
import (
"bytes"
"context"
"golang.org/x/tools/internal/event"
"golang.org/x/tools/internal/event/core"
"golang.org/x/tools/internal/event/export"
"golang.org/x/tools/internal/event/label"
"golang.org/x/tools/internal/xcontext"
)
type contextKey int
const (
clientKey = contextKey(iota)
)
func WithClient(ctx context.Context, client Client) context.Context {
return context.WithValue(ctx, clientKey, client)
}
func LogEvent(ctx context.Context, ev core.Event, tags label.Map) context.Context {
if !event.IsLog(ev) {
return ctx
}
client, ok := ctx.Value(clientKey).(Client)
if !ok {
return ctx
}
buf := &bytes.Buffer{}
p := export.Printer{}
p.WriteEvent(buf, ev, tags)
msg := &LogMessageParams{Type: Info, Message: buf.String()}
if event.IsError(ev) {
msg.Type = Error
}
go client.LogMessage(xcontext.Detach(ctx), msg)
return ctx
}