-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
log.go
156 lines (124 loc) · 3.31 KB
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// SPDX-FileCopyrightText: 2021 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package protocol
import (
"bytes"
"context"
"fmt"
"io"
"sync"
"time"
"go.lsp.dev/jsonrpc2"
)
// loggingStream represents a logging of jsonrpc2.Stream.
type loggingStream struct {
stream jsonrpc2.Stream
log io.Writer
logMu sync.Mutex
}
// LoggingStream returns a stream that does LSP protocol logging.
func LoggingStream(stream jsonrpc2.Stream, w io.Writer) jsonrpc2.Stream {
return &loggingStream{
stream: stream,
log: w,
}
}
// Read implements jsonrpc2.Stream.Read.
func (s *loggingStream) Read(ctx context.Context) (jsonrpc2.Message, int64, error) {
msg, count, err := s.stream.Read(ctx)
if err == nil {
s.logCommon(msg, true)
}
return msg, count, err
}
// Write implements jsonrpc2.Stream.Write.
func (s *loggingStream) Write(ctx context.Context, msg jsonrpc2.Message) (int64, error) {
s.logCommon(msg, false)
count, err := s.stream.Write(ctx, msg)
return count, err
}
// Close implements jsonrpc2.Stream.Close.
func (s *loggingStream) Close() error {
return s.stream.Close()
}
type req struct {
method string
start time.Time
}
type mapped struct {
mu sync.Mutex
clientCalls map[string]req
serverCalls map[string]req
}
var maps = &mapped{
mu: sync.Mutex{},
clientCalls: make(map[string]req),
serverCalls: 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) req {
m.mu.Lock()
v := m.clientCalls[id]
delete(m.clientCalls, id)
m.mu.Unlock()
return v
}
func (m *mapped) server(id string) req {
m.mu.Lock()
v := m.serverCalls[id]
delete(m.serverCalls, id)
m.mu.Unlock()
return v
}
func (m *mapped) setClient(id string, r req) {
m.mu.Lock()
m.clientCalls[id] = r
m.mu.Unlock()
}
func (m *mapped) setServer(id string, r req) {
m.mu.Lock()
m.serverCalls[id] = r
m.mu.Unlock()
}
const eor = "\r\n\r\n\r\n"
func (s *loggingStream) logCommon(msg jsonrpc2.Message, isRead bool) {
if msg == nil || s.log == nil {
return
}
s.logMu.Lock()
direction, pastTense := "Received", "Received"
get, set := maps.client, maps.setServer
if isRead {
direction, pastTense = "Sending", "Sent"
get, set = maps.server, maps.setClient
}
tm := time.Now()
tmfmt := tm.Format("15:04:05.000 PM")
var buf bytes.Buffer
fmt.Fprintf(&buf, "[Trace - %s] ", tmfmt) // common beginning
switch msg := msg.(type) {
case *jsonrpc2.Call:
id := fmt.Sprint(msg.ID())
fmt.Fprintf(&buf, "%s request '%s - (%s)'.\n", direction, msg.Method(), id)
fmt.Fprintf(&buf, "Params: %s%s", msg.Params(), eor)
set(id, req{method: msg.Method(), start: tm})
case *jsonrpc2.Notification:
fmt.Fprintf(&buf, "%s notification '%s'.\n", direction, 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(s.log, "[Error - %s] %s #%s %s%s", pastTense, tmfmt, id, err, eor)
return
}
cc := get(id)
elapsed := tm.Sub(cc.start)
fmt.Fprintf(&buf, "%s response '%s - (%s)' in %dms.\n",
direction, cc.method, id, elapsed/time.Millisecond)
fmt.Fprintf(&buf, "Result: %s%s", msg.Result(), eor)
}
s.log.Write(buf.Bytes())
s.logMu.Unlock()
}