diff --git a/cmd/audio-player/looper/circleci.go b/cmd/audio-player/looper/circleci.go index 3ccce301..2cee84c2 100644 --- a/cmd/audio-player/looper/circleci.go +++ b/cmd/audio-player/looper/circleci.go @@ -2,6 +2,8 @@ package main +import "bitbucket.org/ausocean/utils/logger" + const audioCmd = "" -func initCommand() {} +func initCommand(log *logger.Logger) {} diff --git a/cmd/audio-player/looper/main.go b/cmd/audio-player/looper/main.go index b32e71f5..55ab2835 100644 --- a/cmd/audio-player/looper/main.go +++ b/cmd/audio-player/looper/main.go @@ -29,40 +29,47 @@ package main import ( "bytes" "io" - "log" "os" "os/exec" "sync" + + "bitbucket.org/ausocean/utils/logger" + "gopkg.in/natefinch/lumberjack.v2" ) func main() { + // Constants. const soundFile = "/home/pi/48khz.wav" - const logFile = "audio.log" - // Making log file. - _, err := os.Stat(logFile) - if !os.IsNotExist(err) { - err := os.Remove(logFile) - if err != nil { - log.Fatalf("fatal: error clearing file: %v", err) - } + // Logging constants. + const ( + logPath = "/var/log/audiolooper/audiolooper.log" + logMaxSize = 500 // MB + logMaxBackup = 10 + logMaxAge = 28 // days + logVerbosity = logger.Debug + logSuppress = true + ) + + // Create lumberjack logger to handle logging to file. + fileLog := &lumberjack.Logger{ + Filename: logPath, + MaxSize: logMaxSize, + MaxBackups: logMaxBackup, + MaxAge: logMaxAge, } - f, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE, 0666) - if err != nil { - log.Fatalf("fatal: error opening file %s: %v", logFile, err) - } - defer f.Close() - log.SetOutput(f) + // Create logger that we call methods on to log. + log := logger.New(logVerbosity, fileLog, logSuppress) // Pi model specific initialisation - initCommand() + initCommand(log) // Infinite loop that outputs audio and gathers debug information. var numPlays int for { numPlays++ - log.Printf("debug: play number: %d\n", numPlays) + log.Log(logger.Debug, "playing audio", "play number", numPlays) cmd := exec.Command(audioCmd, soundFile) @@ -76,7 +83,7 @@ func main() { err := cmd.Start() if err != nil { - log.Fatalf("fatal: cmd.Start() for 'play' failed with '%s'\n", err) + log.Log(logger.Fatal, "cmd.Start() for 'play' failed", "error", err.Error()) } var wg sync.WaitGroup @@ -92,13 +99,13 @@ func main() { err = cmd.Wait() if err != nil { - log.Fatalf("fatal: cmd.Run() for 'play' failed with %s\n", err) + log.Log(logger.Fatal, "cmd.Run() for 'play' failed", "error", err.Error()) } if errStdout != nil || errStderr != nil { - log.Fatal("fatal: failed to capture stdout or stderr\n") + log.Log(logger.Fatal, "failed to capture stdout or stderr") } outStr, errStr := string(stdoutBuf.Bytes()), string(stderrBuf.Bytes()) - log.Printf("\nout:\n%s\nerr:\n%s\n____________________________________________________\n\n", outStr, errStr) + log.Log(logger.Debug, "stdout received", "stdout", outStr) + log.Log(logger.Debug, "stderr received", "stderr", errStr) } - } diff --git a/cmd/audio-player/looper/pi0.go b/cmd/audio-player/looper/pi0.go index 732ae90a..fb4b6988 100644 --- a/cmd/audio-player/looper/pi0.go +++ b/cmd/audio-player/looper/pi0.go @@ -3,23 +3,24 @@ package main import ( - "log" "os/exec" "time" + + "bitbucket.org/ausocean/utils/logger" ) const audioCmd = "play" -func initCommand() { +func initCommand(log *logger.Logger) { const soundcardPath = "/usr/share/doc/audioInjector/asound.state.RCA.thru.test" const sleepDur = 1 * time.Second // alsactl is a command that ensures the sound will be played through the correct soundcard. path, err := exec.LookPath("alsactl") if err != nil { - log.Fatalf("fatal: didn't find 'alsactl' executable\n") + log.Log(logger.Fatal, "didn't find 'alsactl' executable") } - log.Printf("debug: 'alsactl' executable is in '%s'\n", path) + log.Log(logger.Debug, "'alsactl' executable path", "path", path) for { cmdInit := exec.Command("alsactl", "-f", soundcardPath, "restore") @@ -27,14 +28,14 @@ func initCommand() { if err == nil { break } - log.Printf("fatal(ish): cmd.Run() for 'alsactl' failed with '%s'\n", err) + log.Log(logger.Warning, "cmd.Run() for 'alsactl' failed", "error", err.Error()) time.Sleep(sleepDur) } // Making sure that play command is on the pi. path, err = exec.LookPath(audioCmd) if err != nil { - log.Fatalf("fatal: didn't find 'play' executable\n") + log.Log(logger.Fatal, "didn't find 'play' executable") } - log.Printf("debug: 'play' executable is in '%s'\n", path) + log.Log(logger.Debug, "'play' executable path", "path", path) } diff --git a/cmd/audio-player/looper/pi3.go b/cmd/audio-player/looper/pi3.go index 0d970bb1..676bcab0 100644 --- a/cmd/audio-player/looper/pi3.go +++ b/cmd/audio-player/looper/pi3.go @@ -3,17 +3,18 @@ package main import ( - "log" "os/exec" + + "bitbucket.org/ausocean/utils/logger" ) const audioCmd = "omxplayer" -func initCommand() { +func initCommand(log *logger.Logger) { // Making sure that omxplayer command is on the pi. path, err := exec.LookPath("omxplayer") if err != nil { - log.Fatalf("fatal: didn't find 'omxplayer' executable\n") + log.Log(logger.Fatal, "didn't find 'omxplayer' executable") } - log.Printf("debug: 'omxplayer' executable is in '%s'\n", path) + log.Log(logger.Debug, "'omxplayer' executable path", "path", path) }