1package protocol 2 3import ( 4 "context" 5 "fmt" 6 7 "golang.org/x/tools/internal/telemetry" 8 "golang.org/x/tools/internal/telemetry/export" 9 "golang.org/x/tools/internal/xcontext" 10) 11 12func init() { 13 export.AddExporters(logExporter{}) 14} 15 16type contextKey int 17 18const ( 19 clientKey = contextKey(iota) 20) 21 22func WithClient(ctx context.Context, client Client) context.Context { 23 return context.WithValue(ctx, clientKey, client) 24} 25 26// logExporter sends the log event back to the client if there is one stored on the 27// context. 28type logExporter struct{} 29 30func (logExporter) StartSpan(context.Context, *telemetry.Span) {} 31func (logExporter) FinishSpan(context.Context, *telemetry.Span) {} 32func (logExporter) Metric(context.Context, telemetry.MetricData) {} 33func (logExporter) Flush() {} 34 35func (logExporter) Log(ctx context.Context, event telemetry.Event) { 36 client, ok := ctx.Value(clientKey).(Client) 37 if !ok { 38 return 39 } 40 msg := &LogMessageParams{Type: Info, Message: fmt.Sprint(event)} 41 if event.Error != nil { 42 msg.Type = Error 43 } 44 go client.LogMessage(xcontext.Detach(ctx), msg) 45} 46