Skip to content

Commit b432f63

Browse files
committed
improvements to runtime logging
1 parent d882e36 commit b432f63

File tree

6 files changed

+19
-19
lines changed

6 files changed

+19
-19
lines changed

src/debug/debug.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ func Init(level string) {
1111
slog.SetLogLoggerLevel(getLogLevel(level))
1212
}
1313

14-
func Debug(ctx string) slog.Attr {
14+
func RuntimeAttr(ctx string) slog.Attr {
1515
_, file, line, ok := runtime.Caller(1)
1616
if ok {
1717
return slog.Group("runtime",
1818
slog.String("file", file),
19-
slog.Int("line",line),
20-
slog.String("msg", ctx),
19+
slog.Int("line", line),
20+
slog.String("ctx", ctx),
2121
)
2222
} else {
23-
return slog.String("msg", ctx)
23+
return slog.String("msg", "failed getting runtime")
2424
}
2525
}
2626

src/downloader/downloader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func moveDownload(srcDir, destDir, trackPath, file string) error { // Move downl
172172

173173
defer func() error {
174174
if cerr := in.Close(); cerr != nil {
175-
return fmt.Errorf("warning: failed to close source file: %s", cerr)
175+
return fmt.Errorf("failed to close source file: %s", cerr)
176176
}
177177
return nil
178178
}()

src/downloader/monitor.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (c *DownloadClient) MonitorDownloads(tracks []*models.Track, m Monitor) err
8888
file, path := parsePath(track.File)
8989
if monCfg.MigrateDownload {
9090
if err = moveDownload(monCfg.FromDir, monCfg.ToDir, path, file); err != nil {
91-
debug.Debug(err.Error())
91+
slog.Debug("error while moving file", debug.RuntimeAttr(err.Error()))
9292
} else {
9393
slog.Info("track moved successfully", "service", monCfg.Service)
9494
}
@@ -97,7 +97,7 @@ func (c *DownloadClient) MonitorDownloads(tracks []*models.Track, m Monitor) err
9797
track.File = file
9898
successDownloads += 1
9999
if err = m.Cleanup(*track, fileStatus.ID); err != nil {
100-
debug.Debug(err.Error())
100+
slog.Debug("cleanup failed", debug.RuntimeAttr(err.Error()))
101101
}
102102
continue
103103

@@ -111,7 +111,7 @@ func (c *DownloadClient) MonitorDownloads(tracks []*models.Track, m Monitor) err
111111
slog.Info("[monitor] no download progress for file, skipping", "service", monCfg.Service, "file", track.File, "duration", monCfg.MonitorDuration)
112112
tracker.Skipped = true
113113
if err = m.Cleanup(*track, fileStatus.ID); err != nil {
114-
debug.Debug(err.Error())
114+
slog.Debug("cleanup failed", debug.RuntimeAttr(err.Error()))
115115
}
116116
continue
117117
}

src/downloader/slskd.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,11 @@ func (c Slskd) searchStatus(ID, trackDetails string, count int) (bool, error) {
204204
} else if queryResult.IsComplete && (queryResult.FileCount == 0 || queryResult.FileCount == queryResult.LockedFileCount) {
205205
return false, fmt.Errorf("search complete, did not find any available files for %s", trackDetails)
206206
} else if count >= c.Cfg.Retry {
207-
debug.Debug(fmt.Sprintf("search not completed for ID: %s", ID))
207+
slog.Debug(fmt.Sprintf("failed to remove %s", ID), debug.RuntimeAttr(""))
208208
return false, fmt.Errorf("search wasn't completed after %d retries, skipping %s", count, trackDetails)
209209
}
210210

211-
debug.Debug(fmt.Sprintf("[%d/%d] Searching for %s", count, c.Cfg.Retry, trackDetails))
211+
slog.Debug(fmt.Sprintf("[%s] (%d/%d) Searching for %s", "slskd", count, c.Cfg.Retry, trackDetails))
212212
time.Sleep(20 * time.Second)
213213
return c.searchStatus(ID, trackDetails, count+1)
214214
}
@@ -336,7 +336,7 @@ func (c Slskd) queueDownload(files []File, track *models.Track) error {
336336
continue
337337
}
338338
if err := c.deleteSearch(track.ID); err != nil {
339-
debug.Debug(fmt.Sprintf("failed to delete search: %s", err.Error()))
339+
slog.Debug("failed to delete search", debug.RuntimeAttr(err.Error()))
340340
}
341341
return fmt.Errorf("couldn't download track: %s - %s", track.CleanTitle, track.Artist)
342342
}
@@ -400,10 +400,10 @@ func (c Slskd) deleteDownload(user, ID string) error {
400400

401401
func (c *Slskd) Cleanup(track models.Track, fileID string) error {
402402
if err := c.deleteSearch(track.ID); err != nil {
403-
debug.Debug(fmt.Sprintf("failed to delete search request: %v", err))
403+
slog.Debug("failed to delete search request", debug.RuntimeAttr(err.Error()))
404404
}
405405
if err := c.deleteDownload(track.MainArtistID, fileID); err != nil {
406-
debug.Debug(fmt.Sprintf("failed to delete download: %v", err))
406+
slog.Debug("failed to delete download", debug.RuntimeAttr(err.Error()))
407407
}
408408
return nil
409409
}

src/downloader/youtube.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (c *Youtube) QueryTrack(track *models.Track) error { // Queries youtube for
9191

9292
func queryYTMusic(track *models.Track, query string) error {
9393

94-
debug.Debug(fmt.Sprintf("Querying YTMusic for track %s", query))
94+
slog.Debug(fmt.Sprintf("Querying YTMusic for track %s", query))
9595

9696
cmd := exec.Command("python3", "search_ytmusic.py", query, "1")
9797

@@ -187,7 +187,7 @@ func saveVideo(c Youtube, track models.Track, stream *goutubedl.DownloadResult)
187187
if _, err = io.Copy(file, stream); err != nil {
188188
slog.Error("failed to copy stream to file", "context", err.Error())
189189
if err = os.Remove(input); err != nil {
190-
debug.Debug(fmt.Sprintf("failed to remove %s: %s", input, err.Error()))
190+
slog.Debug(fmt.Sprintf("failed to remove file %s", input), debug.RuntimeAttr(err.Error()))
191191
}
192192
return false
193193
}
@@ -205,12 +205,12 @@ func saveVideo(c Youtube, track models.Track, stream *goutubedl.DownloadResult)
205205
if err = cmd.Run(); err != nil {
206206
slog.Error("failed to convert audio", "context", err.Error())
207207
if err = os.Remove(input); err != nil {
208-
debug.Debug(fmt.Sprintf("failed to remove %s: %s", input, err.Error()))
208+
slog.Debug(fmt.Sprintf("failed to remove %s", input), debug.RuntimeAttr(err.Error()))
209209
}
210210
return false
211211
}
212212
if err = os.Remove(input); err != nil {
213-
debug.Debug(fmt.Sprintf("failed to remove %s: %s", input, err.Error()))
213+
slog.Debug(fmt.Sprintf("failed to remove %s", input), debug.RuntimeAttr(err.Error()))
214214
}
215215
return true
216216
}

src/util/http.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (c *HttpClient) MakeRequest(method, url string, payload io.Reader, headers
5656
}
5757

5858
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
59-
debug.Debug(fmt.Sprintf("full response: %s", string(body)))
59+
slog.Debug("response info", debug.RuntimeAttr(string(body)))
6060
return nil, fmt.Errorf("got %d from %s", resp.StatusCode, url)
6161
}
6262

@@ -66,7 +66,7 @@ func (c *HttpClient) MakeRequest(method, url string, payload io.Reader, headers
6666
func ParseResp[T any](body []byte, target *T) error {
6767

6868
if err := json.Unmarshal(body, target); err != nil {
69-
debug.Debug(fmt.Sprintf("full response: %s", string(body)))
69+
slog.Debug("response info", debug.RuntimeAttr(string(body)))
7070
return fmt.Errorf("error unmarshaling response body: %s", err.Error())
7171
}
7272
return nil

0 commit comments

Comments
 (0)