Skip to content

Commit fc59a56

Browse files
committed
Added flags to enable logging of gRPC calls
1 parent 7d7ade9 commit fc59a56

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

cli/daemon/daemon.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ func NewCommand() *cobra.Command {
5656
cmd.PersistentFlags().String("port", "", tr("The TCP port the daemon will listen to"))
5757
configuration.Settings.BindPFlag("daemon.port", cmd.PersistentFlags().Lookup("port"))
5858
cmd.Flags().BoolVar(&daemonize, "daemonize", false, tr("Do not terminate daemon process if the parent process dies"))
59+
cmd.Flags().BoolVar(&debug, "debug", false, tr("Enable debug logging of gRPC calls"))
5960
return cmd
6061
}
6162

6263
var daemonize bool
64+
var debug bool
6365

6466
func runDaemonCommand(cmd *cobra.Command, args []string) {
6567

@@ -69,8 +71,15 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
6971
defer stats.Flush()
7072
}
7173
port := configuration.Settings.GetString("daemon.port")
72-
s := grpc.NewServer()
73-
74+
var s *grpc.Server
75+
if debug {
76+
s = grpc.NewServer(
77+
grpc.UnaryInterceptor(unaryLoggerInterceptor),
78+
grpc.StreamInterceptor(streamLoggerInterceptor),
79+
)
80+
} else {
81+
s = grpc.NewServer()
82+
}
7483
// Set specific user-agent for the daemon
7584
configuration.Settings.Set("network.user_agent_ext", "daemon")
7685

cli/daemon/interceptors.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to license@arduino.cc.
15+
16+
package daemon
17+
18+
import (
19+
"context"
20+
"encoding/json"
21+
"fmt"
22+
23+
"google.golang.org/grpc"
24+
)
25+
26+
func log(isRequest bool, msg interface{}) {
27+
j, _ := json.MarshalIndent(msg, "| ", " ")
28+
inOut := map[bool]string{true: "REQ: ", false: "RESP: "}
29+
fmt.Println("| " + inOut[isRequest] + string(j))
30+
}
31+
32+
func logError(err error) {
33+
if err != nil {
34+
fmt.Println("| ERROR: ", err)
35+
}
36+
}
37+
38+
func unaryLoggerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
39+
fmt.Println("CALLED:", info.FullMethod)
40+
log(true, req)
41+
resp, err := handler(ctx, req)
42+
logError(err)
43+
log(false, resp)
44+
fmt.Println()
45+
return resp, err
46+
}
47+
48+
func streamLoggerInterceptor(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
49+
streamReq := ""
50+
if info.IsClientStream {
51+
streamReq = "STREAM_REQ "
52+
}
53+
if info.IsServerStream {
54+
streamReq += "STREAM_RESP"
55+
}
56+
fmt.Println("CALLED:", info.FullMethod, streamReq)
57+
err := handler(srv, &loggingServerStream{ServerStream: stream})
58+
logError(err)
59+
fmt.Println()
60+
return err
61+
}
62+
63+
type loggingServerStream struct {
64+
grpc.ServerStream
65+
}
66+
67+
func (l *loggingServerStream) RecvMsg(m interface{}) error {
68+
err := l.ServerStream.RecvMsg(m)
69+
logError(err)
70+
log(true, m)
71+
return err
72+
}
73+
74+
func (l *loggingServerStream) SendMsg(m interface{}) error {
75+
err := l.ServerStream.SendMsg(m)
76+
logError(err)
77+
log(false, m)
78+
return err
79+
}

0 commit comments

Comments
 (0)