cmd/audio-player/looper: use same logger as revid

This commit is contained in:
Scott 2020-02-28 14:30:55 +10:30
parent 89691e9689
commit 17c7ef857e
4 changed files with 45 additions and 34 deletions

View File

@ -2,6 +2,8 @@
package main package main
import "bitbucket.org/ausocean/utils/logger"
const audioCmd = "" const audioCmd = ""
func initCommand() {} func initCommand(log *logger.Logger) {}

View File

@ -29,40 +29,47 @@ package main
import ( import (
"bytes" "bytes"
"io" "io"
"log"
"os" "os"
"os/exec" "os/exec"
"sync" "sync"
"bitbucket.org/ausocean/utils/logger"
"gopkg.in/natefinch/lumberjack.v2"
) )
func main() { func main() {
// Constants.
const soundFile = "/home/pi/48khz.wav" const soundFile = "/home/pi/48khz.wav"
const logFile = "audio.log"
// Making log file. // Logging constants.
_, err := os.Stat(logFile) const (
if !os.IsNotExist(err) { logPath = "/var/log/audiolooper/audiolooper.log"
err := os.Remove(logFile) logMaxSize = 500 // MB
if err != nil { logMaxBackup = 10
log.Fatalf("fatal: error clearing file: %v", err) 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) // Create logger that we call methods on to log.
if err != nil { log := logger.New(logVerbosity, fileLog, logSuppress)
log.Fatalf("fatal: error opening file %s: %v", logFile, err)
}
defer f.Close()
log.SetOutput(f)
// Pi model specific initialisation // Pi model specific initialisation
initCommand() initCommand(log)
// Infinite loop that outputs audio and gathers debug information. // Infinite loop that outputs audio and gathers debug information.
var numPlays int var numPlays int
for { for {
numPlays++ numPlays++
log.Printf("debug: play number: %d\n", numPlays) log.Log(logger.Debug, "playing audio", "play number", numPlays)
cmd := exec.Command(audioCmd, soundFile) cmd := exec.Command(audioCmd, soundFile)
@ -76,7 +83,7 @@ func main() {
err := cmd.Start() err := cmd.Start()
if err != nil { 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 var wg sync.WaitGroup
@ -92,13 +99,13 @@ func main() {
err = cmd.Wait() err = cmd.Wait()
if err != nil { 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 { 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()) 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)
} }
} }

View File

@ -3,23 +3,24 @@
package main package main
import ( import (
"log"
"os/exec" "os/exec"
"time" "time"
"bitbucket.org/ausocean/utils/logger"
) )
const audioCmd = "play" const audioCmd = "play"
func initCommand() { func initCommand(log *logger.Logger) {
const soundcardPath = "/usr/share/doc/audioInjector/asound.state.RCA.thru.test" const soundcardPath = "/usr/share/doc/audioInjector/asound.state.RCA.thru.test"
const sleepDur = 1 * time.Second const sleepDur = 1 * time.Second
// alsactl is a command that ensures the sound will be played through the correct soundcard. // alsactl is a command that ensures the sound will be played through the correct soundcard.
path, err := exec.LookPath("alsactl") path, err := exec.LookPath("alsactl")
if err != nil { 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 { for {
cmdInit := exec.Command("alsactl", "-f", soundcardPath, "restore") cmdInit := exec.Command("alsactl", "-f", soundcardPath, "restore")
@ -27,14 +28,14 @@ func initCommand() {
if err == nil { if err == nil {
break 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) time.Sleep(sleepDur)
} }
// Making sure that play command is on the pi. // Making sure that play command is on the pi.
path, err = exec.LookPath(audioCmd) path, err = exec.LookPath(audioCmd)
if err != nil { 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)
} }

View File

@ -3,17 +3,18 @@
package main package main
import ( import (
"log"
"os/exec" "os/exec"
"bitbucket.org/ausocean/utils/logger"
) )
const audioCmd = "omxplayer" const audioCmd = "omxplayer"
func initCommand() { func initCommand(log *logger.Logger) {
// Making sure that omxplayer command is on the pi. // Making sure that omxplayer command is on the pi.
path, err := exec.LookPath("omxplayer") path, err := exec.LookPath("omxplayer")
if err != nil { 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)
} }