2019-07-10 13:19:29 -06:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2019-08-14 10:51:42 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry"
|
2019-07-10 13:19:29 -06:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
|
2020-03-01 10:16:26 -07:00
|
|
|
func LogEvent(ctx context.Context, event telemetry.Event) context.Context {
|
|
|
|
if event.Type != telemetry.EventLog {
|
|
|
|
return ctx
|
|
|
|
}
|
2019-07-10 13:19:29 -06:00
|
|
|
client, ok := ctx.Value(clientKey).(Client)
|
|
|
|
if !ok {
|
2020-03-01 10:16:26 -07:00
|
|
|
return ctx
|
2019-07-10 13:19:29 -06:00
|
|
|
}
|
2019-08-14 10:51:42 -06:00
|
|
|
msg := &LogMessageParams{Type: Info, Message: fmt.Sprint(event)}
|
|
|
|
if event.Error != nil {
|
2019-07-10 13:19:29 -06:00
|
|
|
msg.Type = Error
|
|
|
|
}
|
2019-07-19 14:16:41 -06:00
|
|
|
go client.LogMessage(xcontext.Detach(ctx), msg)
|
2020-03-01 10:16:26 -07:00
|
|
|
return ctx
|
2019-07-10 13:19:29 -06:00
|
|
|
}
|