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
import "bitbucket.org/ausocean/utils/logger"
const audioCmd = ""
func initCommand() {}
func initCommand(log *logger.Logger) {}

View File

@ -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)
}
}

View File

@ -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)
}

View File

@ -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)
}