Replaced Beego logging

This commit is contained in:
Deluan
2020-01-08 20:45:07 -05:00
committed by Deluan Quintão
parent 6eda38a951
commit 84d69a4f41
28 changed files with 559 additions and 282 deletions
+19 -18
View File
@@ -1,21 +1,22 @@
package engine
import (
"context"
"fmt"
"strconv"
"time"
"github.com/astaxie/beego"
"github.com/cloudsonic/sonic-server/domain"
"github.com/cloudsonic/sonic-server/log"
"github.com/cloudsonic/sonic-server/utils"
)
type Browser interface {
MediaFolders() (domain.MediaFolders, error)
Indexes(ifModifiedSince time.Time) (domain.ArtistIndexes, time.Time, error)
Directory(id string) (*DirectoryInfo, error)
Artist(id string) (*DirectoryInfo, error)
Album(id string) (*DirectoryInfo, error)
Directory(ctx context.Context, id string) (*DirectoryInfo, error)
Artist(ctx context.Context, id string) (*DirectoryInfo, error)
Album(ctx context.Context, id string) (*DirectoryInfo, error)
GetSong(id string) (*Entry, error)
}
@@ -73,32 +74,32 @@ type DirectoryInfo struct {
Genre string
}
func (b *browser) Artist(id string) (*DirectoryInfo, error) {
beego.Debug("Found Artist with id", id)
func (b *browser) Artist(ctx context.Context, id string) (*DirectoryInfo, error) {
a, albums, err := b.retrieveArtist(id)
if err != nil {
return nil, err
}
log.Debug(ctx, "Found Artist", "id", id, "name", a.Name)
return b.buildArtistDir(a, albums), nil
}
func (b *browser) Album(id string) (*DirectoryInfo, error) {
beego.Debug("Found Album with id", id)
func (b *browser) Album(ctx context.Context, id string) (*DirectoryInfo, error) {
al, tracks, err := b.retrieveAlbum(id)
if err != nil {
return nil, err
}
log.Debug(ctx, "Found Album", "id", id, "name", al.Name)
return b.buildAlbumDir(al, tracks), nil
}
func (b *browser) Directory(id string) (*DirectoryInfo, error) {
func (b *browser) Directory(ctx context.Context, id string) (*DirectoryInfo, error) {
switch {
case b.isArtist(id):
return b.Artist(id)
case b.isAlbum(id):
return b.Album(id)
case b.isArtist(ctx, id):
return b.Artist(ctx, id)
case b.isAlbum(ctx, id):
return b.Album(ctx, id)
default:
beego.Debug("Id", id, "not found")
log.Debug(ctx, "Directory not found", "id", id)
return nil, domain.ErrNotFound
}
}
@@ -153,19 +154,19 @@ func (b *browser) buildAlbumDir(al *domain.Album, tracks domain.MediaFiles) *Dir
return dir
}
func (b *browser) isArtist(id string) bool {
func (b *browser) isArtist(ctx context.Context, id string) bool {
found, err := b.artistRepo.Exists(id)
if err != nil {
beego.Debug(fmt.Errorf("Error searching for Artist %s: %v", id, err))
log.Debug(ctx, "Error searching for Artist", "id", id, err)
return false
}
return found
}
func (b *browser) isAlbum(id string) bool {
func (b *browser) isAlbum(ctx context.Context, id string) bool {
found, err := b.albumRepo.Exists(id)
if err != nil {
beego.Debug(fmt.Errorf("Error searching for Album %s: %v", id, err))
log.Debug(ctx, "Error searching for Album", "id", id, err)
return false
}
return found
+8 -8
View File
@@ -1,19 +1,19 @@
package engine
import (
"fmt"
"context"
"sort"
"github.com/astaxie/beego"
"github.com/cloudsonic/sonic-server/domain"
"github.com/cloudsonic/sonic-server/itunesbridge"
"github.com/cloudsonic/sonic-server/log"
)
type Playlists interface {
GetAll() (domain.Playlists, error)
Get(id string) (*PlaylistInfo, error)
Create(name string, ids []string) error
Delete(playlistId string) error
Create(ctx context.Context, name string, ids []string) error
Delete(ctx context.Context, playlistId string) error
Update(playlistId string, name *string, idsToAdd []string, idxToRemove []int) error
}
@@ -42,21 +42,21 @@ type PlaylistInfo struct {
Comment string
}
func (p *playlists) Create(name string, ids []string) error {
func (p *playlists) Create(ctx context.Context, name string, ids []string) error {
pid, err := p.itunes.CreatePlaylist(name, ids)
if err != nil {
return err
}
beego.Info(fmt.Sprintf("Created playlist '%s' with id '%s'", name, pid))
log.Info(ctx, "Created playlist", "playlist", name, "id", pid)
return nil
}
func (p *playlists) Delete(playlistId string) error {
func (p *playlists) Delete(ctx context.Context, playlistId string) error {
err := p.itunes.DeletePlaylist(playlistId)
if err != nil {
return err
}
beego.Info(fmt.Sprintf("Deleted playlist with id '%s'", playlistId))
log.Info(ctx, "Deleted playlist", "id", playlistId)
return nil
}
+11 -9
View File
@@ -1,15 +1,17 @@
package engine
import (
"github.com/astaxie/beego"
"context"
"github.com/cloudsonic/sonic-server/domain"
"github.com/cloudsonic/sonic-server/itunesbridge"
"github.com/cloudsonic/sonic-server/log"
"github.com/cloudsonic/sonic-server/utils"
)
type Ratings interface {
SetStar(star bool, ids ...string) error
SetRating(id string, rating int) error
SetStar(ctx context.Context, star bool, ids ...string) error
SetRating(ctx context.Context, id string, rating int) error
}
func NewRatings(itunes itunesbridge.ItunesControl, mr domain.MediaFileRepository, alr domain.AlbumRepository, ar domain.ArtistRepository) Ratings {
@@ -23,14 +25,14 @@ type ratings struct {
artistRepo domain.ArtistRepository
}
func (r ratings) SetRating(id string, rating int) error {
func (r ratings) SetRating(ctx context.Context, id string, rating int) error {
rating = utils.MinInt(rating, 5) * 20
isAlbum, _ := r.albumRepo.Exists(id)
if isAlbum {
mfs, _ := r.mfRepo.FindByAlbum(id)
if len(mfs) > 0 {
beego.Debug("SetRating:", rating, "Album:", mfs[0].Album)
log.Debug(ctx, "Set Rating", "value", rating, "album", mfs[0].Album)
if err := r.itunes.SetAlbumRating(mfs[0].Id, rating); err != nil {
return err
}
@@ -43,7 +45,7 @@ func (r ratings) SetRating(id string, rating int) error {
return err
}
if mf != nil {
beego.Debug("SetRating:", rating, "Song:", mf.Title)
log.Debug(ctx, "Set Rating", "value", rating, "song", mf.Title)
if err := r.itunes.SetTrackRating(mf.Id, rating); err != nil {
return err
}
@@ -52,13 +54,13 @@ func (r ratings) SetRating(id string, rating int) error {
return domain.ErrNotFound
}
func (r ratings) SetStar(star bool, ids ...string) error {
func (r ratings) SetStar(ctx context.Context, star bool, ids ...string) error {
for _, id := range ids {
isAlbum, _ := r.albumRepo.Exists(id)
if isAlbum {
mfs, _ := r.mfRepo.FindByAlbum(id)
if len(mfs) > 0 {
beego.Debug("SetStar:", star, "Album:", mfs[0].Album)
log.Debug(ctx, "Set Star", "value", star, "album", mfs[0].Album)
if err := r.itunes.SetAlbumLoved(mfs[0].Id, star); err != nil {
return err
}
@@ -71,7 +73,7 @@ func (r ratings) SetStar(star bool, ids ...string) error {
return err
}
if mf != nil {
beego.Debug("SetStar:", star, "Song:", mf.Title)
log.Debug(ctx, "Set Star", "value", star, "song", mf.Title)
if err := r.itunes.SetTrackLoved(mf.Id, star); err != nil {
return err
}
+11 -10
View File
@@ -1,13 +1,14 @@
package engine
import (
"context"
"errors"
"fmt"
"time"
"github.com/astaxie/beego"
"github.com/cloudsonic/sonic-server/domain"
"github.com/cloudsonic/sonic-server/itunesbridge"
"github.com/cloudsonic/sonic-server/log"
)
const (
@@ -16,8 +17,8 @@ const (
)
type Scrobbler interface {
Register(playerId int, trackId string, playDate time.Time) (*domain.MediaFile, error)
NowPlaying(playerId int, playerName, trackId, username string) (*domain.MediaFile, error)
Register(ctx context.Context, playerId int, trackId string, playDate time.Time) (*domain.MediaFile, error)
NowPlaying(ctx context.Context, playerId int, playerName, trackId, username string) (*domain.MediaFile, error)
}
func NewScrobbler(itunes itunesbridge.ItunesControl, mr domain.MediaFileRepository, npr NowPlayingRepository) Scrobbler {
@@ -30,7 +31,7 @@ type scrobbler struct {
npRepo NowPlayingRepository
}
func (s *scrobbler) detectSkipped(playerId int, trackId string) {
func (s *scrobbler) detectSkipped(ctx context.Context, playerId int, trackId string) {
size, _ := s.npRepo.Count(playerId)
switch size {
case 0:
@@ -53,22 +54,22 @@ func (s *scrobbler) detectSkipped(playerId int, trackId string) {
}
diff := np.Start.Sub(prev.Start)
if diff < minSkipped || diff > maxSkipped {
beego.Debug(fmt.Sprintf("-- Playtime for track %s was %v. Not skipping.", prev.TrackId, diff))
log.Debug(ctx, fmt.Sprintf("-- Playtime for track %s was %v. Not skipping.", prev.TrackId, diff))
prev = np
continue
}
err = s.itunes.MarkAsSkipped(prev.TrackId, prev.Start.Add(1*time.Minute))
if err != nil {
beego.Warn("Error skipping track", prev.TrackId)
log.Warn(ctx, "Error skipping track", "id", prev.TrackId)
} else {
beego.Debug("-- Skipped track", prev.TrackId)
log.Debug(ctx, "-- Skipped track "+prev.TrackId)
}
}
}
}
func (s *scrobbler) Register(playerId int, trackId string, playTime time.Time) (*domain.MediaFile, error) {
s.detectSkipped(playerId, trackId)
func (s *scrobbler) Register(ctx context.Context, playerId int, trackId string, playTime time.Time) (*domain.MediaFile, error) {
s.detectSkipped(ctx, playerId, trackId)
mf, err := s.mfRepo.Get(trackId)
if err != nil {
@@ -85,7 +86,7 @@ func (s *scrobbler) Register(playerId int, trackId string, playTime time.Time) (
return mf, nil
}
func (s *scrobbler) NowPlaying(playerId int, playerName, trackId, username string) (*domain.MediaFile, error) {
func (s *scrobbler) NowPlaying(ctx context.Context, playerId int, playerName, trackId, username string) (*domain.MediaFile, error) {
mf, err := s.mfRepo.Get(trackId)
if err != nil {
return nil, err
+17 -17
View File
@@ -27,7 +27,7 @@ func TestScrobbler(t *testing.T) {
Convey("When I scrobble an existing song", func() {
now := time.Now()
mf, err := scrobbler.Register(1, "2", now)
mf, err := scrobbler.Register(nil, 1, "2", now)
Convey("Then I get the scrobbled song back", func() {
So(err, ShouldBeNil)
@@ -42,7 +42,7 @@ func TestScrobbler(t *testing.T) {
})
Convey("When the ID is not in the DB", func() {
_, err := scrobbler.Register(1, "3", time.Now())
_, err := scrobbler.Register(nil, 1, "3", time.Now())
Convey("Then I receive an error", func() {
So(err, ShouldNotBeNil)
@@ -54,7 +54,7 @@ func TestScrobbler(t *testing.T) {
})
Convey("When I inform the song that is now playing", func() {
mf, err := scrobbler.NowPlaying(1, "DSub", "2", "deluan")
mf, err := scrobbler.NowPlaying(nil, 1, "DSub", "2", "deluan")
Convey("Then I get the song for that id back", func() {
So(err, ShouldBeNil)
@@ -100,11 +100,11 @@ func TestSkipping(t *testing.T) {
npRepo.ClearAll()
Convey("When I skip 2 songs", func() {
npRepo.OverrideNow(aPointInTime)
scrobbler.NowPlaying(1, "DSub", "1", "deluan")
scrobbler.NowPlaying(nil, 1, "DSub", "1", "deluan")
npRepo.OverrideNow(aPointInTime.Add(2 * time.Second))
scrobbler.NowPlaying(1, "DSub", "3", "deluan")
scrobbler.NowPlaying(nil, 1, "DSub", "3", "deluan")
npRepo.OverrideNow(aPointInTime.Add(3 * time.Second))
scrobbler.NowPlaying(1, "DSub", "2", "deluan")
scrobbler.NowPlaying(nil, 1, "DSub", "2", "deluan")
Convey("Then the NowPlaying song should be the last one", func() {
np, err := npRepo.GetAll()
So(err, ShouldBeNil)
@@ -114,12 +114,12 @@ func TestSkipping(t *testing.T) {
})
Convey("When I play one song", func() {
npRepo.OverrideNow(aPointInTime)
scrobbler.NowPlaying(1, "DSub", "1", "deluan")
scrobbler.NowPlaying(nil, 1, "DSub", "1", "deluan")
Convey("And I skip it before 20 seconds", func() {
npRepo.OverrideNow(aPointInTime.Add(7 * time.Second))
scrobbler.NowPlaying(1, "DSub", "2", "deluan")
scrobbler.NowPlaying(nil, 1, "DSub", "2", "deluan")
Convey("Then the first song should be marked as skipped", func() {
mf, err := scrobbler.Register(1, "2", aPointInTime.Add(3*time.Minute))
mf, err := scrobbler.Register(nil, 1, "2", aPointInTime.Add(3*time.Minute))
So(mf.Id, ShouldEqual, "2")
So(itCtrl.skipped, ShouldContainKey, "1")
So(err, ShouldBeNil)
@@ -127,9 +127,9 @@ func TestSkipping(t *testing.T) {
})
Convey("And I skip it before 3 seconds", func() {
npRepo.OverrideNow(aPointInTime.Add(2 * time.Second))
scrobbler.NowPlaying(1, "DSub", "2", "deluan")
scrobbler.NowPlaying(nil, 1, "DSub", "2", "deluan")
Convey("Then the first song should be marked as skipped", func() {
mf, err := scrobbler.Register(1, "2", aPointInTime.Add(3*time.Minute))
mf, err := scrobbler.Register(nil, 1, "2", aPointInTime.Add(3*time.Minute))
So(mf.Id, ShouldEqual, "2")
So(itCtrl.skipped, ShouldBeEmpty)
So(err, ShouldBeNil)
@@ -137,16 +137,16 @@ func TestSkipping(t *testing.T) {
})
Convey("And I skip it after 20 seconds", func() {
npRepo.OverrideNow(aPointInTime.Add(30 * time.Second))
scrobbler.NowPlaying(1, "DSub", "2", "deluan")
scrobbler.NowPlaying(nil, 1, "DSub", "2", "deluan")
Convey("Then the first song should be marked as skipped", func() {
mf, err := scrobbler.Register(1, "2", aPointInTime.Add(3*time.Minute))
mf, err := scrobbler.Register(nil, 1, "2", aPointInTime.Add(3*time.Minute))
So(mf.Id, ShouldEqual, "2")
So(itCtrl.skipped, ShouldBeEmpty)
So(err, ShouldBeNil)
})
})
Convey("And I scrobble it before starting to play the other song", func() {
mf, err := scrobbler.Register(1, "1", time.Now())
mf, err := scrobbler.Register(nil, 1, "1", time.Now())
Convey("Then the first song should NOT marked as skipped", func() {
So(mf.Id, ShouldEqual, "1")
So(itCtrl.skipped, ShouldBeEmpty)
@@ -156,10 +156,10 @@ func TestSkipping(t *testing.T) {
})
Convey("When the NowPlaying for the next song happens before the Scrobble", func() {
npRepo.OverrideNow(aPointInTime)
scrobbler.NowPlaying(1, "DSub", "1", "deluan")
scrobbler.NowPlaying(nil, 1, "DSub", "1", "deluan")
npRepo.OverrideNow(aPointInTime.Add(10 * time.Second))
scrobbler.NowPlaying(1, "DSub", "2", "deluan")
scrobbler.Register(1, "1", aPointInTime.Add(10*time.Minute))
scrobbler.NowPlaying(nil, 1, "DSub", "2", "deluan")
scrobbler.Register(nil, 1, "1", aPointInTime.Add(10*time.Minute))
Convey("Then the NowPlaying song should be the last one", func() {
np, _ := npRepo.GetAll()
So(np, ShouldHaveLength, 1)
+13 -12
View File
@@ -1,10 +1,11 @@
package engine
import (
"context"
"strings"
"github.com/astaxie/beego"
"github.com/cloudsonic/sonic-server/domain"
"github.com/cloudsonic/sonic-server/log"
"github.com/deluan/gomate"
"github.com/kennygrant/sanitize"
)
@@ -19,9 +20,9 @@ type Search interface {
RemoveAlbum(ids ...string) error
RemoveMediaFile(ids ...string) error
SearchArtist(q string, offset int, size int) (Entries, error)
SearchAlbum(q string, offset int, size int) (Entries, error)
SearchSong(q string, offset int, size int) (Entries, error)
SearchArtist(ctx context.Context, q string, offset int, size int) (Entries, error)
SearchAlbum(ctx context.Context, q string, offset int, size int) (Entries, error)
SearchSong(ctx context.Context, q string, offset int, size int) (Entries, error)
}
type search struct {
@@ -84,7 +85,7 @@ func (s *search) RemoveMediaFile(ids ...string) error {
return s.idxSong.Remove(ids...)
}
func (s *search) SearchArtist(q string, offset int, size int) (Entries, error) {
func (s *search) SearchArtist(ctx context.Context, q string, offset int, size int) (Entries, error) {
q = sanitize.Accents(strings.ToLower(strings.TrimSuffix(q, "*")))
min := offset
max := min + size - 1
@@ -95,7 +96,7 @@ func (s *search) SearchArtist(q string, offset int, size int) (Entries, error) {
res := make(Entries, 0, len(resp))
for _, id := range resp {
a, err := s.artistRepo.Get(id)
if criticalError("Artist", id, err) {
if criticalError(ctx, "Artist", id, err) {
return nil, err
}
if err == nil {
@@ -105,7 +106,7 @@ func (s *search) SearchArtist(q string, offset int, size int) (Entries, error) {
return res, nil
}
func (s *search) SearchAlbum(q string, offset int, size int) (Entries, error) {
func (s *search) SearchAlbum(ctx context.Context, q string, offset int, size int) (Entries, error) {
q = sanitize.Accents(strings.ToLower(strings.TrimSuffix(q, "*")))
min := offset
max := min + size - 1
@@ -116,7 +117,7 @@ func (s *search) SearchAlbum(q string, offset int, size int) (Entries, error) {
res := make(Entries, 0, len(resp))
for _, id := range resp {
al, err := s.albumRepo.Get(id)
if criticalError("Album", id, err) {
if criticalError(ctx, "Album", id, err) {
return nil, err
}
if err == nil {
@@ -126,7 +127,7 @@ func (s *search) SearchAlbum(q string, offset int, size int) (Entries, error) {
return res, nil
}
func (s *search) SearchSong(q string, offset int, size int) (Entries, error) {
func (s *search) SearchSong(ctx context.Context, q string, offset int, size int) (Entries, error) {
q = sanitize.Accents(strings.ToLower(strings.TrimSuffix(q, "*")))
min := offset
max := min + size - 1
@@ -137,7 +138,7 @@ func (s *search) SearchSong(q string, offset int, size int) (Entries, error) {
res := make(Entries, 0, len(resp))
for _, id := range resp {
mf, err := s.mfileRepo.Get(id)
if criticalError("Song", id, err) {
if criticalError(ctx, "Song", id, err) {
return nil, err
}
if err == nil {
@@ -147,12 +148,12 @@ func (s *search) SearchSong(q string, offset int, size int) (Entries, error) {
return res, nil
}
func criticalError(kind, id string, err error) bool {
func criticalError(ctx context.Context, kind, id string, err error) bool {
switch {
case err != nil:
return true
case err == domain.ErrNotFound:
beego.Warn(kind, "Id", id, "not in the DB. Need a reindex?")
log.Warn(ctx, kind+"Id not in DB. Need a reindex?", "id", id)
}
return false
}
+8 -7
View File
@@ -1,41 +1,42 @@
package engine
import (
"context"
"io"
"os"
"os/exec"
"strconv"
"strings"
"github.com/astaxie/beego"
"github.com/cloudsonic/sonic-server/conf"
"github.com/cloudsonic/sonic-server/log"
)
// TODO Encapsulate as a io.Reader
func Stream(path string, bitRate int, maxBitRate int, w io.Writer) error {
func Stream(ctx context.Context, path string, bitRate int, maxBitRate int, w io.Writer) error {
var f io.Reader
var err error
enabled := !conf.Sonic.DisableDownsampling
if enabled && maxBitRate > 0 && bitRate > maxBitRate {
f, err = downsample(path, maxBitRate)
f, err = downsample(ctx, path, maxBitRate)
} else {
f, err = os.Open(path)
}
if err != nil {
beego.Error("Error opening file", path, ":", err)
log.Error(ctx, "Error opening file", "path", path, err)
return err
}
if _, err = io.Copy(w, f); err != nil {
beego.Error("Error copying file", path, ":", err)
log.Error(ctx, "Error copying file", "path", path, err)
return err
}
return err
}
func downsample(path string, maxBitRate int) (f io.Reader, err error) {
func downsample(ctx context.Context, path string, maxBitRate int) (f io.Reader, err error) {
cmdLine, args := createDownsamplingCommand(path, maxBitRate)
beego.Debug("Executing cmd:", cmdLine, args)
log.Debug(ctx, "Executing command", "cmdLine", cmdLine, "args", args)
cmd := exec.Command(cmdLine, args...)
cmd.Stderr = os.Stderr
if f, err = cmd.StdoutPipe(); err != nil {