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-07 15:35:36 -06:00
|
|
|
s.logMu.Lock()
|
|
|
|
defer s.logMu.Unlock()
|
2020-04-16 19:49:42 -06:00
|
|
|
logIn(s.log, msg)
|
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-07 15:35:36 -06:00
|
|
|
s.logMu.Lock()
|
|
|
|
defer s.logMu.Unlock()
|
2020-04-16 19:49:42 -06:00
|
|
|
logOut(s.log, msg)
|
|
|
|
count, err := s.stream.Write(ctx, msg)
|
2019-08-27 10:52:32 -06:00
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
func (m *mapped) client(id string, del bool) req {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
v := m.clientCalls[id]
|
|
|
|
if del {
|
|
|
|
delete(m.clientCalls, id)
|
|
|
|
}
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *mapped) server(id string, del bool) req {
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
v := m.serverCalls[id]
|
|
|
|
if del {
|
|
|
|
delete(m.serverCalls, id)
|
|
|
|
}
|
|
|
|
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-16 19:49:42 -06:00
|
|
|
func logCommon(outfd io.Writer) (time.Time, string) {
|
2019-08-27 10:52:32 -06:00
|
|
|
if outfd == nil {
|
2020-04-16 19:49:42 -06:00
|
|
|
return time.Time{}, ""
|
2019-08-27 10:52:32 -06:00
|
|
|
}
|
|
|
|
tm := time.Now()
|
|
|
|
tmfmt := tm.Format("15:04:05.000 PM")
|
2020-04-16 19:49:42 -06:00
|
|
|
return tm, tmfmt
|
2019-08-27 10:52:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// logOut and logIn could be combined. "received"<->"Sending", serverCalls<->clientCalls
|
|
|
|
// but it wouldn't be a lot shorter or clearer and "shutdown" is a special case
|
|
|
|
|
|
|
|
// Writing a message to the client, log it
|
2020-04-16 19:49:42 -06:00
|
|
|
func logOut(outfd io.Writer, msg jsonrpc2.Message) {
|
|
|
|
tm, tmfmt := logCommon(outfd)
|
2020-04-16 19:12:43 -06:00
|
|
|
if msg == nil {
|
2019-08-27 10:52:32 -06:00
|
|
|
return
|
|
|
|
}
|
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())
|
|
|
|
fmt.Fprintf(&buf, "Received request '%s - (%s)'.\n", msg.Method(), id)
|
|
|
|
fmt.Fprintf(&buf, "Params: %s%s", msg.Params(), eor)
|
|
|
|
maps.setServer(id, req{method: msg.Method(), start: tm})
|
|
|
|
case *jsonrpc2.Notification:
|
|
|
|
fmt.Fprintf(&buf, "Received notification '%s'.\n", msg.Method())
|
|
|
|
fmt.Fprintf(&buf, "Params: %s%s", msg.Params(), eor)
|
|
|
|
case *jsonrpc2.Response:
|
|
|
|
id := fmt.Sprint(msg.ID())
|
|
|
|
if err := msg.Err(); err != nil {
|
|
|
|
fmt.Fprintf(outfd, "[Error - %s] Received #%s %s%s", tmfmt, id, err, eor)
|
|
|
|
return
|
|
|
|
}
|
2019-09-03 11:06:23 -06:00
|
|
|
cc := maps.client(id, true)
|
|
|
|
elapsed := tm.Sub(cc.start)
|
2019-08-27 10:52:32 -06:00
|
|
|
fmt.Fprintf(&buf, "Received response '%s - (%s)' in %dms.\n",
|
2019-09-03 11:06:23 -06:00
|
|
|
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
|
|
|
}
|
|
|
|
outfd.Write([]byte(buf.String()))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Got a message from the client, log it
|
2020-04-16 19:49:42 -06:00
|
|
|
func logIn(outfd io.Writer, msg jsonrpc2.Message) {
|
|
|
|
tm, tmfmt := logCommon(outfd)
|
2020-04-16 19:12:43 -06:00
|
|
|
if msg == nil {
|
2019-08-27 10:52:32 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
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())
|
|
|
|
fmt.Fprintf(&buf, "Sending request '%s - (%s)'.\n", msg.Method(), id)
|
|
|
|
fmt.Fprintf(&buf, "Params: %s%s", msg.Params(), eor)
|
|
|
|
maps.setServer(id, req{method: msg.Method(), start: tm})
|
|
|
|
case *jsonrpc2.Notification:
|
|
|
|
fmt.Fprintf(&buf, "Sending notification '%s'.\n", msg.Method())
|
|
|
|
fmt.Fprintf(&buf, "Params: %s%s", msg.Params(), eor)
|
|
|
|
case *jsonrpc2.Response:
|
|
|
|
id := fmt.Sprint(msg.ID())
|
|
|
|
if err := msg.Err(); err != nil {
|
|
|
|
fmt.Fprintf(outfd, "[Error - %s] Sent #%s %s%s", tmfmt, id, err, eor)
|
|
|
|
return
|
2019-10-10 08:59:39 -06:00
|
|
|
}
|
2020-04-16 19:12:43 -06:00
|
|
|
cc := maps.client(id, true)
|
|
|
|
elapsed := tm.Sub(cc.start)
|
|
|
|
fmt.Fprintf(&buf, "Sending response '%s - (%s)' in %dms.\n",
|
|
|
|
cc.method, id, elapsed/time.Millisecond)
|
|
|
|
fmt.Fprintf(&buf, "Result: %s%s", msg.Result(), eor)
|
2019-08-27 10:52:32 -06:00
|
|
|
}
|
|
|
|
outfd.Write([]byte(buf.String()))
|
|
|
|
}
|