2019-07-10 13:19:29 -06:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2019-08-13 13:07:39 -06:00
|
|
|
"golang.org/x/tools/internal/telemetry/log"
|
|
|
|
"golang.org/x/tools/internal/telemetry/tag"
|
2019-07-10 13:19:29 -06:00
|
|
|
"golang.org/x/tools/internal/xcontext"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
log.AddLogger(logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
type contextKey int
|
|
|
|
|
|
|
|
const (
|
|
|
|
clientKey = contextKey(iota)
|
|
|
|
)
|
|
|
|
|
|
|
|
func WithClient(ctx context.Context, client Client) context.Context {
|
|
|
|
return context.WithValue(ctx, clientKey, client)
|
|
|
|
}
|
|
|
|
|
|
|
|
// logger implements log.Logger in terms of the LogMessage call to a client.
|
|
|
|
func logger(ctx context.Context, at time.Time, tags tag.List) bool {
|
|
|
|
client, ok := ctx.Value(clientKey).(Client)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
entry := log.ToEntry(ctx, time.Time{}, tags)
|
|
|
|
msg := &LogMessageParams{Type: Info, Message: fmt.Sprint(entry)}
|
|
|
|
if entry.Error != nil {
|
|
|
|
msg.Type = Error
|
|
|
|
}
|
2019-07-19 14:16:41 -06:00
|
|
|
go client.LogMessage(xcontext.Detach(ctx), msg)
|
2019-07-10 13:19:29 -06:00
|
|
|
return true
|
|
|
|
}
|