cmd/audio-player/looper: general clean up of main file

No longer doing multiwriter that stdout and stderr to stdout and stderr again and buffs. Now only buffs so that we can log anything in stdout or stderr.
This commit is contained in:
Saxon 2020-03-06 17:18:32 +10:30
parent 170a18830e
commit bb22272804
1 changed files with 46 additions and 51 deletions

View File

@ -32,28 +32,25 @@ import (
"flag"
"fmt"
"io"
"os"
"os/exec"
"sync"
"bitbucket.org/ausocean/utils/logger"
"gopkg.in/natefinch/lumberjack.v2"
)
func main() {
// Command line flags.
var soundFilePtr = flag.String("path", "", "Location of sound file")
flag.Parse()
// Logging related constants.
const (
logPath = "/var/log/audiolooper/audiolooper.log"
logMaxSize = 500 // MB
logMaxBackup = 10
logMaxAge = 28 // days
logVerbosity = logger.Debug
logSuppress = true
)
// Logging constants.
const (
logPath = "/var/log/audiolooper/audiolooper.log"
logMaxSize = 500 // MB
logMaxBackup = 10
logMaxAge = 28 // days
logVerbosity = logger.Debug
logSuppress = true
)
func main() {
filePtr := flag.String("path", "", "Path to sound file we wish to play.")
flag.Parse()
// Create lumberjack logger to handle logging to file.
fileLog := &lumberjack.Logger{
@ -66,64 +63,62 @@ func main() {
// Create logger that we call methods on to log.
log := logger.New(logVerbosity, fileLog, logSuppress)
// Pi model specific initialisation
// Call initialisation code that is specific to the platform (pi 0 or 3).
initCommand(log)
// Infinite loop that outputs audio and gathers debug information.
// Repeatedly play audio file.
var numPlays int
var fatal bool
for {
numPlays++
log.Log(logger.Debug, "playing audio", "play number", numPlays)
cmd := exec.Command(audioCmd, *filePtr)
cmd := exec.Command(audioCmd, *soundFilePtr)
var stdoutBuf, stderrBuf bytes.Buffer
stdoutIn, err := cmd.StdoutPipe()
// We'd like to see what the playback software is outputting, so pipe
// stdout and stderr.
outPipe, err := cmd.StdoutPipe()
if err != nil {
log.Log(logger.Error, "failed to make stdout pipe", "error", err.Error())
log.Log(logger.Error, "failed to pipe stdout", "error", err)
}
stderrIn, err := cmd.StderrPipe()
errPipe, err := cmd.StderrPipe()
if err != nil {
log.Log(logger.Error, "failed to make stderr pipe", "error", err.Error())
log.Log(logger.Error, "failed to pipe stderr", "error", err)
}
var errStdout, errStderr error
stdout := io.MultiWriter(os.Stdout, &stdoutBuf)
stderr := io.MultiWriter(os.Stderr, &stderrBuf)
// Start playback of the audio file.
err = cmd.Start()
if err != nil {
log.Log(logger.Error, "cmd.Start() failed", "error", err.Error())
fatal = true
log.Log(logger.Error, "start failed", "error", err.Error())
continue
}
numPlays++
log.Log(logger.Debug, "playing audio", "numPlays", numPlays)
var wg sync.WaitGroup
wg.Add(1)
// Copy any std out to a buffer for logging.
var outBuff bytes.Buffer
go func() {
_, errStdout = io.Copy(stdout, stdoutIn)
wg.Done()
_, err = io.Copy(&outBuff, outPipe)
if err != nil {
log.Log(logger.Error, "failed to copy out pipe", "error", err)
}
}()
_, errStderr = io.Copy(stderr, stderrIn)
wg.Wait()
// Copy any std error to a buffer for logging.
var errBuff bytes.Buffer
go func() {
_, err = io.Copy(&errBuff, errPipe)
if err != nil {
log.Log(logger.Error, "failed to copy error pipe", "error", err)
}
}()
// Wait for playback to complete.
err = cmd.Wait()
if err != nil {
log.Log(logger.Error, "cmd.Run() failed", "error", err.Error())
fatal = true
log.Log(logger.Error, "failed to wait for execution finish", "error", err.Error())
}
if errStdout != nil || errStderr != nil {
log.Log(logger.Error, "failed to capture stdout or stderr")
fatal = true
}
outStr, errStr := string(stdoutBuf.Bytes()), string(stderrBuf.Bytes())
log.Log(logger.Debug, "stdout received", "stdout", outStr)
log.Log(logger.Debug, "stderr received", "stderr", errStr)
log.Log(logger.Debug, "stdout received", "stdout", string(outBuff.Bytes()))
if fatal == true {
log.Log(logger.Fatal, "a fatal error has occured while trying to play (see above)")
// If there was any errors on stderr, log them.
if errBuff.Len() != 0 {
log.Log(logger.Error, "errors from stderr", "stderr", string(errBuff.Bytes()))
}
}
}