-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathlsp_logger.go
77 lines (69 loc) · 2.63 KB
/
lsp_logger.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
package ls
import (
"fmt"
"log"
"github.com/fatih/color"
"go.bug.st/json"
"go.bug.st/lsp/jsonrpc"
)
type LSPLogger struct {
IncomingPrefix, OutgoingPrefix string
HiColor, LoColor func(format string, a ...interface{}) string
ErrorColor func(format string, a ...interface{}) string
}
func (l *LSPLogger) LogOutgoingRequest(id string, method string, params json.RawMessage) {
log.Print(l.HiColor("%s REQU %s %s", l.OutgoingPrefix, method, id))
}
func (l *LSPLogger) LogOutgoingCancelRequest(id string) {
log.Print(l.LoColor("%s CANCEL %s", l.OutgoingPrefix, id))
}
func (l *LSPLogger) LogIncomingResponse(id string, method string, resp json.RawMessage, respErr *jsonrpc.ResponseError) {
e := ""
if respErr != nil {
e = l.ErrorColor(" ERROR: %s", respErr.AsError())
}
log.Print(l.LoColor("%s RESP %s %s%s", l.IncomingPrefix, method, id, e))
}
func (l *LSPLogger) LogOutgoingNotification(method string, params json.RawMessage) {
log.Print(l.HiColor("%s NOTIF %s", l.OutgoingPrefix, method))
}
func (l *LSPLogger) LogIncomingRequest(id string, method string, params json.RawMessage) jsonrpc.FunctionLogger {
spaces := " "
log.Print(l.HiColor(fmt.Sprintf("%s REQU %s %s", l.IncomingPrefix, method, id)))
return &LSPFunctionLogger{
colorFunc: l.HiColor,
prefix: fmt.Sprintf("%s %s %s", spaces[:len(l.IncomingPrefix)], method, id),
}
}
func (l *LSPLogger) LogIncomingCancelRequest(id string) {
log.Print(l.LoColor("%s CANCEL %s", l.IncomingPrefix, id))
}
func (l *LSPLogger) LogOutgoingResponse(id string, method string, resp json.RawMessage, respErr *jsonrpc.ResponseError) {
e := ""
if respErr != nil {
e = l.ErrorColor(" ERROR: %s", respErr.AsError())
}
log.Print(l.LoColor("%s RESP %s %s%s", l.OutgoingPrefix, method, id, e))
}
func (l *LSPLogger) LogIncomingNotification(method string, params json.RawMessage) jsonrpc.FunctionLogger {
spaces := " "
log.Print(l.HiColor(fmt.Sprintf("%s NOTIF %s", l.IncomingPrefix, method)))
return &LSPFunctionLogger{
colorFunc: l.HiColor,
prefix: fmt.Sprintf("%s %s", spaces[:len(l.IncomingPrefix)], method),
}
}
type LSPFunctionLogger struct {
colorFunc func(format string, a ...interface{}) string
prefix string
}
func NewLSPFunctionLogger(colofFunction func(format string, a ...interface{}) string, prefix string) *LSPFunctionLogger {
color.NoColor = false
return &LSPFunctionLogger{
colorFunc: colofFunction,
prefix: prefix,
}
}
func (l *LSPFunctionLogger) Logf(format string, a ...interface{}) {
log.Print(l.colorFunc(l.prefix+": "+format, a...))
}