Skip to content

Added --debug-file flag to gRPC daemon debug mode #1704

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 1 commit into from
Apr 4, 2022
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
19 changes: 19 additions & 0 deletions cli/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
srv_debug "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/debug/v1"
srv_monitor "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/monitor/v1"
srv_settings "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/settings/v1"
"github.com/arduino/go-paths-helper"
"github.com/segmentio/stats/v4"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -47,6 +48,7 @@ var (
ip string
daemonize bool
debug bool
debugFile string
debugFilters []string
)

Expand All @@ -65,6 +67,7 @@ func NewCommand() *cobra.Command {
configuration.Settings.BindPFlag("daemon.port", daemonCommand.PersistentFlags().Lookup("port"))
daemonCommand.Flags().BoolVar(&daemonize, "daemonize", false, tr("Do not terminate daemon process if the parent process dies"))
daemonCommand.Flags().BoolVar(&debug, "debug", false, tr("Enable debug logging of gRPC calls"))
daemonCommand.Flags().StringVar(&debugFile, "debug-file", "", tr("Append debug logging to the specified file"))
daemonCommand.Flags().StringSliceVar(&debugFilters, "debug-filter", []string{}, tr("Display only the provided gRPC calls"))
return daemonCommand
}
Expand All @@ -79,7 +82,23 @@ func runDaemonCommand(cmd *cobra.Command, args []string) {
}
port := configuration.Settings.GetString("daemon.port")
gRPCOptions := []grpc.ServerOption{}
if debugFile != "" {
if !debug {
feedback.Error(tr("The flag --debug-file must be used with --debug."))
os.Exit(errorcodes.ErrBadArgument)
}
}
if debug {
if debugFile != "" {
outFile := paths.New(debugFile)
f, err := outFile.Append()
if err != nil {
feedback.Error(tr("Error opening debug logging file: %s", err))
os.Exit(errorcodes.ErrBadCall)
}
debugStdOut = f
defer f.Close()
}
gRPCOptions = append(gRPCOptions,
grpc.UnaryInterceptor(unaryLoggerInterceptor),
grpc.StreamInterceptor(streamLoggerInterceptor),
Expand Down
17 changes: 10 additions & 7 deletions cli/daemon/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,23 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"strings"

"google.golang.org/grpc"
)

var debugStdOut = os.Stdout

func log(isRequest bool, msg interface{}) {
j, _ := json.MarshalIndent(msg, "| ", " ")
inOut := map[bool]string{true: "REQ: ", false: "RESP: "}
fmt.Println("| " + inOut[isRequest] + string(j))
fmt.Fprintln(debugStdOut, "| "+inOut[isRequest]+string(j))
}

func logError(err error) {
if err != nil {
fmt.Println("| ERROR: ", err)
fmt.Fprintln(debugStdOut, "| ERROR: ", err)
}
}

Expand All @@ -52,12 +55,12 @@ func unaryLoggerInterceptor(ctx context.Context, req interface{}, info *grpc.Una
if !logSelector(info.FullMethod) {
return handler(ctx, req)
}
fmt.Println("CALLED:", info.FullMethod)
fmt.Fprintln(debugStdOut, "CALLED:", info.FullMethod)
log(true, req)
resp, err := handler(ctx, req)
logError(err)
log(false, resp)
fmt.Println()
fmt.Fprintln(debugStdOut)
return resp, err
}

Expand All @@ -72,11 +75,11 @@ func streamLoggerInterceptor(srv interface{}, stream grpc.ServerStream, info *gr
if info.IsServerStream {
streamReq += "STREAM_RESP"
}
fmt.Println("CALLED:", info.FullMethod, streamReq)
fmt.Fprintln(debugStdOut, "CALLED:", info.FullMethod, streamReq)
err := handler(srv, &loggingServerStream{ServerStream: stream})
logError(err)
fmt.Println("STREAM CLOSED")
fmt.Println()
fmt.Fprintln(debugStdOut, "STREAM CLOSED")
fmt.Fprintln(debugStdOut)
return err
}

Expand Down