Skip to content

Commit 306f195

Browse files
authored
Added --debug-file flag to gRPC daemon debug mode (#1704)
1 parent 0364ce3 commit 306f195

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

cli/daemon/daemon.go

+19
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
srv_debug "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/debug/v1"
3737
srv_monitor "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/monitor/v1"
3838
srv_settings "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/settings/v1"
39+
"github.com/arduino/go-paths-helper"
3940
"github.com/segmentio/stats/v4"
4041
"github.com/sirupsen/logrus"
4142
"github.com/spf13/cobra"
@@ -47,6 +48,7 @@ var (
4748
ip string
4849
daemonize bool
4950
debug bool
51+
debugFile string
5052
debugFilters []string
5153
)
5254

@@ -65,6 +67,7 @@ func NewCommand() *cobra.Command {
6567
configuration.Settings.BindPFlag("daemon.port", daemonCommand.PersistentFlags().Lookup("port"))
6668
daemonCommand.Flags().BoolVar(&daemonize, "daemonize", false, tr("Do not terminate daemon process if the parent process dies"))
6769
daemonCommand.Flags().BoolVar(&debug, "debug", false, tr("Enable debug logging of gRPC calls"))
70+
daemonCommand.Flags().StringVar(&debugFile, "debug-file", "", tr("Append debug logging to the specified file"))
6871
daemonCommand.Flags().StringSliceVar(&debugFilters, "debug-filter", []string{}, tr("Display only the provided gRPC calls"))
6972
return daemonCommand
7073
}
@@ -79,7 +82,23 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
7982
}
8083
port := configuration.Settings.GetString("daemon.port")
8184
gRPCOptions := []grpc.ServerOption{}
85+
if debugFile != "" {
86+
if !debug {
87+
feedback.Error(tr("The flag --debug-file must be used with --debug."))
88+
os.Exit(errorcodes.ErrBadArgument)
89+
}
90+
}
8291
if debug {
92+
if debugFile != "" {
93+
outFile := paths.New(debugFile)
94+
f, err := outFile.Append()
95+
if err != nil {
96+
feedback.Error(tr("Error opening debug logging file: %s", err))
97+
os.Exit(errorcodes.ErrBadCall)
98+
}
99+
debugStdOut = f
100+
defer f.Close()
101+
}
83102
gRPCOptions = append(gRPCOptions,
84103
grpc.UnaryInterceptor(unaryLoggerInterceptor),
85104
grpc.StreamInterceptor(streamLoggerInterceptor),

cli/daemon/interceptors.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,23 @@ import (
1919
"context"
2020
"encoding/json"
2121
"fmt"
22+
"os"
2223
"strings"
2324

2425
"google.golang.org/grpc"
2526
)
2627

28+
var debugStdOut = os.Stdout
29+
2730
func log(isRequest bool, msg interface{}) {
2831
j, _ := json.MarshalIndent(msg, "| ", " ")
2932
inOut := map[bool]string{true: "REQ: ", false: "RESP: "}
30-
fmt.Println("| " + inOut[isRequest] + string(j))
33+
fmt.Fprintln(debugStdOut, "| "+inOut[isRequest]+string(j))
3134
}
3235

3336
func logError(err error) {
3437
if err != nil {
35-
fmt.Println("| ERROR: ", err)
38+
fmt.Fprintln(debugStdOut, "| ERROR: ", err)
3639
}
3740
}
3841

@@ -52,12 +55,12 @@ func unaryLoggerInterceptor(ctx context.Context, req interface{}, info *grpc.Una
5255
if !logSelector(info.FullMethod) {
5356
return handler(ctx, req)
5457
}
55-
fmt.Println("CALLED:", info.FullMethod)
58+
fmt.Fprintln(debugStdOut, "CALLED:", info.FullMethod)
5659
log(true, req)
5760
resp, err := handler(ctx, req)
5861
logError(err)
5962
log(false, resp)
60-
fmt.Println()
63+
fmt.Fprintln(debugStdOut)
6164
return resp, err
6265
}
6366

@@ -72,11 +75,11 @@ func streamLoggerInterceptor(srv interface{}, stream grpc.ServerStream, info *gr
7275
if info.IsServerStream {
7376
streamReq += "STREAM_RESP"
7477
}
75-
fmt.Println("CALLED:", info.FullMethod, streamReq)
78+
fmt.Fprintln(debugStdOut, "CALLED:", info.FullMethod, streamReq)
7679
err := handler(srv, &loggingServerStream{ServerStream: stream})
7780
logError(err)
78-
fmt.Println("STREAM CLOSED")
79-
fmt.Println()
81+
fmt.Fprintln(debugStdOut, "STREAM CLOSED")
82+
fmt.Fprintln(debugStdOut)
8083
return err
8184
}
8285

0 commit comments

Comments
 (0)