refactor(cli): rewrite log configuration

This commit is contained in:
Cyrille Nofficial 2022-01-03 18:03:17 +01:00
parent d191e7baf1
commit 4c8ef726fa
3 changed files with 7 additions and 10 deletions

View File

@ -28,20 +28,17 @@ func main() {
flag.StringVar(&tfSteeringTopic, "mqtt-topic-tf-steering", os.Getenv("MQTT_TOPIC_TF_STEERING"), "Mqtt topic that contains tenorflow steering value, use MQTT_TOPIC_TF_STEERING if args not set") flag.StringVar(&tfSteeringTopic, "mqtt-topic-tf-steering", os.Getenv("MQTT_TOPIC_TF_STEERING"), "Mqtt topic that contains tenorflow steering value, use MQTT_TOPIC_TF_STEERING if args not set")
flag.StringVar(&driveModeTopic, "mqtt-topic-drive-mode", os.Getenv("MQTT_TOPIC_DRIVE_MODE"), "Mqtt topic that contains DriveMode value, use MQTT_TOPIC_DRIVE_MODE if args not set") flag.StringVar(&driveModeTopic, "mqtt-topic-drive-mode", os.Getenv("MQTT_TOPIC_DRIVE_MODE"), "Mqtt topic that contains DriveMode value, use MQTT_TOPIC_DRIVE_MODE if args not set")
flag.BoolVar(&debug, "debug", false, "Display raw value to debug") logLevel := zap.LevelFlag("log", zap.InfoLevel, "log level")
flag.Parse() flag.Parse()
if len(os.Args) <= 1 { if len(os.Args) <= 1 {
flag.PrintDefaults() flag.PrintDefaults()
os.Exit(1) os.Exit(1)
} }
config := zap.NewDevelopmentConfig() config := zap.NewDevelopmentConfig()
if debug { config.Level = zap.NewAtomicLevelAt(*logLevel)
config.Level = zap.NewAtomicLevelAt(zap.DebugLevel)
} else {
config.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
}
lgr, err := config.Build() lgr, err := config.Build()
if err != nil { if err != nil {
log.Fatalf("unable to init logger: %v", err) log.Fatalf("unable to init logger: %v", err)
@ -53,6 +50,7 @@ func main() {
}() }()
zap.ReplaceGlobals(lgr) zap.ReplaceGlobals(lgr)
debug = logLevel.Enabled(zap.DebugLevel)
client, err := cli.Connect(mqttBroker, username, password, clientId) client, err := cli.Connect(mqttBroker, username, password, clientId)
if err != nil { if err != nil {
log.Fatalf("unable to connect to mqtt bus: %v", err) log.Fatalf("unable to connect to mqtt bus: %v", err)

View File

@ -17,7 +17,6 @@ func NewPart(client mqtt.Client, steeringTopic, driveModeTopic, rcSteeringTopic,
rcSteeringTopic: rcSteeringTopic, rcSteeringTopic: rcSteeringTopic,
tfSteeringTopic: tfSteeringTopic, tfSteeringTopic: tfSteeringTopic,
driveMode: events.DriveMode_USER, driveMode: events.DriveMode_USER,
debug: debug,
} }
} }
@ -72,7 +71,7 @@ func (p *SteeringPart) onRCSteering(_ mqtt.Client, message mqtt.Message) {
err := proto.Unmarshal(message.Payload(), &evt) err := proto.Unmarshal(message.Payload(), &evt)
if err != nil { if err != nil {
zap.S().Debugf("unable to unmarshal rc event: %v", err) zap.S().Debugf("unable to unmarshal rc event: %v", err)
}else { } else {
zap.S().Debugf("receive steering message from radio command: %0.00f", evt.GetSteering()) zap.S().Debugf("receive steering message from radio command: %0.00f", evt.GetSteering())
} }
} }
@ -90,7 +89,7 @@ func (p *SteeringPart) onTFSteering(_ mqtt.Client, message mqtt.Message) {
err := proto.Unmarshal(message.Payload(), &evt) err := proto.Unmarshal(message.Payload(), &evt)
if err != nil { if err != nil {
zap.S().Debugf("unable to unmarshal tensorflow event: %v", err) zap.S().Debugf("unable to unmarshal tensorflow event: %v", err)
}else { } else {
zap.S().Debugf("receive steering message from tensorflow: %0.00f", evt.GetSteering()) zap.S().Debugf("receive steering message from tensorflow: %0.00f", evt.GetSteering())
} }
} }

View File

@ -34,7 +34,7 @@ func TestDefaultSteering(t *testing.T) {
rcSteeringTopic := "topic/rcSteering" rcSteeringTopic := "topic/rcSteering"
tfSteeringTopic := "topic/tfSteering" tfSteeringTopic := "topic/tfSteering"
p := NewPart(nil, steeringTopic, driveModeTopic, rcSteeringTopic, tfSteeringTopic) p := NewPart(nil, steeringTopic, driveModeTopic, rcSteeringTopic, tfSteeringTopic, true)
cases := []struct { cases := []struct {
driveMode events.DriveModeMessage driveMode events.DriveModeMessage