mirror of
https://github.com/golang/go
synced 2024-11-19 07:14:45 -07:00
7ec096a112
Also add enough support that using it from within the context of the lsp will report back to the original client. Change-Id: I081f157c289642454e9f0476747b2131dcd4e16c Reviewed-on: https://go-review.googlesource.com/c/tools/+/185996 Run-TryBot: Ian Cottrell <iancottrell@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
43 lines
946 B
Go
43 lines
946 B
Go
package protocol
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"golang.org/x/tools/internal/lsp/telemetry/log"
|
|
"golang.org/x/tools/internal/lsp/telemetry/tag"
|
|
"golang.org/x/tools/internal/lsp/xlog"
|
|
"golang.org/x/tools/internal/xcontext"
|
|
)
|
|
|
|
func init() {
|
|
log.AddLogger(logger)
|
|
}
|
|
|
|
type contextKey int
|
|
|
|
const (
|
|
clientKey = contextKey(iota)
|
|
)
|
|
|
|
func WithClient(ctx context.Context, client Client) context.Context {
|
|
ctx = xlog.With(ctx, logSink{client: client})
|
|
return context.WithValue(ctx, clientKey, client)
|
|
}
|
|
|
|
// logger implements log.Logger in terms of the LogMessage call to a client.
|
|
func logger(ctx context.Context, at time.Time, tags tag.List) bool {
|
|
client, ok := ctx.Value(clientKey).(Client)
|
|
if !ok {
|
|
return false
|
|
}
|
|
entry := log.ToEntry(ctx, time.Time{}, tags)
|
|
msg := &LogMessageParams{Type: Info, Message: fmt.Sprint(entry)}
|
|
if entry.Error != nil {
|
|
msg.Type = Error
|
|
}
|
|
client.LogMessage(xcontext.Detach(ctx), msg)
|
|
return true
|
|
}
|