diff --git a/server/ctrlsubsonic/encode/encode.go b/server/ctrlsubsonic/encode/encode.go index 44be907..79821fc 100644 --- a/server/ctrlsubsonic/encode/encode.go +++ b/server/ctrlsubsonic/encode/encode.go @@ -2,6 +2,7 @@ package encode import ( "fmt" + "log" "net/http" "io" @@ -9,6 +10,7 @@ import ( "os/exec" "github.com/cespare/xxhash" + "github.com/pkg/errors" ) type Profile struct { @@ -37,7 +39,7 @@ func copyCmdOutput(out, cache io.Writer, pipeReader io.Reader) { // Start copying! if _, err := io.Copy(w, pipeReader); err != nil { - fmt.Printf("Error while writing encoded output: %s\n", err) + log.Printf("error while writing encoded output: %s\n", err) } } @@ -55,12 +57,12 @@ func writeCmdOutput(out, cache io.Writer, pipeReader io.ReadCloser) { data := buffer[0:n] _, err = out.Write(data) if err != nil { - fmt.Printf("Error while writing HTTP response: %s\n", err) + log.Printf("error while writing HTTP response: %s\n", err) } _, err = cache.Write(data) if err != nil { - fmt.Printf("Error while writing cache file: %s\n", err) + log.Printf("error while writing cache file: %s\n", err) } if f, ok := out.(http.Flusher); ok { @@ -106,7 +108,7 @@ func Encode(out io.Writer, trackPath, cachePath string, profile *Profile, bitrat // Create cache file: cacheFile, err := os.Create(cachePath) if err != nil { - fmt.Printf("Failed to write to cache file `%s`: %s\n", cachePath, err) + return errors.Wrapf(err, "writing to cache file %q: %v", cachePath) } //// I'm still unsure if buffer version (writeCmdOutput) is any better than io.Copy-based one (copyCmdOutput). @@ -116,21 +118,17 @@ func Encode(out io.Writer, trackPath, cachePath string, profile *Profile, bitrat go writeCmdOutput(out, cacheFile, pipeReader) // Run FFmpeg: - err = cmd.Run() - if err != nil { - fmt.Printf("Failed to encode `%s`: %s\n", trackPath, err) + if err := cmd.Run(); err != nil { + return errors.Wrapf(err, "running ffmpeg") } // Close all pipes and flush cache file: pipeWriter.Close() - err = cacheFile.Sync() - if err != nil { - fmt.Printf("Failed to flush `%s`: %s\n", cachePath, err) + if err := cacheFile.Sync(); err != nil { + return errors.Wrapf(err, "flushing %q", cachePath) } cacheFile.Close() - - fmt.Printf("`%s`: Encoded track to [%s/%s] successfully\n", - trackPath, profile.Format, profile.Bitrate) + return nil } // Generate cache key (file name). For, you know, encoded tracks cache. diff --git a/server/ctrlsubsonic/handlers_raw.go b/server/ctrlsubsonic/handlers_raw.go index 2efef46..43047f1 100644 --- a/server/ctrlsubsonic/handlers_raw.go +++ b/server/ctrlsubsonic/handlers_raw.go @@ -116,11 +116,14 @@ func (c *Controller) ServeStream(w http.ResponseWriter, r *http.Request) *spec.R if fileExists(cacheFile) { log.Printf("cache [%s/%s] hit!\n", profile.Format, bitrate) http.ServeFile(w, r, cacheFile) - return + return nil } + log.Printf("cache [%s/%s] miss!\n", profile.Format, bitrate) if err := encode.Encode(w, absPath, cacheFile, profile, bitrate); err != nil { - log.Printf("cache [%s/%s] miss!\n", profile.Format, bitrate) + log.Printf("error encoding %q: %v\n", absPath, err) } + log.Printf("track `%s` encoded to [%s/%s] successfully\n", + track.Filename, profile.Format, profile.Bitrate) return nil }