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