github.com/jd-ly/tools@v0.5.7/internal/lsp/protocol/context.go (about) 1 package protocol 2 3 import ( 4 "bytes" 5 "context" 6 7 "github.com/jd-ly/tools/internal/event" 8 "github.com/jd-ly/tools/internal/event/core" 9 "github.com/jd-ly/tools/internal/event/export" 10 "github.com/jd-ly/tools/internal/event/label" 11 "github.com/jd-ly/tools/internal/xcontext" 12 ) 13 14 type contextKey int 15 16 const ( 17 clientKey = contextKey(iota) 18 ) 19 20 func WithClient(ctx context.Context, client Client) context.Context { 21 return context.WithValue(ctx, clientKey, client) 22 } 23 24 func LogEvent(ctx context.Context, ev core.Event, tags label.Map) context.Context { 25 if !event.IsLog(ev) { 26 return ctx 27 } 28 client, ok := ctx.Value(clientKey).(Client) 29 if !ok { 30 return ctx 31 } 32 buf := &bytes.Buffer{} 33 p := export.Printer{} 34 p.WriteEvent(buf, ev, tags) 35 msg := &LogMessageParams{Type: Info, Message: buf.String()} 36 if event.IsError(ev) { 37 msg.Type = Error 38 } 39 go client.LogMessage(xcontext.Detach(ctx), msg) 40 return ctx 41 }