From 969bbdc6d1a4e7b384f79c926a39ce1cdd8ce3f3 Mon Sep 17 00:00:00 2001 From: AntoineH Date: Thu, 5 Sep 2024 10:19:31 +0200 Subject: [PATCH] Log motor state with logrus --- go.mod | 1 + go.sum | 4 ++++ src/cmd/motor/serve.go | 31 +++++++++++++++++++++++++++---- 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 5fda1b5..03610f2 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( github.com/pelletier/go-toml/v2 v2.2.2 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect + github.com/sirupsen/logrus v1.9.3 // indirect github.com/sourcegraph/conc v0.3.0 // indirect github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.6.0 // indirect diff --git a/go.sum b/go.sum index 8e3b6ae..eca92f9 100644 --- a/go.sum +++ b/go.sum @@ -23,6 +23,8 @@ github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6ke github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= @@ -40,6 +42,7 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= @@ -56,6 +59,7 @@ golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= golang.org/x/net v0.26.0 h1:soB7SVo0PWrY4vPW/+ay0jKDNScG2X9wFeYlXIvJsOQ= golang.org/x/net v0.26.0/go.mod h1:5YKkiSynbBIh3p6iOc/vibscux0x38BZDkn8sCUPxHE= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= diff --git a/src/cmd/motor/serve.go b/src/cmd/motor/serve.go index b06ef85..bc049b8 100644 --- a/src/cmd/motor/serve.go +++ b/src/cmd/motor/serve.go @@ -9,6 +9,7 @@ import ( "time" "github.com/spf13/cobra" + "github.com/sirupsen/logrus" "context" "log" @@ -74,6 +75,7 @@ func (s *server) SetVelocity(ctx context.Context, in *pb.Velocity) (*pb.Empty, e func (s *server) GetData(ctx context.Context, in *pb.Empty) (*pb.MotorData, error) { var state State select { + //TODO: Risk of empty buffer due to logging ? case state = <-stateChan : return &pb.MotorData{Angle: state.Angle, Velocity: state.Velocity, Error: state.Error}, nil default: @@ -103,11 +105,12 @@ func init_sim(){ state.Velocity = 0 state.Error = "" - //Channels + //Channels (Beware, blocking behavior might occur in case of channel buffer full due to high load) stateChan = make(chan State, 1) // State channel of the simulation cmdVelChan = make(chan float64, 1)// Command channel of the simulation - go motor_sim(state, curr_config, 1) //Run sim at 10Hz + go motor_sim(state, curr_config, 10) //Run sim at 10Hz + go state_log(1) //Log every seconds //close(cmdVelChan) //Stop sim } @@ -156,9 +159,29 @@ func motor_sim(state_init State, config Config, sim_freq float64){ break; default: //No message sent (buffer full/no receiver) } - - log.Printf("Motor state: %+v", state) //Wait for next iteration time.Sleep(time.Duration(float64(time.Second)/sim_freq)) } +} + +func state_log(log_freq float64){ + //Logging coroutine + var state State + for{ + select { + case state = <-stateChan : + //Log state + // log.Printf("Motor state: %+v", state) + logrus.WithFields(logrus.Fields{ + "Angle (°)": state.Angle, + "Velocity (°/s)": state.Velocity, + "Error": state.Error, + }).Info("Motor state: ") + default: + log.Printf("No state:") + //No message received (buffer empty), do nothing + } + //Wait for next iteration + time.Sleep(time.Duration(float64(time.Second)/log_freq)) + } } \ No newline at end of file