Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate logrus in favour of log/slog #137

Merged
merged 5 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,40 @@ package main

import (
"context"
slog "log/slog"

"github.com/marcus-crane/october/backend"
"github.com/sirupsen/logrus"
)

// App struct
type App struct {
ctx context.Context
logger *slog.Logger
portable bool
}

// NewApp creates a new App application struct
func NewApp(portable bool) *App {
func NewApp(portable bool, logger *slog.Logger) *App {
logger.Debug("Initialising app struct")
return &App{
logger: logger,
portable: portable,
}
}

// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.logger.Debug("Calling app startup method")
a.ctx = ctx
}

func (a *App) domReady(ctx context.Context) {
a.logger.Debug("Calling app domReady method")
a.ctx = ctx
backend.StartLogger(a.portable)
logrus.WithContext(ctx).Info("Logger should be initialised now")
logrus.WithContext(ctx).Info("Backend is about to start up")
}

func (a *App) shutdown(ctx context.Context) {
logrus.WithContext(ctx).Info("Shutting down. Goodbye!")
a.logger.Debug("Calling app shutdown method")
backend.CloseLogFile()
}
2 changes: 0 additions & 2 deletions backend/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package backend

import (
"github.com/glebarez/sqlite"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)

Expand All @@ -11,7 +10,6 @@ var Conn *gorm.DB
func OpenConnection(filepath string) error {
conn, err := gorm.Open(sqlite.Open(filepath), &gorm.Config{})
if err != nil {
logrus.WithError(err).WithField("filepath", filepath).Error("Failed to open DB connection")
return err
}
Conn = conn
Expand Down
57 changes: 38 additions & 19 deletions backend/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package backend

import (
"fmt"
"log/slog"
"strings"

"github.com/sirupsen/logrus"

"github.com/pgaskin/koboutils/v2/kobo"
)

Expand Down Expand Up @@ -155,14 +154,20 @@ func (Bookmark) TableName() string {
return "Bookmark"
}

func GetKoboMetadata(detectedPaths []string) []Kobo {
func GetKoboMetadata(detectedPaths []string, logger *slog.Logger) []Kobo {
var kobos []Kobo
for _, path := range detectedPaths {
_, _, deviceId, err := kobo.ParseKoboVersion(path)
if err != nil {
logrus.WithField("kobo_path", path).WithError(err).Error("Failed to parse Kobo version")
logger.Error("Failed to parse Kobo version",
slog.String("error", err.Error()),
slog.String("kobo_path", path),
)
}
logrus.WithField("device_id", deviceId).Info("Found an attached device")
logger.Info("Found attached device",
slog.String("device_id", deviceId),
slog.String("kobo_path", path),
)
device, found := kobo.DeviceByID(deviceId)
if found {
kobos = append(kobos, Kobo{
Expand All @@ -187,7 +192,9 @@ func GetKoboMetadata(detectedPaths []string) []Kobo {
})
continue
}
logrus.WithField("device_id", deviceId).Warn("Found a device that isn't officially supported but will likely still operate just fine")
logger.Warn("Found a device that isn't officially supported but will likely still operate just fine",
slog.String("device_id", deviceId),
)
// We can handle unsupported Kobos in future but at present, there are none
kobos = append(kobos, Kobo{
MntPath: path,
Expand All @@ -197,55 +204,67 @@ func GetKoboMetadata(detectedPaths []string) []Kobo {
return kobos
}

func (k *Kobo) ListDeviceContent(includeStoreBought bool) ([]Content, error) {
func (k *Kobo) ListDeviceContent(includeStoreBought bool, logger *slog.Logger) ([]Content, error) {
var content []Content
logrus.Debug("Retrieving content list from device")
logger.Debug("Retrieving content list from device")
result := Conn.Where(&Content{ContentType: "6", VolumeIndex: -1})
if !includeStoreBought {
result = result.Where("ContentID LIKE '%file:///%'")
}
result = result.Order("___PercentRead desc, title asc").Find(&content)
if result.Error != nil {
logrus.WithError(result.Error).Error("Failed to retrieve content from device")
logger.Error("Failed to retrieve content from device",
slog.String("error", result.Error.Error()),
)
return nil, result.Error
}
logrus.WithField("content_count", len(content)).Debug("Successfully retrieved device content")
logger.Debug("Successfully retrieved device content",
slog.Int("content_count", len(content)),
)
return content, nil
}

func (k *Kobo) ListDeviceBookmarks(includeStoreBought bool) ([]Bookmark, error) {
func (k *Kobo) ListDeviceBookmarks(includeStoreBought bool, logger *slog.Logger) ([]Bookmark, error) {
var bookmarks []Bookmark
logrus.Debug("Retrieving bookmarks from device")
logger.Debug("Retrieving bookmarks from device")
result := Conn
if !includeStoreBought {
result = result.Where("VolumeID LIKE '%file:///%'")
}
result = result.Order("VolumeID ASC, ChapterProgress ASC").Find(&bookmarks).Limit(1)
if result.Error != nil {
logrus.WithError(result.Error).Error("Failed to retrieve bookmarks from device")
logger.Error("Failed to retrieve bookmarks from device",
slog.String("error", result.Error.Error()),
)
return nil, result.Error
}
logrus.WithField("bookmark_count", len(bookmarks)).Debug("Successfully retrieved device bookmarks")
logger.Debug("Successfully retrieved device bookmarks",
slog.Int("bookmark_count", len(bookmarks)),
)
return bookmarks, nil
}

func (k *Kobo) BuildContentIndex(content []Content) map[string]Content {
logrus.Debug("Building an index out of device content")
func (k *Kobo) BuildContentIndex(content []Content, logger *slog.Logger) map[string]Content {
logger.Debug("Building an index out of device content")
contentIndex := make(map[string]Content)
for _, item := range content {
contentIndex[item.ContentID] = item
}
logrus.WithField("index_count", len(contentIndex)).Debug("Built content index")
logger.Debug("Built content index",
slog.Int("index_count", len(contentIndex)),
)
return contentIndex
}

func (k *Kobo) CountDeviceBookmarks() HighlightCounts {
func (k *Kobo) CountDeviceBookmarks(logger *slog.Logger) HighlightCounts {
var totalCount int64
var officialCount int64
var sideloadedCount int64
result := Conn.Model(&Bookmark{}).Count(&totalCount)
if result.Error != nil {
logrus.WithError(result.Error).Error("Failed to count bookmarks on device")
logger.Error("Failed to count bookmarks on device",
slog.String("error", result.Error.Error()),
)
}
Conn.Model(&Bookmark{}).Where("VolumeID LIKE '%file:///%'").Count(&sideloadedCount)
Conn.Model(&Bookmark{}).Where("VolumeID NOT LIKE '%file:///%'").Count(&officialCount)
Expand Down
12 changes: 5 additions & 7 deletions backend/device_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package backend

import (
"log/slog"
"os"
"path/filepath"
"testing"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

Expand All @@ -22,20 +22,18 @@ func setupTmpKobo(dir string, deviceId string) string {
content := []byte(deviceId)
err := os.Mkdir(filepath.Join(dir, ".kobo"), 0777)
if err != nil {
logrus.Fatal(err)
return ""
panic(err)
}
tmpfn := filepath.Join(dir, ".kobo", "version")
if err := os.WriteFile(tmpfn, content, 0666); err != nil {
logrus.Fatal(err)
return ""
panic(err)
}
return dir
}

func TestGetKoboMetadata_HandleNoDevices(t *testing.T) {
var expected []Kobo
actual := GetKoboMetadata([]string{})
actual := GetKoboMetadata([]string{}, slog.New(&discardHandler{}))
assert.Equal(t, expected, actual)
}

Expand Down Expand Up @@ -85,6 +83,6 @@ func TestGetKoboMetadata_HandleConnectedDevices(t *testing.T) {
},
}
detectedPaths := []string{fakeLibraVolume, fakeMiniVolume, fakeElipsaVolume, fakeClara2EVolume, fakeUnknownVolume}
actual := GetKoboMetadata(detectedPaths)
actual := GetKoboMetadata(detectedPaths, slog.New(&discardHandler{}))
assert.Equal(t, expected, actual)
}
Loading
Loading