Skip to content

Commit

Permalink
Finish migration to log/slog
Browse files Browse the repository at this point in the history
Signed-off-by: Marcus Crane <[email protected]>
  • Loading branch information
marcus-crane committed Dec 16, 2024
1 parent f4fcd27 commit 9dbe5ee
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 83 deletions.
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)
}
17 changes: 11 additions & 6 deletions backend/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func StartBackend(ctx *context.Context, version string, portable bool, logger *s
RuntimeContext: ctx,
Settings: settings,
Readwise: &Readwise{
logger: logger,
UserAgent: fmt.Sprintf(UserAgentFmt, version),
},
Kobo: &Kobo{},
Expand Down Expand Up @@ -122,7 +123,7 @@ func (b *Backend) DetectKobos() []Kobo {
b.logger.Error("Failed to detect any connected Kobos")
panic(err)
}
kobos := GetKoboMetadata(connectedKobos)
kobos := GetKoboMetadata(connectedKobos, b.logger)
b.logger.Info("Found one or more kobos",
"count", len(kobos),
)
Expand Down Expand Up @@ -159,6 +160,10 @@ func (b *Backend) SelectKobo(devicePath string) error {
}
}
if err := OpenConnection(b.SelectedKobo.DbPath); err != nil {
b.logger.Error("Failed to open DB connection",
slog.String("error", err.Error()),
slog.String("db_path", b.SelectedKobo.DbPath),
)
return err
}
return nil
Expand All @@ -185,7 +190,7 @@ func (b *Backend) PromptForLocalDBPath() error {
}

func (b *Backend) ForwardToReadwise() (int, error) {
highlightBreakdown := b.Kobo.CountDeviceBookmarks()
highlightBreakdown := b.Kobo.CountDeviceBookmarks(b.logger)
slog.Info("Got highlight counts from device",
slog.Int("highlight_count_sideload", int(highlightBreakdown.Sideloaded)),
slog.Int("highlight_count_official", int(highlightBreakdown.Official)),
Expand All @@ -200,22 +205,22 @@ func (b *Backend) ForwardToReadwise() (int, error) {
slog.Error("Tried to submit highlights with no sideloaded highlights + store-bought syncing disabled. Result is that no highlights would be fetched.")
return 0, fmt.Errorf("You have disabled store-bought syncing but you don't have any sideloaded highlights either. This combination means there are no highlights left to be synced.")
}
content, err := b.Kobo.ListDeviceContent(includeStoreBought)
content, err := b.Kobo.ListDeviceContent(includeStoreBought, b.logger)
if err != nil {
slog.Error("Received an error trying to list content from device",
slog.String("error", err.Error()),
)
return 0, err
}
contentIndex := b.Kobo.BuildContentIndex(content)
bookmarks, err := b.Kobo.ListDeviceBookmarks(includeStoreBought)
contentIndex := b.Kobo.BuildContentIndex(content, b.logger)
bookmarks, err := b.Kobo.ListDeviceBookmarks(includeStoreBought, b.logger)
if err != nil {
slog.Error("Received an error trying to list bookmarks from device",
slog.String("error", err.Error()),
)
return 0, err
}
payload, err := BuildPayload(bookmarks, contentIndex)
payload, err := BuildPayload(bookmarks, contentIndex, b.logger)
if err != nil {
slog.Error("Received an error trying to build Readwise payload",
slog.String("error", err.Error()),
Expand Down
12 changes: 8 additions & 4 deletions backend/log.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package backend

import (
"context"
"fmt"
"log/slog"
"os"
"time"

"github.com/sirupsen/logrus"
)

var logFileHandle *os.File
Expand All @@ -18,8 +17,6 @@ func StartLogger(portable bool, level slog.Leveler) (*slog.Logger, error) {
if err != nil {
panic("Failed to create location to store logfiles")
}

logrus.SetFormatter(&logrus.JSONFormatter{})
file, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
return &slog.Logger{}, err
Expand All @@ -35,3 +32,10 @@ func StartLogger(portable bool, level slog.Leveler) (*slog.Logger, error) {
func CloseLogFile() {
logFileHandle.Close()
}

// Dummy until real handler ships in Go 1.24
type discardHandler struct {
slog.TextHandler
}

func (d *discardHandler) Enabled(context.Context, slog.Level) bool { return false }
Loading

0 comments on commit 9dbe5ee

Please sign in to comment.