From d14766d5124080cc782ba31527c94760ee669a4e Mon Sep 17 00:00:00 2001 From: Trek H Date: Mon, 15 Feb 2021 11:03:37 +1030 Subject: [PATCH] treatment: use levelled logging functions --- cmd/treatment/main.go | 87 +++++++++++++++++++++---------------------- 1 file changed, 43 insertions(+), 44 deletions(-) diff --git a/cmd/treatment/main.go b/cmd/treatment/main.go index eb8a7e67..b59bbc5c 100644 --- a/cmd/treatment/main.go +++ b/cmd/treatment/main.go @@ -83,7 +83,7 @@ const ( // Variable map to send to netreceiver/vidgrind. var varMap = map[string]string{ "mode": "enum:Paused,Play,Check", - "audioFilePath": "string", + "AudioFilePath": "string", } func main() { @@ -109,25 +109,25 @@ func main() { log := logger.New(logVerbosity, io.MultiWriter(fileLog, netLog), logSuppress) if *filePtr == "" { - log.Log(logger.Fatal, "no file path provided, check usage") + log.Fatal("no file path provided, check usage") } // The netsender client will handle communication with netreceiver and GPIO stuff. - log.Log(logger.Debug, "initialising netsender client") + log.Debug("initialising netsender client") ns, err := netsender.New(log, gpio.InitPin, nil, gpio.WritePin, varMap) if err != nil { - log.Log(logger.Fatal, "could not initialise netsender client", "error", err.Error()) + log.Fatal("could not initialise netsender client", "error", err.Error()) } // Revid will handle the recording and sending of audio for sound checking. - log.Log(logger.Debug, "initialising revid") + log.Debug("initialising revid") rv, err := revid.New(config.Config{Logger: log}, ns) if err != nil { - log.Log(logger.Fatal, "could not initialise revid", "error", err.Error()) + log.Fatal("could not initialise revid", "error", err.Error()) } // Start the control loop. - log.Log(logger.Debug, "starting control loop") + log.Debug("starting control loop") run(rv, ns, filePtr, log, netLog) } @@ -142,65 +142,64 @@ func run(rv *revid.Revid, ns *netsender.Sender, file *string, l *logger.Logger, ) for { - l.Log(logger.Debug, "running netsender") + l.Debug("running netsender") err := ns.Run() if err != nil { - l.Log(logger.Warning, "run failed. Retrying...", "error", err.Error()) + l.Warning("run failed. Retrying...", "error", err.Error()) time.Sleep(netSendRetryTime) continue } - l.Log(logger.Debug, "sending logs") + l.Debug("sending logs") err = nl.Send(ns) if err != nil { - l.Log(logger.Warning, pkg+"Logs could not be sent", "error", err.Error()) + l.Warning(pkg+"Logs could not be sent", "error", err.Error()) } - l.Log(logger.Debug, "checking varsum") + l.Debug("checking varsum") newVs := ns.VarSum() if vs == newVs { sleep(ns, l) continue } vs = newVs - l.Log(logger.Info, "varsum changed", "vs", vs) + l.Info("varsum changed", "vs", vs) - l.Log(logger.Debug, "getting new vars") + l.Debug("getting new vars") vars, err := ns.Vars() if err != nil { - l.Log(logger.Error, pkg+"netSender failed to get vars", "error", err.Error()) + l.Error(pkg+"netSender failed to get vars", "error", err.Error()) time.Sleep(netSendRetryTime) continue } - l.Log(logger.Info, "got new vars", "vars", vars) + l.Info("got new vars", "vars", vars) // Configure revid based on the vars. - l.Log(logger.Debug, "updating revid configuration") + l.Debug("updating revid configuration") err = rv.Update(vars) if err != nil { - l.Log(logger.Warning, pkg+"couldn't update revid", "error", err.Error()) + l.Warning(pkg+"couldn't update revid", "error", err.Error()) sleep(ns, l) continue } - l.Log(logger.Info, "revid successfully reconfigured") + l.Info("revid successfully reconfigured") - l.Log(logger.Debug, "checking mode") + l.Debug("checking mode") switch ns.Mode() { case modePaused: stopAudio(&wg, &treating, audioQuit) - l.Log(logger.Info, "mode is Paused, stopping revid") + l.Info("mode is Paused, stopping revid") rv.Stop() case modeTreatment: - l.Log(logger.Debug, "checking audio file path") - f := vars["audioFilePath"] + l.Debug("checking audio file path") + f := vars["AudioFilePath"] if f != "" && *file != f { file = &f - l.Log(logger.Info, "updated audio file path", "audioFilePath", f) - l.Log(logger.Info, "stopping audio") + l.Info("updated audio file path, stopping audio", "AudioFilePath", f) stopAudio(&wg, &treating, audioQuit) } if !treating { - l.Log(logger.Info, "starting audio treatment") + l.Info("starting audio treatment") rv.Stop() audioQuit = make(chan struct{}) treating = true @@ -209,19 +208,19 @@ func run(rv *revid.Revid, ns *netsender.Sender, file *string, l *logger.Logger, } case modeCheck: stopAudio(&wg, &treating, audioQuit) - l.Log(logger.Info, "sound checking") + l.Info("sound checking") err = rv.Start() if err != nil { - l.Log(logger.Error, "could not start revid", "error", err.Error()) + l.Error("could not start revid", "error", err.Error()) ns.SetMode(modePaused, &vs) sleep(ns, l) continue } default: - l.Log(logger.Warning, "mode is not valid", "mode", ns.Mode()) + l.Warning("mode is not valid", "mode", ns.Mode()) } - l.Log(logger.Info, "revid updated with new mode") + l.Info("revid updated with new mode") sleep(ns, l) } } @@ -237,28 +236,28 @@ func playAudio(file *string, quit chan struct{}, wg *sync.WaitGroup, l *logger.L // stdout and stderr. outPipe, err := cmd.StdoutPipe() if err != nil { - l.Log(logger.Error, "failed to pipe stdout", "error", err) + l.Error("failed to pipe stdout", "error", err) } errPipe, err := cmd.StderrPipe() if err != nil { - l.Log(logger.Error, "failed to pipe stderr", "error", err) + l.Error("failed to pipe stderr", "error", err) } // Start playback of the audio file. err = cmd.Start() if err != nil { - l.Log(logger.Error, "start failed", "error", err.Error()) + l.Error("start failed", "error", err.Error()) continue } numPlays++ - l.Log(logger.Debug, "playing audio", "numPlays", numPlays) + l.Debug("playing audio", "numPlays", numPlays) // Copy any std out to a buffer for logging. var outBuff bytes.Buffer go func() { _, err = io.Copy(&outBuff, outPipe) if err != nil { - l.Log(logger.Error, "failed to copy out pipe", "error", err) + l.Error("failed to copy out pipe", "error", err) } }() @@ -267,20 +266,20 @@ func playAudio(file *string, quit chan struct{}, wg *sync.WaitGroup, l *logger.L go func() { _, err = io.Copy(&errBuff, errPipe) if err != nil { - l.Log(logger.Error, "failed to copy error pipe", "error", err) + l.Error("failed to copy error pipe", "error", err) } }() // Wait for playback to complete. err = cmd.Wait() if err != nil { - l.Log(logger.Error, "failed to wait for execution finish", "error", err.Error()) + l.Error("failed to wait for execution finish", "error", err.Error()) } - l.Log(logger.Debug, "stdout received", "stdout", string(outBuff.Bytes())) + l.Debug("stdout received", "stdout", string(outBuff.Bytes())) // If there was any errors on stderr, log them. if errBuff.Len() != 0 { - l.Log(logger.Error, "errors from stderr", "stderr", string(errBuff.Bytes())) + l.Error("errors from stderr", "stderr", string(errBuff.Bytes())) } // Check for audio signal halt. @@ -309,14 +308,14 @@ func stopAudio(wg *sync.WaitGroup, treating *bool, signal chan struct{}) { // sleep uses a delay to halt the program based on the monitoring period // netsender parameter (mp) defined in the netsender.conf config. func sleep(ns *netsender.Sender, l *logger.Logger) { - l.Log(logger.Debug, "sleeping") + l.Debug("sleeping") t, err := strconv.Atoi(ns.Param("mp")) if err != nil { - l.Log(logger.Error, pkg+"could not get sleep time, using default", "error", err) + l.Error(pkg+"could not get sleep time, using default", "error", err) t = defaultSleepTime } time.Sleep(time.Duration(t) * time.Second) - l.Log(logger.Debug, "finished sleeping") + l.Debug("finished sleeping") } // checkPath wraps the use of lookPath to check the existence of executables @@ -324,7 +323,7 @@ func sleep(ns *netsender.Sender, l *logger.Logger) { func checkPath(cmd string, l *logger.Logger) { path, err := exec.LookPath(cmd) if err != nil { - l.Log(logger.Fatal, fmt.Sprintf("couldn't find %s", cmd), "error", err) + l.Fatal(fmt.Sprintf("couldn't find %s", cmd), "error", err) } - l.Log(logger.Debug, fmt.Sprintf("found %s", cmd), "path", path) + l.Debug(fmt.Sprintf("found %s", cmd), "path", path) }