From 5004fee6ce1eb8fdde6acbe8b5b3d9e61e74859d Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Fri, 23 Dec 2022 11:40:22 +0100 Subject: port log usage to llog --- main.go | 44 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/main.go b/main.go index 09d8d95..517634c 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,6 @@ import ( "fmt" "html/template" "io/ioutil" - "log" "net/http" "muhq.space/go/wrms/llog" @@ -36,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") - log.Println(fmt.Sprintf("Searching for %s", pattern)) + llog.Debug(fmt.Sprintf("Searching for %s", pattern)) results := wrms.Player.Search(pattern) data, err := json.Marshal(Event{"search", results}) if err != nil { - log.Fatal(err) + llog.Error(fmt.Sprintf("Encoding the search result failed with %s", err)) return } - log.Println("Search returned", string(data)) + llog.Info(fmt.Sprintf("Search returned: %s", string(data))) w.Write(data) } @@ -58,7 +57,7 @@ func genericVoteHandler(w http.ResponseWriter, r *http.Request, vote string) { connId := uuidCookie.Value songUri := r.URL.Query().Get("song") - log.Println(fmt.Sprintf("%s song %s via url %s", vote, songUri, r.URL)) + llog.Info(fmt.Sprintf("%s song %s via url %s", vote, songUri, r.URL)) wrms.AdjustSongWeight(connId, songUri, vote) } @@ -77,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 { - log.Println("Failed to read request body", string(data)) + llog.Warning(fmt.Sprintf("Failed to read request body: %s", string(data))) http.Error(w, "Failed to read request body", http.StatusInternalServerError) return } @@ -88,7 +87,7 @@ func addHandler(w http.ResponseWriter, r *http.Request) { return } - log.Println("Added song", string(data)) + llog.Info(fmt.Sprintf("Added song %s", string(data))) wrms.AddSong(song) wrms.Broadcast(Event{"add", []Song{song}}) fmt.Fprintf(w, "Added song %s", string(data)) @@ -101,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 { - log.Println(err) + llog.Warning(fmt.Sprintf("Accepting the websocket failed with %s", err)) return } @@ -111,29 +110,29 @@ func wsEndpoint(w http.ResponseWriter, r *http.Request) { uuidCookie, err := r.Cookie("UUID") if err != nil { - log.Fatal("websocket connection has no set UUID cookie") + llog.Error("websocket connection has no set UUID cookie") } id, err := uuid.Parse(uuidCookie.Value) if err != nil { - log.Fatal("Invalid UUID set in cookie") + llog.Error("Invalid UUID set in cookie") } defer func() { - log.Println("cancel context of connection", id) + llog.Info(fmt.Sprintf("cancel context of connection %d", id)) cancel() }() conn := Connection{id, make(chan Event), c} wrms.Connections = append(wrms.Connections, conn) - log.Println("New websocket connection with id", id) + llog.Info(fmt.Sprintf("New websocket connection with id %d", id)) initialCmds := [][]byte{} if wrms.Playing { data, err := json.Marshal(Event{"play", []Song{wrms.CurrentSong}}) if err != nil { - log.Fatal(err) + llog.Error(fmt.Sprintf("Encoding the play event failed with %s", err)) return } initialCmds = append(initialCmds, data) @@ -144,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 { - log.Fatal(err) + llog.Error(fmt.Sprintf("Encoding the add event failed with %s", err)) return } initialCmds = append(initialCmds, data) @@ -161,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 { - log.Fatal(err) + llog.Error(fmt.Sprintf("Encoding the upvoted event failed with %s", err)) return } initialCmds = append(initialCmds, data) @@ -170,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 { - log.Fatal(err) + llog.Error(fmt.Sprintf("Encoding the upvoted event failed with %s", err)) return } initialCmds = append(initialCmds, data) } for _, data := range initialCmds { - log.Println("Sending initial cmd", string(data)) + llog.Info(fmt.Sprintf("Sending initial cmd %s", string(data))) err = c.Write(ctx, websocket.MessageText, data) if err != nil { - log.Println(err) + llog.Warning(fmt.Sprintf("Sending the initial commands failed with %s", err)) return } } @@ -188,14 +187,14 @@ func wsEndpoint(w http.ResponseWriter, r *http.Request) { for ev := range conn.Events { data, err := json.Marshal(ev) if err != nil { - log.Fatal(err) + llog.Error(fmt.Sprintf("Encoding the %s event failed with %s", ev.Event, err)) return } llog.Debug(fmt.Sprintf("Sending ev %s to %s", string(data), id)) err = c.Write(ctx, websocket.MessageText, data) if err != nil { - log.Println(err) + llog.Warning(fmt.Sprintf("Sending the ev %s to %d failed with %s", string(data), id, err)) return } } @@ -230,7 +229,6 @@ func main() { // wrms.AddSong(NewDummySong("Lala", "SNFMT")) // wrms.AddSong(NewDummySong("Hobelbank", "MC Wankwichtel")) - log.Println(wrms) - - log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", config.port), nil)) + err := http.ListenAndServe(fmt.Sprintf(":%d", config.port), nil) + llog.Error(fmt.Sprintf("Serving http failed with %s", err)) } -- cgit v1.2.3