mirror of
https://github.com/golang/go
synced 2024-11-18 20:44:45 -07:00
206ec5b82a
Change-Id: Idc662385ed08bd62593ccd1d54afd3fa8c1a7d29 Reviewed-on: https://go-review.googlesource.com/c/tools/+/222558 Run-TryBot: Ian Cottrell <iancottrell@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
36 lines
665 B
Go
36 lines
665 B
Go
package protocol
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"golang.org/x/tools/internal/telemetry/event"
|
|
"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 event.Event) context.Context {
|
|
if !ev.IsLog() {
|
|
return ctx
|
|
}
|
|
client, ok := ctx.Value(clientKey).(Client)
|
|
if !ok {
|
|
return ctx
|
|
}
|
|
msg := &LogMessageParams{Type: Info, Message: fmt.Sprint(ev)}
|
|
if ev.Error != nil {
|
|
msg.Type = Error
|
|
}
|
|
go client.LogMessage(xcontext.Detach(ctx), msg)
|
|
return ctx
|
|
}
|