2019-08-27 10:52:32 -06:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
2019-09-03 11:06:23 -06:00
|
|
|
"sync"
|
2019-08-27 10:52:32 -06:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/jsonrpc2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type loggingStream struct {
|
|
|
|
stream jsonrpc2.Stream
|
2020-04-07 15:35:36 -06:00
|
|
|
logMu sync.Mutex
|
2019-08-27 10:52:32 -06:00
|
|
|
log io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoggingStream returns a stream that does LSP protocol logging too
|
|
|
|
func LoggingStream(str jsonrpc2.Stream, w io.Writer) jsonrpc2.Stream {
|
2020-04-07 15:35:36 -06:00
|
|
|
return &loggingStream{stream: str, log: w}
|
2019-08-27 10:52:32 -06:00
|
|
|
}
|
|
|
|
|
2020-04-16 19:49:42 -06:00
|
|
|
func (s *loggingStream) Read(ctx context.Context) (jsonrpc2.Message, int64, error) {
|
|
|
|
msg, count, err := s.stream.Read(ctx)
|
2019-08-27 10:52:32 -06:00
|
|
|
if err == nil {
|
2020-04-29 16:50:39 -06:00
|
|
|
s.logCommon(msg, true)
|
2019-08-27 10:52:32 -06:00
|
|
|
}
|
2020-04-16 19:49:42 -06:00
|
|
|
return msg, count, err
|
2019-08-27 10:52:32 -06:00
|
|
|
}
|
|
|
|
|
2020-04-16 19:49:42 -06:00
|
|
|
func (s *loggingStream) Write(ctx context.Context, msg jsonrpc2.Message) (int64, error) {
|
2020-04-29 16:50:39 -06:00
|
|
|
s.logCommon(msg, false)
|
2020-04-16 19:49:42 -06:00
|
|
|
count, err := s.stream.Write(ctx, msg)
|
2019-08-27 10:52:32 -06:00
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
|
2020-05-01 14:33:27 -06:00
|
|
|
func (s *loggingStream) Close() error {
|
|
|
|
return s.stream.Close()
|
|
|
|
}
|
|
|
|
|
2019-08-27 10:52:32 -06:00
|
|
|
type req struct {
|
|
|
|
method string
|
|
|
|
start time.Time
|
|
|
|
}
|
|
|
|
|
2019-09-03 11:06:23 -06:00
|
|
|
type mapped struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
clientCalls map[string]req
|
|
|
|
serverCalls map[string]req
|
|
|
|
}
|
|
|
|
|
|
|
|
var maps = &mapped{
|
|
|
|
sync.Mutex{},
|
|
|
|
make(map[string]req),
|
|
|
|
make(map[string]req),
|
|
|
|
}
|
|
|
|
|
|
|
|
// these 4 methods are each used exactly once, but it seemed
|
|
|
|
// better to have the encapsulation rather than ad hoc mutex
|
|
|
|
// code in 4 places
|
2020-04-21 09:47:21 -06:00
|
|
|
func (m *mapped) client(id string) req {
|
2019-09-03 11:06:23 -06:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
v := m.clientCalls[id]
|
2020-04-21 09:47:21 -06:00
|
|
|
delete(m.clientCalls, id)
|
2019-09-03 11:06:23 -06:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2020-04-21 09:47:21 -06:00
|
|
|
func (m *mapped) server(id string) req {
|
2019-09-03 11:06:23 -06:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
v := m.serverCalls[id]
|
2020-04-21 09:47:21 -06:00
|
|
|
delete(m.serverCalls, id)
|
2019-09-03 11:06:23 -06:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mapped) setClient(id string, r req) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
m.clientCalls[id] = r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mapped) setServer(id string, r req) {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
m.serverCalls[id] = r
|
|
|
|
}
|
2019-08-27 10:52:32 -06:00
|
|
|
|
|
|
|
const eor = "\r\n\r\n\r\n"
|
|
|
|
|
2020-04-29 16:50:39 -06:00
|
|
|
func (s *loggingStream) logCommon(msg jsonrpc2.Message, isRead bool) {
|
|
|
|
s.logMu.Lock()
|
|
|
|
defer s.logMu.Unlock()
|
2020-04-21 09:47:21 -06:00
|
|
|
direction, pastTense := "Received", "Received"
|
|
|
|
get, set := maps.client, maps.setServer
|
|
|
|
if isRead {
|
|
|
|
direction, pastTense = "Sending", "Sent"
|
|
|
|
get, set = maps.server, maps.setClient
|
|
|
|
}
|
2020-04-29 16:50:39 -06:00
|
|
|
if msg == nil || s.log == nil {
|
2020-04-17 10:49:46 -06:00
|
|
|
return
|
2019-08-27 10:52:32 -06:00
|
|
|
}
|
|
|
|
tm := time.Now()
|
|
|
|
tmfmt := tm.Format("15:04:05.000 PM")
|
2020-04-16 19:12:43 -06:00
|
|
|
|
2019-08-27 10:52:32 -06:00
|
|
|
buf := strings.Builder{}
|
|
|
|
fmt.Fprintf(&buf, "[Trace - %s] ", tmfmt) // common beginning
|
2020-04-16 19:12:43 -06:00
|
|
|
switch msg := msg.(type) {
|
|
|
|
case *jsonrpc2.Call:
|
|
|
|
id := fmt.Sprint(msg.ID())
|
2020-04-17 10:49:46 -06:00
|
|
|
fmt.Fprintf(&buf, "%s request '%s - (%s)'.\n", direction, msg.Method(), id)
|
2020-04-16 19:12:43 -06:00
|
|
|
fmt.Fprintf(&buf, "Params: %s%s", msg.Params(), eor)
|
2020-04-21 09:47:21 -06:00
|
|
|
set(id, req{method: msg.Method(), start: tm})
|
2020-04-16 19:12:43 -06:00
|
|
|
case *jsonrpc2.Notification:
|
2020-04-17 10:49:46 -06:00
|
|
|
fmt.Fprintf(&buf, "%s notification '%s'.\n", direction, msg.Method())
|
2020-04-16 19:12:43 -06:00
|
|
|
fmt.Fprintf(&buf, "Params: %s%s", msg.Params(), eor)
|
|
|
|
case *jsonrpc2.Response:
|
|
|
|
id := fmt.Sprint(msg.ID())
|
|
|
|
if err := msg.Err(); err != nil {
|
2020-04-29 16:50:39 -06:00
|
|
|
fmt.Fprintf(s.log, "[Error - %s] %s #%s %s%s", pastTense, tmfmt, id, err, eor)
|
2020-04-16 19:12:43 -06:00
|
|
|
return
|
|
|
|
}
|
2020-04-21 09:47:21 -06:00
|
|
|
cc := get(id)
|
2019-09-03 11:06:23 -06:00
|
|
|
elapsed := tm.Sub(cc.start)
|
2020-04-21 09:47:21 -06:00
|
|
|
fmt.Fprintf(&buf, "%s response '%s - (%s)' in %dms.\n",
|
|
|
|
direction, cc.method, id, elapsed/time.Millisecond)
|
2020-04-16 19:12:43 -06:00
|
|
|
fmt.Fprintf(&buf, "Result: %s%s", msg.Result(), eor)
|
2019-08-27 10:52:32 -06:00
|
|
|
}
|
2020-04-29 16:50:39 -06:00
|
|
|
s.log.Write([]byte(buf.String()))
|
2019-08-27 10:52:32 -06:00
|
|
|
}
|