From 0001c3e36eae1e6d6b5342ecf2e3d4e07675312b Mon Sep 17 00:00:00 2001 From: Cyrille Nofficial Date: Tue, 12 Oct 2021 17:34:47 +0200 Subject: [PATCH] fix: log configuration --- cmd/rc-steering/rc-steering.go | 24 +++++++++++++++++++----- pkg/steering/steering.go | 20 +++++++++++++++----- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/cmd/rc-steering/rc-steering.go b/cmd/rc-steering/rc-steering.go index 8ecd9d1..9af477d 100644 --- a/cmd/rc-steering/rc-steering.go +++ b/cmd/rc-steering/rc-steering.go @@ -18,12 +18,8 @@ func main() { var cameraTopic, steeringTopic string var modelPath string var edgeVerbosity int + var debug bool - lgr, err := zap.NewProductionConfig().Build() - if err != nil { - log.Fatalf("unable to init logger: %v", err) - } - zap.ReplaceGlobals(lgr) mqttQos := cli.InitIntFlag("MQTT_QOS", 0) _, mqttRetain := os.LookupEnv("MQTT_RETAIN") @@ -34,6 +30,7 @@ func main() { flag.StringVar(&steeringTopic, "mqtt-topic-road", os.Getenv("MQTT_TOPIC_STEERING"), "Mqtt topic to publish road detection result, use MQTT_TOPIC_STEERING if args not set") flag.StringVar(&cameraTopic, "mqtt-topic-camera", os.Getenv("MQTT_TOPIC_CAMERA"), "Mqtt topic that contains camera frame values, use MQTT_TOPIC_CAMERA if args not set") flag.IntVar(&edgeVerbosity, "edge-verbosity", 0, "Edge TPU Verbosity") + flag.BoolVar(&debug, "debug", false, "Display debug logs") flag.Parse() if len(os.Args) <= 1 { @@ -41,6 +38,23 @@ func main() { os.Exit(1) } + config := zap.NewDevelopmentConfig() + if debug { + config.Level = zap.NewAtomicLevelAt(zap.DebugLevel) + } else { + config.Level = zap.NewAtomicLevelAt(zap.InfoLevel) + } + lgr, err := config.Build() + if err != nil { + log.Fatalf("unable to init logger: %v", err) + } + defer func() { + if err := lgr.Sync(); err != nil { + log.Printf("unable to Sync logger: %v\n", err) + } + }() + zap.ReplaceGlobals(lgr) + if modelPath == "" { zap.L().Error("model path is mandatory") flag.PrintDefaults() diff --git a/pkg/steering/steering.go b/pkg/steering/steering.go index 7424bfa..51840f8 100644 --- a/pkg/steering/steering.go +++ b/pkg/steering/steering.go @@ -61,17 +61,22 @@ func (p *Part) Start() error { if err != nil { return fmt.Errorf("cannot get EdgeTPU version: %w", err) } - zap.S().Infof("EdgeTPU Version: %s\n", edgetpuVersion) + zap.S().Infof("EdgeTPU Version: %s", edgetpuVersion) edgetpu.Verbosity(p.edgeVebosity) p.options = tflite.NewInterpreterOptions() p.options.SetNumThread(4) p.options.SetErrorReporter(func(msg string, userData interface{}) { - zap.L().Error(msg, - zap.Any("userData", userData), + zap.S().Errorw(msg, + "userData", userData, ) }, nil) + zap.S().Infof("find %d edgetpu devices", len(devices)) + zap.S().Infow("configure edgetpu", + "path", devices[0].Path, + "type", uint32(devices[0].Type), + ) // Add the first EdgeTPU device d := edgetpu.New(devices[0]) if d == nil { @@ -85,7 +90,7 @@ func (p *Part) Start() error { } if err := registerCallbacks(p); err != nil { - zap.S().Errorf("unable to register callbacks: %v", err) + zap.S().Errorw("unable to register callbacks", "error", err) return err } @@ -119,7 +124,8 @@ func (p *Part) onFrame(_ mqtt.Client, message mqtt.Message) { img, _, err := image.Decode(bytes.NewReader(msg.GetFrame())) if err != nil { - zap.L().Error("unable to decode frame", zap.Error(err)) + zap.L().Error("unable to decode frame, skip frame", zap.Error(err)) + return } steering, confidence, err := p.Value(img) @@ -130,6 +136,10 @@ func (p *Part) onFrame(_ mqtt.Client, message mqtt.Message) { ) return } + zap.L().Debug("new steering value", + zap.Float32("steering", steering), + zap.Float32("confidence", confidence), + ) msgSteering := &events.SteeringMessage{ Steering: steering, Confidence: confidence,