2019-07-10 13:19:29 -06:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2020-05-08 15:27:48 -06:00
|
|
|
"bytes"
|
2019-07-10 13:19:29 -06:00
|
|
|
"context"
|
|
|
|
|
2020-04-21 07:41:57 -06:00
|
|
|
"golang.org/x/tools/internal/event"
|
2020-04-17 07:32:56 -06:00
|
|
|
"golang.org/x/tools/internal/event/core"
|
2020-05-08 15:27:48 -06:00
|
|
|
"golang.org/x/tools/internal/event/export"
|
2020-04-20 13:44:34 -06:00
|
|
|
"golang.org/x/tools/internal/event/label"
|
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-04-20 13:44:34 -06:00
|
|
|
func LogEvent(ctx context.Context, ev core.Event, tags label.Map) context.Context {
|
2020-04-21 07:41:57 -06:00
|
|
|
if !event.IsLog(ev) {
|
2020-03-20 06:29:48 -06:00
|
|
|
return ctx
|
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-20 06:29:48 -06:00
|
|
|
return ctx
|
2019-07-10 13:19:29 -06:00
|
|
|
}
|
2020-05-08 15:27:48 -06:00
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
p := export.Printer{}
|
|
|
|
p.WriteEvent(buf, ev, tags)
|
|
|
|
msg := &LogMessageParams{Type: Info, Message: buf.String()}
|
2020-04-21 07:41:57 -06:00
|
|
|
if event.IsError(ev) {
|
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-20 06:29:48 -06:00
|
|
|
return ctx
|
2019-07-10 13:19:29 -06:00
|
|
|
}
|