From 86c2c38dfeaf88d4f021f8d06b9cdbcc0119c1df Mon Sep 17 00:00:00 2001 From: abs3nt Date: Sun, 18 Feb 2024 22:17:28 -0800 Subject: [PATCH] log settings --- main.go | 10 ++++- src/app/fx.go | 39 ------------------- src/components/commands/play.go | 8 +++- src/components/logger/logger.go | 67 +++++++++++++++++++++++++++++++++ src/components/tui/main.go | 14 ++++++- src/components/tui/views.go | 1 + src/config/config.go | 2 + src/services/config.go | 18 +++++++++ 8 files changed, 115 insertions(+), 44 deletions(-) delete mode 100644 src/app/fx.go create mode 100644 src/components/logger/logger.go create mode 100644 src/services/config.go diff --git a/main.go b/main.go index 0f8912a..816fe11 100644 --- a/main.go +++ b/main.go @@ -1,22 +1,28 @@ package main import ( + "gfx.cafe/util/go/fxplus" "go.uber.org/fx" - "git.asdf.cafe/abs3nt/gspot/src/app" "git.asdf.cafe/abs3nt/gspot/src/components/cache" "git.asdf.cafe/abs3nt/gspot/src/components/cli" "git.asdf.cafe/abs3nt/gspot/src/components/commands" + "git.asdf.cafe/abs3nt/gspot/src/components/logger" + "git.asdf.cafe/abs3nt/gspot/src/services" ) func main() { var s fx.Shutdowner app := fx.New( + fxplus.WithLogger, fx.Populate(&s), - app.Config, + services.Config, fx.Provide( + fxplus.Context, cache.NewCache, commands.NewCommander, + services.NewSpotifyClient, + logger.NewLogger, ), fx.Invoke( cli.Run, diff --git a/src/app/fx.go b/src/app/fx.go deleted file mode 100644 index 9d8cc30..0000000 --- a/src/app/fx.go +++ /dev/null @@ -1,39 +0,0 @@ -package app - -import ( - "log/slog" - "os" - - "gfx.cafe/util/go/fxplus" - "git.asdf.cafe/abs3nt/gunner" - "github.com/lmittmann/tint" - "go.uber.org/fx" - - "git.asdf.cafe/abs3nt/gspot/src/config" - "git.asdf.cafe/abs3nt/gspot/src/services" -) - -var Services = fx.Options( - fx.NopLogger, - fx.Provide( - func() *slog.Logger { - return slog.New(tint.NewHandler(os.Stdout, &tint.Options{ - Level: slog.LevelDebug.Level(), - TimeFormat: "[15:04:05.000]", - })) - }, - services.NewSpotifyClient, - fxplus.Context, - ), -) - -var Config = fx.Options( - fx.Provide( - func() *config.Config { - c := &config.Config{} - gunner.LoadApp(c, "gspot") - return c - }, - ), - Services, -) diff --git a/src/components/commands/play.go b/src/components/commands/play.go index 74b9658..0f2e877 100644 --- a/src/components/commands/play.go +++ b/src/components/commands/play.go @@ -29,6 +29,7 @@ func (c *Commander) Play() error { } func (c *Commander) PlayLikedSongs(position int) error { + c.Log.Debug("Playing liked songs") err := c.ClearRadio() if err != nil { return err @@ -37,6 +38,7 @@ func (c *Commander) PlayLikedSongs(position int) error { if err != nil { return err } + c.Log.Debug("got playlist", "id", playlist.ID) songs, err := c.Client.CurrentUsersTracks(c.Context, spotify.Limit(50), spotify.Offset(position)) if err != nil { return err @@ -49,11 +51,13 @@ func (c *Commander) PlayLikedSongs(position int) error { if err != nil { return err } + c.Log.Debug("added songs to playlist") err = c.Client.PlayOpt(c.Context, &spotify.PlayOptions{ PlaybackContext: &playlist.URI, }) if err != nil { if isNoActiveError(err) { + c.Log.Debug("need to activate device") deviceID, err := c.activateDevice() if err != nil { return err @@ -67,7 +71,9 @@ func (c *Commander) PlayLikedSongs(position int) error { } } } + c.Log.Debug("starting loop") for page := 2; page <= 5; page++ { + c.Log.Debug("doing loop", "page", page) songs, err := c.Client.CurrentUsersTracks(c.Context, spotify.Limit(50), spotify.Offset((50*(page-1))+position)) if err != nil { return err @@ -81,7 +87,7 @@ func (c *Commander) PlayLikedSongs(position int) error { return err } } - + c.Log.Debug("done") return err } diff --git a/src/components/logger/logger.go b/src/components/logger/logger.go new file mode 100644 index 0000000..0f601ed --- /dev/null +++ b/src/components/logger/logger.go @@ -0,0 +1,67 @@ +package logger + +import ( + "log/slog" + "os" + "path/filepath" + "strings" + + "github.com/lmittmann/tint" + "go.uber.org/fx" + + "git.asdf.cafe/abs3nt/gspot/src/config" +) + +type LoggerResult struct { + fx.Out + Logger *slog.Logger +} + +type LoggerParams struct { + fx.In + + Config *config.Config +} + +func NewLogger(p LoggerParams) LoggerResult { + lvl := slog.LevelInfo + configLevel := strings.ToUpper(p.Config.LogLevel) + switch configLevel { + case "INFO": + lvl = slog.LevelInfo + case "WARN": + lvl = slog.LevelWarn + case "ERROR": + lvl = slog.LevelError + case "DEBUG": + lvl = slog.LevelDebug + } + if strings.ToUpper(p.Config.LogOutput) == "FILE" { + fp := "" + p, err := os.UserConfigDir() + if err != nil { + p, err := os.UserHomeDir() + if err != nil { + os.Exit(1) + } + fp = filepath.Join(p, ".config", "gspot", "gspot.log") + } else { + fp = filepath.Join(p, "gspot", "gspot.log") + } + f, err := os.Create(fp) + if err != nil { + os.Exit(1) + } + return LoggerResult{ + Logger: slog.New(slog.NewJSONHandler(f, &slog.HandlerOptions{ + Level: lvl.Level(), + })), + } + } + return LoggerResult{ + Logger: slog.New(tint.NewHandler(os.Stdout, &tint.Options{ + Level: lvl.Level(), + TimeFormat: "[15:04:05.000]", + })), + } +} diff --git a/src/components/tui/main.go b/src/components/tui/main.go index 1fe0ae4..0dd395b 100644 --- a/src/components/tui/main.go +++ b/src/components/tui/main.go @@ -604,8 +604,11 @@ func Tick() tea.Cmd { func (m *mainModel) TickPlayback() { playing, _ := m.commands.Client.PlayerCurrentlyPlaying(m.commands.Context) if playing != nil && playing.Playing && playing.Item != nil { + if currentlyPlaying == nil || currentlyPlaying.Item == nil || + currentlyPlaying.Item.ID != playing.Item.ID { + playbackContext, _ = m.getContext(playing) + } currentlyPlaying = playing - playbackContext, _ = m.getContext(playing) } ticker := time.NewTicker(1 * time.Second) quit := make(chan struct{}) @@ -613,10 +616,14 @@ func (m *mainModel) TickPlayback() { for { select { case <-ticker.C: + m.commands.Log.Debug("TICKING PLAYBACK") playing, _ := m.commands.Client.PlayerCurrentlyPlaying(m.commands.Context) if playing != nil && playing.Playing && playing.Item != nil { + if currentlyPlaying == nil || currentlyPlaying.Item == nil || + currentlyPlaying.Item.ID != playing.Item.ID { + playbackContext, _ = m.getContext(playing) + } currentlyPlaying = playing - playbackContext, _ = m.getContext(playing) } case <-quit: ticker.Stop() @@ -665,18 +672,21 @@ func (m *mainModel) getContext(playing *spotify.CurrentlyPlaying) (string, error id := strings.Split(string(context.URI), ":")[2] switch context.Type { case "album": + m.commands.Log.Debug("ALBUM CONTEXT") album, err := m.commands.Client.GetAlbum(m.commands.Context, spotify.ID(id)) if err != nil { return "", err } return album.Name, nil case "playlist": + m.commands.Log.Debug("PLAYLIST CONTEXT") playlist, err := m.commands.Client.GetPlaylist(m.commands.Context, spotify.ID(id)) if err != nil { return "", err } return playlist.Name, nil case "artist": + m.commands.Log.Debug("ARTIST CONTEXT") artist, err := m.commands.Client.GetArtist(m.commands.Context, spotify.ID(id)) if err != nil { return "", err diff --git a/src/components/tui/views.go b/src/components/tui/views.go index 04a3829..69d14a4 100644 --- a/src/components/tui/views.go +++ b/src/components/tui/views.go @@ -267,6 +267,7 @@ func SavedTracksView(commands *commands.Commander) ([]list.Item, error) { } func MainView(c *commands.Commander) ([]list.Item, error) { + c.Log.Debug("SWITCHING TO MAIN VIEW") wg := errgroup.Group{} var saved_items *spotify.SavedTrackPage var playlists *spotify.SimplePlaylistPage diff --git a/src/config/config.go b/src/config/config.go index 435a406..67de52a 100644 --- a/src/config/config.go +++ b/src/config/config.go @@ -5,4 +5,6 @@ type Config struct { ClientSecret string `yaml:"client_secret"` ClientSecretCmd string `yaml:"client_secret_cmd"` Port string `yaml:"port"` + LogLevel string `yaml:"log_level" default:"info"` + LogOutput string `yaml:"log_output" default:"stdout"` } diff --git a/src/services/config.go b/src/services/config.go new file mode 100644 index 0000000..fe2cf22 --- /dev/null +++ b/src/services/config.go @@ -0,0 +1,18 @@ +package services + +import ( + "git.asdf.cafe/abs3nt/gunner" + "go.uber.org/fx" + + "git.asdf.cafe/abs3nt/gspot/src/config" +) + +var Config = fx.Options( + fx.Provide( + func() *config.Config { + c := &config.Config{} + gunner.LoadApp(c, "gspot") + return c + }, + ), +)