@@ -13,8 +13,6 @@ import (
13
13
"runtime"
14
14
"strings"
15
15
16
- "github.com/sirupsen/logrus"
17
-
18
16
"github.com/pgaskin/koboutils/v2/kobo"
19
17
20
18
wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
@@ -34,10 +32,18 @@ type Backend struct {
34
32
portable bool
35
33
}
36
34
37
- func StartBackend (ctx * context.Context , version string , portable bool , logger * slog.Logger ) * Backend {
38
- settings , err := LoadSettings (portable )
35
+ func StartBackend (ctx * context.Context , version string , portable bool , logger * slog.Logger ) (* Backend , error ) {
36
+ settings , err := LoadSettings (portable , logger )
37
+ logger .Info ("Successfully parsed settings file" ,
38
+ slog .String ("path" , settings .path ),
39
+ slog .Bool ("upload_store_highlights" , settings .UploadStoreHighlights ),
40
+ slog .Bool ("upload_covers" , settings .UploadCovers ),
41
+ )
39
42
if err != nil {
40
- logrus .WithContext (* ctx ).WithError (err ).Error ("Failed to load settings" )
43
+ logger .Error ("Failed to load settings" ,
44
+ slog .String ("error" , err .Error ()),
45
+ )
46
+ return & Backend {}, err
41
47
}
42
48
return & Backend {
43
49
SelectedKobo : Kobo {},
@@ -50,9 +56,10 @@ func StartBackend(ctx *context.Context, version string, portable bool, logger *s
50
56
Kobo : & Kobo {},
51
57
Content : & Content {},
52
58
Bookmark : & Bookmark {},
59
+ logger : logger ,
53
60
version : version ,
54
61
portable : portable ,
55
- }
62
+ }, nil
56
63
}
57
64
58
65
func (b * Backend ) GetSettings () * Settings {
@@ -90,10 +97,20 @@ func (b *Backend) NavigateExplorerToLogLocation() {
90
97
if runtime .GOOS == "linux" {
91
98
explorerCommand = "xdg-open"
92
99
}
100
+ b .logger .Info ("Opening logs in system file explorer" ,
101
+ slog .String ("command" , explorerCommand ),
102
+ slog .String ("os" , runtime .GOOS ),
103
+ )
93
104
logLocation , err := LocateDataFile ("october/logs" , b .portable )
94
105
if err != nil {
95
- logrus .WithError (err ).Error ("Failed to determine XDG data location for opening log location in explorer" )
106
+ b .logger .Error ("Failed to determine XDG data location for opening log location in explorer" ,
107
+ slog .String ("error" , err .Error ()),
108
+ )
96
109
}
110
+ b .logger .Debug ("Executing command to open system file explorer" ,
111
+ slog .String ("command" , explorerCommand ),
112
+ slog .String ("os" , runtime .GOOS ),
113
+ )
97
114
// We will always get an error because the file explorer doesn't exit so it is unable to
98
115
// return a 0 successful exit code until y'know, the user exits the window
99
116
_ = exec .Command (explorerCommand , logLocation ).Run ()
@@ -102,10 +119,21 @@ func (b *Backend) NavigateExplorerToLogLocation() {
102
119
func (b * Backend ) DetectKobos () []Kobo {
103
120
connectedKobos , err := kobo .Find ()
104
121
if err != nil {
122
+ b .logger .Error ("Failed to detect any connected Kobos" )
105
123
panic (err )
106
124
}
107
125
kobos := GetKoboMetadata (connectedKobos )
126
+ b .logger .Info ("Found one or more kobos" ,
127
+ "count" , len (kobos ),
128
+ )
108
129
for _ , kb := range kobos {
130
+ b .logger .Info ("Found connected device" ,
131
+ slog .String ("mount_path" , kb .MntPath ),
132
+ slog .String ("database_path" , kb .DbPath ),
133
+ slog .String ("name" , kb .Name ),
134
+ slog .Int ("display_ppi" , kb .DisplayPPI ),
135
+ slog .Int ("storage" , kb .Storage ),
136
+ )
109
137
b .ConnectedKobos [kb .MntPath ] = kb
110
138
}
111
139
return kobos
@@ -119,6 +147,9 @@ func (b *Backend) SelectKobo(devicePath string) error {
119
147
if val , ok := b .ConnectedKobos [devicePath ]; ok {
120
148
b .SelectedKobo = val
121
149
} else {
150
+ b .logger .Info ("No device found at path. Selecting local database" ,
151
+ slog .String ("device_path" , devicePath ),
152
+ )
122
153
b .SelectedKobo = Kobo {
123
154
Name : "Local Database" ,
124
155
Storage : 0 ,
@@ -155,46 +186,81 @@ func (b *Backend) PromptForLocalDBPath() error {
155
186
156
187
func (b * Backend ) ForwardToReadwise () (int , error ) {
157
188
highlightBreakdown := b .Kobo .CountDeviceBookmarks ()
189
+ slog .Info ("Got highlight counts from device" ,
190
+ slog .Int ("highlight_count_sideload" , int (highlightBreakdown .Sideloaded )),
191
+ slog .Int ("highlight_count_official" , int (highlightBreakdown .Official )),
192
+ slog .Int ("highlight_count_total" , int (highlightBreakdown .Total )),
193
+ )
158
194
if highlightBreakdown .Total == 0 {
159
- logrus .Error ("Tried to submit highlights when there are none on device." )
195
+ slog .Error ("Tried to submit highlights when there are none on device." )
160
196
return 0 , fmt .Errorf ("Your device doesn't seem to have any highlights so there is nothing left to sync." )
161
197
}
162
198
includeStoreBought := b .Settings .UploadStoreHighlights
163
199
if ! includeStoreBought && highlightBreakdown .Sideloaded == 0 {
164
- logrus .Error ("Tried to submit highlights with no sideloaded highlights + store-bought syncing disabled. Result is that no highlights would be fetched." )
200
+ slog .Error ("Tried to submit highlights with no sideloaded highlights + store-bought syncing disabled. Result is that no highlights would be fetched." )
165
201
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." )
166
202
}
167
203
content , err := b .Kobo .ListDeviceContent (includeStoreBought )
168
204
if err != nil {
205
+ slog .Error ("Received an error trying to list content from device" ,
206
+ slog .String ("error" , err .Error ()),
207
+ )
169
208
return 0 , err
170
209
}
171
210
contentIndex := b .Kobo .BuildContentIndex (content )
172
211
bookmarks , err := b .Kobo .ListDeviceBookmarks (includeStoreBought )
173
212
if err != nil {
213
+ slog .Error ("Received an error trying to list bookmarks from device" ,
214
+ slog .String ("error" , err .Error ()),
215
+ )
174
216
return 0 , err
175
217
}
176
218
payload , err := BuildPayload (bookmarks , contentIndex )
177
219
if err != nil {
220
+ slog .Error ("Received an error trying to build Readwise payload" ,
221
+ slog .String ("error" , err .Error ()),
222
+ )
178
223
return 0 , err
179
224
}
180
225
numUploads , err := b .Readwise .SendBookmarks (payload , b .Settings .ReadwiseToken )
181
226
if err != nil {
227
+ slog .Error ("Received an error trying to send bookmarks to Readwise" ,
228
+ slog .String ("error" , err .Error ()),
229
+ )
182
230
return 0 , err
183
231
}
232
+ slog .Info ("Successfully uploaded bookmarks to Readwise" ,
233
+ slog .Int ("payload_count" , numUploads ),
234
+ )
184
235
if b .Settings .UploadCovers {
185
236
uploadedBooks , err := b .Readwise .RetrieveUploadedBooks (b .Settings .ReadwiseToken )
186
237
if err != nil {
238
+ slog .Error ("Failed to retrieve uploaded titles from Readwise" ,
239
+ slog .String ("error" , err .Error ()),
240
+ )
187
241
return numUploads , fmt .Errorf ("Successfully uploaded %d bookmarks" , numUploads )
188
242
}
243
+ slog .Info ("Retrieved uploaded books from Readwise for cover insertion" ,
244
+ slog .Int ("book_count" , uploadedBooks .Count ),
245
+ )
189
246
for _ , book := range uploadedBooks .Results {
247
+ slog .Info ("Checking cover status for book" ,
248
+ slog .Int ("book_id" , book .ID ),
249
+ slog .String ("book_title" , book .Title ),
250
+ )
190
251
// We don't want to overwrite user uploaded covers or covers already present
191
252
if ! strings .Contains (book .CoverURL , "uploaded_book_covers" ) {
192
253
coverID := kobo .ContentIDToImageID (book .SourceURL )
193
254
coverPath := kobo .CoverTypeLibFull .GeneratePath (false , coverID )
194
255
absCoverPath := path .Join (b .SelectedKobo .MntPath , "/" , coverPath )
195
256
coverBytes , err := os .ReadFile (absCoverPath )
196
257
if err != nil {
197
- logrus .WithError (err ).WithFields (logrus.Fields {"cover" : book .SourceURL , "location" : absCoverPath }).Warn ("Failed to load cover. Carrying on" )
258
+ slog .Warn ("Failed to load cover from disc. Skipping to next book." ,
259
+ slog .String ("error" , err .Error ()),
260
+ slog .String ("cover_path" , absCoverPath ),
261
+ slog .String ("cover_id" , book .SourceURL ),
262
+ )
263
+ continue
198
264
}
199
265
var base64Encoding string
200
266
mimeType := http .DetectContentType (coverBytes )
@@ -207,10 +273,17 @@ func (b *Backend) ForwardToReadwise() (int, error) {
207
273
base64Encoding += base64 .StdEncoding .EncodeToString (coverBytes )
208
274
err = b .Readwise .UploadCover (base64Encoding , book .ID , b .Settings .ReadwiseToken )
209
275
if err != nil {
210
- logrus .WithError (err ).WithField ("cover" , book .SourceURL ).Error ("Failed to upload cover to Readwise" )
276
+ slog .Error ("Failed to upload cover to Readwise. Skipping to next book." ,
277
+ slog .String ("error" , err .Error ()),
278
+ slog .String ("cover_url" , book .SourceURL ),
279
+ )
280
+ continue
211
281
}
212
- logrus .WithField ("cover" , book .SourceURL ).Debug ("Successfully uploaded cover to Readwise" )
282
+ slog .Debug ("Successfully uploaded cover to Readwise" ,
283
+ slog .String ("cover_url" , book .SourceURL ),
284
+ )
213
285
}
286
+ slog .Info ("Cover already exists for book. Skipping as we don't know if this was us prior or a user upload." )
214
287
}
215
288
}
216
289
return numUploads , nil
0 commit comments