Skip to content

Commit 16fddf1

Browse files
authored
Merge pull request #53 from bcmi-labs/logging
Improved and colorful logging
2 parents 60d3a1b + 35f4685 commit 16fddf1

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.12
55
require (
66
github.com/arduino/arduino-cli v0.0.0-20201215104024-6a177ebf56f2
77
github.com/arduino/go-paths-helper v1.5.0
8+
github.com/fatih/color v1.7.0
89
github.com/pkg/errors v0.9.1
910
github.com/sourcegraph/jsonrpc2 v0.0.0-20200429184054-15c2290dcb37
1011
github.com/stretchr/testify v1.6.1

Diff for: handler/builder.go

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ func (handler *InoHandler) rebuildEnvironmentLoop() {
5454
// Regenerate preprocessed sketch!
5555
done := make(chan bool)
5656
go func() {
57+
defer streams.CatchAndLogPanic()
5758

5859
handler.progressHandler.Create("arduinoLanguageServerRebuild")
5960
handler.progressHandler.Begin("arduinoLanguageServerRebuild", &lsp.WorkDoneProgressBegin{

Diff for: handler/handler.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,10 @@ func NewInoHandler(stdio io.ReadWriteCloser, board lsp.Board) *InoHandler {
8383
handler.clangdStarted = sync.NewCond(&handler.dataMux)
8484
stdStream := jsonrpc2.NewBufferedStream(stdio, jsonrpc2.VSCodeObjectCodec{})
8585
var stdHandler jsonrpc2.Handler = jsonrpc2.HandlerWithError(handler.HandleMessageFromIDE)
86-
handler.StdioConn = jsonrpc2.NewConn(context.Background(), stdStream, stdHandler)
86+
handler.StdioConn = jsonrpc2.NewConn(context.Background(), stdStream, stdHandler,
87+
jsonrpc2.OnRecv(streams.JSONRPCConnLogOnRecv("IDE --> LS CL:")),
88+
jsonrpc2.OnSend(streams.JSONRPCConnLogOnSend("IDE <-- LS CL:")),
89+
)
8790

8891
handler.progressHandler = NewProgressProxy(handler.StdioConn)
8992

@@ -176,6 +179,8 @@ func (handler *InoHandler) HandleMessageFromIDE(ctx context.Context, conn *jsonr
176179
// method "initialize"
177180

178181
go func() {
182+
defer streams.CatchAndLogPanic()
183+
179184
// Start clangd asynchronously
180185
log.Printf("LS --- initializing workbench (queued)")
181186
handler.dataMux.Lock()
@@ -514,7 +519,9 @@ func (handler *InoHandler) initializeWorkbench(ctx context.Context, params *lsp.
514519

515520
clangdStream := jsonrpc2.NewBufferedStream(clangdStdio, jsonrpc2.VSCodeObjectCodec{})
516521
clangdHandler := AsyncHandler{jsonrpc2.HandlerWithError(handler.FromClangd)}
517-
handler.ClangdConn = jsonrpc2.NewConn(context.Background(), clangdStream, clangdHandler)
522+
handler.ClangdConn = jsonrpc2.NewConn(context.Background(), clangdStream, clangdHandler,
523+
jsonrpc2.OnRecv(streams.JSONRPCConnLogOnRecv("IDE LS <-- CL:")),
524+
jsonrpc2.OnSend(streams.JSONRPCConnLogOnSend("IDE LS --> CL:")))
518525

519526
// Send initialization command to clangd
520527
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
@@ -1087,12 +1094,15 @@ func (handler *InoHandler) transformClangdResult(method string, inoURI, cppURI l
10871094

10881095
if r.DocumentSymbolArray != nil {
10891096
// Treat the input as []DocumentSymbol
1097+
log.Printf(" <-- documentSymbol(%d document symbols)", len(*r.DocumentSymbolArray))
10901098
return handler.cpp2inoDocumentSymbols(*r.DocumentSymbolArray, inoURI)
10911099
} else if r.SymbolInformationArray != nil {
10921100
// Treat the input as []SymbolInformation
1101+
log.Printf(" <-- documentSymbol(%d symbol information)", len(*r.SymbolInformationArray))
10931102
return handler.cpp2inoSymbolInformation(*r.SymbolInformationArray)
10941103
} else {
10951104
// Treat the input as null
1105+
log.Printf(" <-- null documentSymbol")
10961106
}
10971107

10981108
case *[]lsp.CommandOrCodeAction:

Diff for: streams/jsonrpc2.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package streams
2+
3+
import (
4+
"log"
5+
"runtime/debug"
6+
7+
"github.com/fatih/color"
8+
"github.com/sourcegraph/jsonrpc2"
9+
)
10+
11+
var green = color.New(color.FgHiGreen)
12+
var red = color.New(color.FgHiRed)
13+
14+
// JSONRPCConnLogOnRecv perform logging of the given req and resp
15+
func JSONRPCConnLogOnRecv(prefix string) func(req *jsonrpc2.Request, resp *jsonrpc2.Response) {
16+
return func(req *jsonrpc2.Request, resp *jsonrpc2.Response) {
17+
jsonrpcLog(prefix, req, resp, false)
18+
}
19+
}
20+
21+
// JSONRPCConnLogOnSend perform logging of the given req and resp
22+
func JSONRPCConnLogOnSend(prefix string) func(req *jsonrpc2.Request, resp *jsonrpc2.Response) {
23+
return func(req *jsonrpc2.Request, resp *jsonrpc2.Response) {
24+
jsonrpcLog(prefix, req, resp, true)
25+
}
26+
}
27+
28+
func jsonrpcLog(prefix string, req *jsonrpc2.Request, resp *jsonrpc2.Response, sending bool) {
29+
color.NoColor = false
30+
var c *color.Color
31+
if sending {
32+
c = red
33+
} else {
34+
c = green
35+
}
36+
if resp != nil {
37+
if req != nil {
38+
log.Print(c.Sprintf(prefix+" ANSWER %s %v (%v)", req.Method, req.ID, resp.ID))
39+
} else {
40+
log.Print(c.Sprintf(prefix+" ANSWER UNBOUND (%v)", resp.ID))
41+
}
42+
} else if req != nil {
43+
if !req.Notif {
44+
log.Print(c.Sprintf(prefix+" REQUEST %s %v", req.Method, req.ID))
45+
} else {
46+
log.Print(c.Sprintf(prefix+" NOTIFICATION %s", req.Method))
47+
}
48+
} else {
49+
log.Print(green.Sprintf(prefix + " NULL MESSAGE"))
50+
log.Print(string(debug.Stack()))
51+
}
52+
}

0 commit comments

Comments
 (0)