Skip to content

Improved and colorful logging #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.12
require (
github.com/arduino/arduino-cli v0.0.0-20201215104024-6a177ebf56f2
github.com/arduino/go-paths-helper v1.5.0
github.com/fatih/color v1.7.0
github.com/pkg/errors v0.9.1
github.com/sourcegraph/jsonrpc2 v0.0.0-20200429184054-15c2290dcb37
github.com/stretchr/testify v1.6.1
Expand Down
1 change: 1 addition & 0 deletions handler/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func (handler *InoHandler) rebuildEnvironmentLoop() {
// Regenerate preprocessed sketch!
done := make(chan bool)
go func() {
defer streams.CatchAndLogPanic()

handler.progressHandler.Create("arduinoLanguageServerRebuild")
handler.progressHandler.Begin("arduinoLanguageServerRebuild", &lsp.WorkDoneProgressBegin{
Expand Down
14 changes: 12 additions & 2 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ func NewInoHandler(stdio io.ReadWriteCloser, board lsp.Board) *InoHandler {
handler.clangdStarted = sync.NewCond(&handler.dataMux)
stdStream := jsonrpc2.NewBufferedStream(stdio, jsonrpc2.VSCodeObjectCodec{})
var stdHandler jsonrpc2.Handler = jsonrpc2.HandlerWithError(handler.HandleMessageFromIDE)
handler.StdioConn = jsonrpc2.NewConn(context.Background(), stdStream, stdHandler)
handler.StdioConn = jsonrpc2.NewConn(context.Background(), stdStream, stdHandler,
jsonrpc2.OnRecv(streams.JSONRPCConnLogOnRecv("IDE --> LS CL:")),
jsonrpc2.OnSend(streams.JSONRPCConnLogOnSend("IDE <-- LS CL:")),
)

handler.progressHandler = NewProgressProxy(handler.StdioConn)

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

go func() {
defer streams.CatchAndLogPanic()

// Start clangd asynchronously
log.Printf("LS --- initializing workbench (queued)")
handler.dataMux.Lock()
Expand Down Expand Up @@ -514,7 +519,9 @@ func (handler *InoHandler) initializeWorkbench(ctx context.Context, params *lsp.

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

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

if r.DocumentSymbolArray != nil {
// Treat the input as []DocumentSymbol
log.Printf(" <-- documentSymbol(%d document symbols)", len(*r.DocumentSymbolArray))
return handler.cpp2inoDocumentSymbols(*r.DocumentSymbolArray, inoURI)
} else if r.SymbolInformationArray != nil {
// Treat the input as []SymbolInformation
log.Printf(" <-- documentSymbol(%d symbol information)", len(*r.SymbolInformationArray))
return handler.cpp2inoSymbolInformation(*r.SymbolInformationArray)
} else {
// Treat the input as null
log.Printf(" <-- null documentSymbol")
}

case *[]lsp.CommandOrCodeAction:
Expand Down
52 changes: 52 additions & 0 deletions streams/jsonrpc2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package streams

import (
"log"
"runtime/debug"

"github.com/fatih/color"
"github.com/sourcegraph/jsonrpc2"
)

var green = color.New(color.FgHiGreen)
var red = color.New(color.FgHiRed)

// JSONRPCConnLogOnRecv perform logging of the given req and resp
func JSONRPCConnLogOnRecv(prefix string) func(req *jsonrpc2.Request, resp *jsonrpc2.Response) {
return func(req *jsonrpc2.Request, resp *jsonrpc2.Response) {
jsonrpcLog(prefix, req, resp, false)
}
}

// JSONRPCConnLogOnSend perform logging of the given req and resp
func JSONRPCConnLogOnSend(prefix string) func(req *jsonrpc2.Request, resp *jsonrpc2.Response) {
return func(req *jsonrpc2.Request, resp *jsonrpc2.Response) {
jsonrpcLog(prefix, req, resp, true)
}
}

func jsonrpcLog(prefix string, req *jsonrpc2.Request, resp *jsonrpc2.Response, sending bool) {
color.NoColor = false
var c *color.Color
if sending {
c = red
} else {
c = green
}
if resp != nil {
if req != nil {
log.Print(c.Sprintf(prefix+" ANSWER %s %v (%v)", req.Method, req.ID, resp.ID))
} else {
log.Print(c.Sprintf(prefix+" ANSWER UNBOUND (%v)", resp.ID))
}
} else if req != nil {
if !req.Notif {
log.Print(c.Sprintf(prefix+" REQUEST %s %v", req.Method, req.ID))
} else {
log.Print(c.Sprintf(prefix+" NOTIFICATION %s", req.Method))
}
} else {
log.Print(green.Sprintf(prefix + " NULL MESSAGE"))
log.Print(string(debug.Stack()))
}
}