diff options
| author | Florian Fischer <florian.fischer@muhq.space> | 2022-12-28 13:27:05 +0100 |
|---|---|---|
| committer | Florian Fischer <florian.fischer@muhq.space> | 2022-12-28 13:27:05 +0100 |
| commit | aa5c7fc159b0f5175ede96d46b8d2a2669a176a4 (patch) | |
| tree | 58964be878af574263b8d1994f3097860db2192e | |
| parent | e15e1c6a4cb4a534d649cfc3d1be68498fddac48 (diff) | |
| download | WRMS-aa5c7fc159b0f5175ede96d46b8d2a2669a176a4.tar.gz WRMS-aa5c7fc159b0f5175ede96d46b8d2a2669a176a4.zip | |
llog: use variable argument list
Mirror more closely the fmt interface and reduce the verbosity
of the log call sites.
Additionally the log message is only created if we are actually going
to log it.
| -rw-r--r-- | llog/llog.go | 35 | ||||
| -rw-r--r-- | localBackend.go | 16 | ||||
| -rw-r--r-- | main.go | 38 | ||||
| -rw-r--r-- | player.go | 11 | ||||
| -rw-r--r-- | playlist.go | 9 | ||||
| -rw-r--r-- | wrms.go | 19 | ||||
| -rw-r--r-- | youtube.go | 10 |
7 files changed, 69 insertions, 69 deletions
diff --git a/llog/llog.go b/llog/llog.go index 3c05012..f7a5486 100644 --- a/llog/llog.go +++ b/llog/llog.go @@ -44,38 +44,41 @@ func (l LogLevel) String() string { return getLevelNames()[l-1] } -func Log(l LogLevel, msg string) { +func Log(l LogLevel, format string, a ...any) { + if l > Level { + return + } + + msg := fmt.Sprintf(format, a...) msg = fmt.Sprintf("[%s] %s", l.String(), msg) if l == l_Fatal { log.Fatalln(msg) - } - - if l <= Level { + } else { log.Println(msg) } } -func Fatal(msg string) { - Log(l_Fatal, msg) +func Fatal(format string, a ...any) { + Log(l_Fatal, format, a...) } -func Error(msg string) { - Log(l_Error, msg) +func Error(format string, a ...any) { + Log(l_Error, format, a...) } -func Warning(msg string) { - Log(l_Warning, msg) +func Warning(format string, a ...any) { + Log(l_Warning, format, a...) } -func Info(msg string) { - Log(l_Info, msg) +func Info(format string, a ...any) { + Log(l_Info, format, a...) } -func Debug(msg string) { - Log(l_Debug, msg) +func Debug(format string, a ...any) { + Log(l_Debug, format, a...) } -func DDebug(msg string) { - Log(l_DDebug, msg) +func DDebug(format string, a ...any) { + Log(l_DDebug, format, a...) } diff --git a/localBackend.go b/localBackend.go index a09d9a1..2dbd4fd 100644 --- a/localBackend.go +++ b/localBackend.go @@ -1,14 +1,14 @@ package main import ( - "fmt" - "github.com/dhowden/tag" - "muhq.space/go/wrms/llog" "os" "path" "path/filepath" "strings" "sync" + + "github.com/dhowden/tag" + "muhq.space/go/wrms/llog" ) type LocalBackend struct { @@ -31,7 +31,7 @@ func (b *LocalBackend) insert(song *Song) { } func (b *LocalBackend) findSongs() { - llog.Debug(fmt.Sprintf("Starting song search under: %s", b.musicDir)) + llog.Debug("Starting song search under: %s", b.musicDir) // Insert some Songs song := NewSong("No!", "Bukahara", "local", "Bukahara/Phantasma/01-No!.ogg") @@ -42,7 +42,7 @@ func (b *LocalBackend) findSongs() { err := filepath.Walk(b.musicDir, func(p string, finfo os.FileInfo, err error) error { if err != nil { - llog.Warning(fmt.Sprintf("error %v at path %s\n", err, p)) + llog.Warning("error %v at path %s\n", err, p) return err } @@ -60,13 +60,13 @@ func (b *LocalBackend) findSongs() { f, err := os.Open(p) defer f.Close() if err != nil { - llog.Warning(fmt.Sprintf("error %v opening file %s", err, p)) + llog.Warning("error %v opening file %s", err, p) return err } m, err := tag.ReadFrom(f) if err != nil { - llog.Warning(fmt.Sprintf("error reading tags from %s: %v", p, err)) + llog.Warning("error reading tags from %s: %v", p, err) // Contiune walking return nil } @@ -77,7 +77,7 @@ func (b *LocalBackend) findSongs() { }) if err != nil { - llog.Error(fmt.Sprintf("error walking the path %q: %v", b.musicDir, err)) + llog.Error("error walking the path %q: %v", b.musicDir, err) } } @@ -35,16 +35,16 @@ func landingPage(w http.ResponseWriter, r *http.Request) { func searchHandler(w http.ResponseWriter, r *http.Request) { pattern := r.URL.Query().Get("pattern") - llog.Debug(fmt.Sprintf("Searching for %s", pattern)) + llog.Debug("Searching for %s", pattern) results := wrms.Player.Search(pattern) data, err := json.Marshal(Event{"search", results}) if err != nil { - llog.Error(fmt.Sprintf("Encoding the search result failed with %s", err)) + llog.Error("Encoding the search result failed with %s", err) return } - llog.Info(fmt.Sprintf("Search returned: %s", string(data))) + llog.Info("Search returned: %s", string(data)) w.Write(data) } @@ -57,7 +57,7 @@ func genericVoteHandler(w http.ResponseWriter, r *http.Request, vote string) { connId := uuidCookie.Value songUri := r.URL.Query().Get("song") - llog.Info(fmt.Sprintf("%s song %s via url %s", vote, songUri, r.URL)) + llog.Info("%s song %s via url %s", vote, songUri, r.URL) wrms.AdjustSongWeight(connId, songUri, vote) } @@ -76,7 +76,7 @@ func unvoteHandler(w http.ResponseWriter, r *http.Request) { func addHandler(w http.ResponseWriter, r *http.Request) { data, err := ioutil.ReadAll(r.Body) if err != nil { - llog.Warning(fmt.Sprintf("Failed to read request body: %s", string(data))) + llog.Warning("Failed to read request body: %s", string(data)) http.Error(w, "Failed to read request body", http.StatusInternalServerError) return } @@ -87,7 +87,7 @@ func addHandler(w http.ResponseWriter, r *http.Request) { return } - llog.Info(fmt.Sprintf("Added song %s", string(data))) + llog.Info("Added song %s", string(data)) wrms.AddSong(song) wrms.Broadcast(Event{"add", []Song{song}}) fmt.Fprintf(w, "Added song %s", string(data)) @@ -100,7 +100,7 @@ func playPauseHandler(w http.ResponseWriter, r *http.Request) { func wsEndpoint(w http.ResponseWriter, r *http.Request) { c, err := websocket.Accept(w, r, nil) if err != nil { - llog.Warning(fmt.Sprintf("Accepting the websocket failed with %s", err)) + llog.Warning("Accepting the websocket failed with %s", err) return } @@ -119,20 +119,20 @@ func wsEndpoint(w http.ResponseWriter, r *http.Request) { } defer func() { - llog.Info(fmt.Sprintf("cancel context of connection %d", id)) + llog.Info("cancel context of connection %d", id) cancel() }() conn := Connection{id, make(chan Event), c} wrms.Connections = append(wrms.Connections, conn) - llog.Info(fmt.Sprintf("New websocket connection with id %d", id)) + llog.Info("New websocket connection with id %d", id) initialCmds := [][]byte{} if wrms.Playing { data, err := json.Marshal(Event{"play", []Song{wrms.CurrentSong}}) if err != nil { - llog.Error(fmt.Sprintf("Encoding the play event failed with %s", err)) + llog.Error("Encoding the play event failed with %s", err) return } initialCmds = append(initialCmds, data) @@ -143,7 +143,7 @@ func wsEndpoint(w http.ResponseWriter, r *http.Request) { if len(wrms.Songs) > 0 { data, err := json.Marshal(Event{"add", wrms.Songs}) if err != nil { - llog.Error(fmt.Sprintf("Encoding the add event failed with %s", err)) + llog.Error("Encoding the add event failed with %s", err) return } initialCmds = append(initialCmds, data) @@ -160,7 +160,7 @@ func wsEndpoint(w http.ResponseWriter, r *http.Request) { if len(upvoted) > 0 { data, err := json.Marshal(Event{"upvoted", upvoted}) if err != nil { - llog.Error(fmt.Sprintf("Encoding the upvoted event failed with %s", err)) + llog.Error("Encoding the upvoted event failed with %s", err) return } initialCmds = append(initialCmds, data) @@ -169,17 +169,17 @@ func wsEndpoint(w http.ResponseWriter, r *http.Request) { if len(downvoted) > 0 { data, err := json.Marshal(Event{"downvoted", downvoted}) if err != nil { - llog.Error(fmt.Sprintf("Encoding the upvoted event failed with %s", err)) + llog.Error("Encoding the upvoted event failed with %s", err) return } initialCmds = append(initialCmds, data) } for _, data := range initialCmds { - llog.Info(fmt.Sprintf("Sending initial cmd %s", string(data))) + llog.Info("Sending initial cmd %s", string(data)) err = c.Write(ctx, websocket.MessageText, data) if err != nil { - llog.Warning(fmt.Sprintf("Sending the initial commands failed with %s", err)) + llog.Warning("Sending the initial commands failed with %s", err) return } } @@ -187,14 +187,14 @@ func wsEndpoint(w http.ResponseWriter, r *http.Request) { for ev := range conn.Events { data, err := json.Marshal(ev) if err != nil { - llog.Error(fmt.Sprintf("Encoding the %s event failed with %s", ev.Event, err)) + llog.Error("Encoding the %s event failed with %s", ev.Event, err) return } - llog.Debug(fmt.Sprintf("Sending ev %s to %s", string(data), id)) + llog.Debug("Sending ev %s to %s", string(data), id) err = c.Write(ctx, websocket.MessageText, data) if err != nil { - llog.Warning(fmt.Sprintf("Sending the ev %s to %d failed with %s", string(data), id, err)) + llog.Warning("Sending the ev %s to %d failed with %s", string(data), id, err) return } } @@ -230,5 +230,5 @@ func main() { // wrms.AddSong(NewDummySong("Hobelbank", "MC Wankwichtel")) err := http.ListenAndServe(fmt.Sprintf(":%d", config.port), nil) - llog.Error(fmt.Sprintf("Serving http failed with %s", err)) + llog.Error("Serving http failed with %s", err) } @@ -1,7 +1,6 @@ package main import ( - "fmt" "io" "os/exec" "syscall" @@ -31,11 +30,11 @@ func NewPlayer(wrms *Wrms, backends []string) Player { case "local": b = NewLocalBackend(wrms.Config.localMusicDir) default: - llog.Error(fmt.Sprintf("Not supported backend %s", backend)) + llog.Error("Not supported backend %s", backend) } if err != nil { - llog.Error(fmt.Sprintf("Error during initialization of the %s backend: %s", backend, err.Error())) + llog.Error("Error during initialization of the %s backend: %s", backend, err.Error()) } else { available_backends[backend] = b } @@ -51,7 +50,7 @@ func (player *Player) Play(song *Song) { return } - llog.Info(fmt.Sprintf("Start playing %v", song)) + llog.Info("Start playing %v", song) player.Backends[song.Source].Play(song, player) } @@ -69,7 +68,7 @@ func (player *Player) runMpv() { } func (player *Player) PlayUri(uri string) { - llog.Info(fmt.Sprintf("Start mpv with %s", uri)) + llog.Info("Start mpv with %s", uri) player.mpv = exec.Command("mpv", "--no-video", uri) go player.runMpv() } @@ -89,7 +88,7 @@ func (player *Player) PlayData(data io.Reader) { defer stdin.Close() if _, err := io.Copy(stdin, data); err != nil { - llog.Fatal(fmt.Sprintf("Failed to write song data to mpv (%v)", err)) + llog.Fatal("Failed to write song data to mpv (%v)", err) } }() diff --git a/playlist.go b/playlist.go index c5fa9e1..249bad5 100644 --- a/playlist.go +++ b/playlist.go @@ -2,7 +2,6 @@ package main import ( "container/heap" - "fmt" "muhq.space/go/wrms/llog" ) @@ -38,22 +37,22 @@ func (pl *Playlist) Pop() any { } func (pl *Playlist) PopSong() *Song { - llog.DDebug(fmt.Sprintf("popping song from the playlist (%p) -> %v", pl, pl)) + llog.DDebug("popping song from the playlist (%p) -> %v", pl, pl) if len(*pl) == 0 { return nil } s := heap.Pop(pl).(*Song) - llog.DDebug(fmt.Sprintf("popped song %p from the playlist (%p) -> %v", s, pl, pl)) + llog.DDebug("popped song %p from the playlist (%p) -> %v", s, pl, pl) return s } func (pl *Playlist) Add(s *Song) { heap.Push(pl, s) - llog.DDebug(fmt.Sprintf("added song %p to the playlist (%p) -> %v", s, pl, pl)) + llog.DDebug("added song %p to the playlist (%p) -> %v", s, pl, pl) } func (pl *Playlist) Adjust(s *Song) { heap.Fix(pl, s.index) - llog.DDebug(fmt.Sprintf("adjusting song %p in the playlist (%p) -> %v", s, pl, pl)) + llog.DDebug("adjusting song %p in the playlist (%p) -> %v", s, pl, pl) } @@ -2,7 +2,6 @@ package main import ( "encoding/json" - "fmt" "strings" "muhq.space/go/wrms/llog" @@ -31,7 +30,7 @@ func NewSongFromJson(data []byte) (Song, error) { var s Song err := json.Unmarshal(data, &s) if err != nil { - llog.Error(fmt.Sprintf("Failed to parse song data %s with %s", string(data), err)) + llog.Error("Failed to parse song data %s with %s", string(data), err) return s, err } @@ -52,7 +51,7 @@ type Connection struct { } func (c *Connection) Close() { - llog.Info(fmt.Sprintf("Closing connection %s", c.Id)) + llog.Info("Closing connection %s", c.Id) close(c.Events) c.C.Close(websocket.StatusNormalClosure, "") } @@ -82,7 +81,7 @@ func (wrms *Wrms) Close() { } func (wrms *Wrms) Broadcast(cmd Event) { - llog.Info(fmt.Sprintf("Broadcasting %v", cmd)) + llog.Info("Broadcasting %v", cmd) for i := 0; i < len(wrms.Connections); i++ { wrms.Connections[i].Events <- cmd } @@ -92,7 +91,7 @@ func (wrms *Wrms) AddSong(song Song) { wrms.Songs = append(wrms.Songs, song) s := &wrms.Songs[len(wrms.Songs)-1] wrms.queue.Add(s) - llog.Info(fmt.Sprintf("Added song %s (ptr=%p) to Songs", s.Uri, s)) + llog.Info("Added song %s (ptr=%p) to Songs", s.Uri, s) } func (wrms *Wrms) Next() { @@ -102,7 +101,7 @@ func (wrms *Wrms) Next() { return } - llog.Info(fmt.Sprintf("popped next song and removing it from the song list %v", next)) + llog.Info("popped next song and removing it from the song list %v", next) for i, s := range wrms.Songs { if s.Uri == wrms.CurrentSong.Uri { @@ -147,11 +146,11 @@ func (wrms *Wrms) AdjustSongWeight(connId string, songUri string, vote string) { continue } - llog.Info(fmt.Sprintf("Adjusting song %s (ptr=%p)", s.Uri, s)) + llog.Info("Adjusting song %s (ptr=%p)", s.Uri, s) switch vote { case "up": if _, ok := s.Upvotes[connId]; ok { - llog.Error(fmt.Sprintf("Double upvote of song %s by connections %s", songUri, connId)) + llog.Error("Double upvote of song %s by connections %s", songUri, connId) return } @@ -166,7 +165,7 @@ func (wrms *Wrms) AdjustSongWeight(connId string, songUri string, vote string) { case "down": if _, ok := s.Downvotes[connId]; ok { - llog.Error(fmt.Sprintf("Double downvote of song %s by connections %s", songUri, connId)) + llog.Error("Double downvote of song %s by connections %s", songUri, connId) return } @@ -189,7 +188,7 @@ func (wrms *Wrms) AdjustSongWeight(connId string, songUri string, vote string) { delete(s.Upvotes, connId) s.Weight -= 1 } else { - llog.Error(fmt.Sprintf("Double unvote of song %s by connections %s", songUri, connId)) + llog.Error("Double unvote of song %s by connections %s", songUri, connId) return } @@ -27,11 +27,11 @@ type YoutubeDlSearchResult struct { func (b *YoutubeBackend) Search(pattern string) []Song { searchOption := fmt.Sprintf("ytsearch%d:%s", b.searchResults, pattern) - llog.Debug(fmt.Sprintf("Search youtube using: youtube-dl -j %s", searchOption)) + llog.Debug("Search youtube using: youtube-dl -j %s", searchOption) results, err := exec.Command("youtube-dl", "-j", searchOption).Output() if err != nil { - llog.Error(fmt.Sprintf("youtube-dl failed with: %s", err)) + llog.Error("youtube-dl failed with: %s", err) } songs := []Song{} @@ -43,12 +43,12 @@ func (b *YoutubeBackend) Search(pattern string) []Song { result := YoutubeDlSearchResult{} err = json.Unmarshal([]byte(l), &result) if err != nil { - llog.Debug(fmt.Sprintf("Parsing youtube-dl results '%s' failed", l)) - llog.Error(fmt.Sprintf("Parsing youtube-dl results failed with: %s", err)) + llog.Debug("Parsing youtube-dl results '%s' failed", l) + llog.Error("Parsing youtube-dl results failed with: %s", err) } songs = append(songs, NewSong(result.Title, "", "youtube", result.Id)) } - llog.Debug(fmt.Sprintf("youtube found %d matching videos", len(songs))) + llog.Debug("youtube found %d matching videos", len(songs)) return songs } |
