1 // Copyright 2019 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
15 "golang.org/x/tools/internal/jsonrpc2"
18 type loggingStream struct {
19 stream jsonrpc2.Stream
24 // LoggingStream returns a stream that does LSP protocol logging too
25 func LoggingStream(str jsonrpc2.Stream, w io.Writer) jsonrpc2.Stream {
26 return &loggingStream{stream: str, log: w}
29 func (s *loggingStream) Read(ctx context.Context) (jsonrpc2.Message, int64, error) {
30 msg, count, err := s.stream.Read(ctx)
32 s.logCommon(msg, true)
34 return msg, count, err
37 func (s *loggingStream) Write(ctx context.Context, msg jsonrpc2.Message) (int64, error) {
38 s.logCommon(msg, false)
39 count, err := s.stream.Write(ctx, msg)
43 func (s *loggingStream) Close() error {
44 return s.stream.Close()
54 clientCalls map[string]req
55 serverCalls map[string]req
64 // these 4 methods are each used exactly once, but it seemed
65 // better to have the encapsulation rather than ad hoc mutex
67 func (m *mapped) client(id string) req {
70 v := m.clientCalls[id]
71 delete(m.clientCalls, id)
75 func (m *mapped) server(id string) req {
78 v := m.serverCalls[id]
79 delete(m.serverCalls, id)
83 func (m *mapped) setClient(id string, r req) {
89 func (m *mapped) setServer(id string, r req) {
95 const eor = "\r\n\r\n\r\n"
97 func (s *loggingStream) logCommon(msg jsonrpc2.Message, isRead bool) {
99 defer s.logMu.Unlock()
100 direction, pastTense := "Received", "Received"
101 get, set := maps.client, maps.setServer
103 direction, pastTense = "Sending", "Sent"
104 get, set = maps.server, maps.setClient
106 if msg == nil || s.log == nil {
110 tmfmt := tm.Format("15:04:05.000 PM")
112 buf := strings.Builder{}
113 fmt.Fprintf(&buf, "[Trace - %s] ", tmfmt) // common beginning
114 switch msg := msg.(type) {
116 id := fmt.Sprint(msg.ID())
117 fmt.Fprintf(&buf, "%s request '%s - (%s)'.\n", direction, msg.Method(), id)
118 fmt.Fprintf(&buf, "Params: %s%s", msg.Params(), eor)
119 set(id, req{method: msg.Method(), start: tm})
120 case *jsonrpc2.Notification:
121 fmt.Fprintf(&buf, "%s notification '%s'.\n", direction, msg.Method())
122 fmt.Fprintf(&buf, "Params: %s%s", msg.Params(), eor)
123 case *jsonrpc2.Response:
124 id := fmt.Sprint(msg.ID())
125 if err := msg.Err(); err != nil {
126 fmt.Fprintf(s.log, "[Error - %s] %s #%s %s%s", pastTense, tmfmt, id, err, eor)
130 elapsed := tm.Sub(cc.start)
131 fmt.Fprintf(&buf, "%s response '%s - (%s)' in %dms.\n",
132 direction, cc.method, id, elapsed/time.Millisecond)
133 fmt.Fprintf(&buf, "Result: %s%s", msg.Result(), eor)
135 s.log.Write([]byte(buf.String()))