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

Small logging improvements #858

Merged
merged 4 commits into from
Mar 1, 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
4 changes: 2 additions & 2 deletions cozy/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def __init__(self, pkgdatadir: str):

def do_startup(self):
log.info(distro.linux_distribution(full_distribution_name=False))
log.info(f"Starting up cozy {__version__}")
log.info(f"libadwaita version: {Adw._version}")
log.info("Starting up cozy %s", __version__)
log.info("libadwaita version: %s", Adw._version)

self.ui = CozyUI(self.pkgdatadir, self, __version__)
Adw.Application.do_startup(self)
Expand Down
2 changes: 1 addition & 1 deletion cozy/architecture/profiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def wrap(*args):
time1 = time.perf_counter()
ret = f(*args)
time2 = time.perf_counter()
log.info('{:s} function took {:.3f} ms'.format(f.__name__, (time2-time1)*1000.0))
log.info('%s function took %.3f ms', f.__name__, (time2-time1)*1000.0)

return ret
return wrap
9 changes: 3 additions & 6 deletions cozy/control/artwork_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
q.execute()

def _on_importer_event(self, event, data):
if event == "scan":
if data == ScanStatus.STARTED:

Check failure on line 59 in cozy/control/artwork_cache.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (SIM102)

cozy/control/artwork_cache.py:58:9: SIM102 Use a single `if` statement instead of nested `if` statements
self.delete_artwork_cache()

def _create_artwork_cache(self, book, pixbuf, size):
Expand Down Expand Up @@ -88,8 +88,7 @@
resized_pixbuf.savev(file_path, "jpeg", ["quality", None], ["95"])
except Exception as e:
reporter.warning("artwork_cache", "Failed to save resized cache albumart")
log.warning("Failed to save resized cache albumart for following uuid: " + gen_uuid)
log.warning(e)
log.warning("Failed to save resized cache albumart for uuid %r: %s", gen_uuid, e)

return resized_pixbuf

Expand All @@ -98,7 +97,7 @@
if query.exists():
try:
uuid = query.first().uuid
except Exception as e:

Check failure on line 100 in cozy/control/artwork_cache.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F841)

cozy/control/artwork_cache.py:100:33: F841 Local variable `e` is assigned to but never used
reporter.error("artwork_cache", "load_pixbuf_from_cache: query exists but query.first().uuid crashed.")
return None
else:
Expand All @@ -124,10 +123,10 @@
path = self.get_album_art_path(book, size)

try:
if path:
pixbuf = GdkPixbuf.Pixbuf.new_from_file(path)
else:
pixbuf = None

Check failure on line 129 in cozy/control/artwork_cache.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (SIM108)

cozy/control/artwork_cache.py:126:13: SIM108 Use ternary operator `pixbuf = GdkPixbuf.Pixbuf.new_from_file(path) if path else None` instead of `if`-`else`-block
except Exception as e:
log.warning("Failed to load pixbuf from path: %s. Deleting file.", path)
log.debug(e)
Expand Down Expand Up @@ -170,8 +169,7 @@
pixbuf = loader.get_pixbuf()
except Exception as e:
reporter.warning("artwork_cache", "Could not get book cover from db.")
log.warning("Could not get cover for book " + book.name)
log.warning(e)
log.warning("Could not get cover for book %r: %s", book.name, e)

return pixbuf

Expand Down Expand Up @@ -209,8 +207,7 @@
cover_files = [f for f in os.listdir(directory)
if f.lower().endswith('.png') or f.lower().endswith(".jpg") or f.lower().endswith(".gif")]
except Exception as e:
log.warning("Could not open audiobook directory and look for cover files.")
log.warning(e)
log.warning("Could not open audiobook directory and look for cover files: %s", e)
for elem in (x for x in cover_files if os.path.splitext(x.lower())[0] == "cover"):
# find cover.[jpg,png,gif]
try:
Expand Down
3 changes: 1 addition & 2 deletions cozy/control/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@
db.connect(reuse_if_open=True)
except Exception as e:
reporter.exception("db", e)
log.error("Could not connect to database. ")
log.error(e)
log.error("Could not connect to database: %s", e)


def books():
Expand Down Expand Up @@ -92,10 +91,10 @@
query = Track.select().where(Track.id == book.position)
if book.position < 1:
track_items = get_tracks(book)
if len(track_items) > 0:
track = get_tracks(book)[0]
else:
track = None

Check failure on line 97 in cozy/control/db.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (SIM108)

cozy/control/db.py:94:9: SIM108 Use ternary operator `track = get_tracks(book)[0] if len(track_items) > 0 else None` instead of `if`-`else`-block
elif query.exists():
track = query.get()
else:
Expand Down
8 changes: 4 additions & 4 deletions cozy/control/filesystem_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
self._settings_view_model.add_listener(self.__on_settings_changed)

def init_offline_mode(self):
external_storage = []

Check failure on line 46 in cozy/control/filesystem_monitor.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F841)

cozy/control/filesystem_monitor.py:46:9: F841 Local variable `external_storage` is assigned to but never used
mounts = self.volume_monitor.get_mounts()
# go through all audiobook locations and test if they can be found in the mounts list

Expand All @@ -59,7 +59,7 @@
Free all references.
"""
# self.volume_monitor.unref()
pass

Check failure on line 62 in cozy/control/filesystem_monitor.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (PIE790)

cozy/control/filesystem_monitor.py:62:9: PIE790 Unnecessary `pass` statement

def get_book_online(self, book: Book):
try:
Expand Down Expand Up @@ -117,11 +117,11 @@
log.warning("Mount added but no mount_path is present. Skipping...")
return

log.debug("Volume mounted: " + mount_path)
log.debug("Volume mounted: %s", mount_path)

storage = next((s for s in self.external_storage if mount_path in s.storage.path), None)
if storage:
log.info("Storage online: " + mount_path)
log.info("Storage online: %s", mount_path)
storage.online = True
self.emit_event("storage-online", storage.storage.path)

Expand All @@ -136,11 +136,11 @@
log.warning("Mount removed but no mount_path is present. Skipping...")
return

log.debug("Volume unmounted: " + mount_path)
log.debug("Volume unmounted: %s", mount_path)

storage = next((s for s in self.external_storage if mount_path in s.storage.path), None)
if storage:
log.info("Storage offline: " + mount_path)
log.info("Storage offline: %s", mount_path)
storage.online = False
self.emit_event("storage-offline", storage.storage.path)

Expand Down
10 changes: 5 additions & 5 deletions cozy/control/offline_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from cozy.architecture.event_sender import EventSender
from cozy.control.application_directories import get_cache_dir
import cozy.tools as tools
import cozy.ui

Check failure on line 9 in cozy/control/offline_cache.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F401)

cozy/control/offline_cache.py:9:8: F401 `cozy.ui` imported but unused

from gi.repository import Gio

Expand Down Expand Up @@ -119,7 +119,7 @@

def get_cached_path(self, chapter: Chapter):
query = OfflineCacheModel.select().where(OfflineCacheModel.original_file == chapter.file_id,
OfflineCacheModel.copied == True)

Check failure on line 122 in cozy/control/offline_cache.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E712)

cozy/control/offline_cache.py:122:78: E712 Comparison to `True` should be `cond is True` or `if cond:`
if query.count() > 0:
return os.path.join(self.cache_dir, query.get().cached_file)
else:
Expand Down Expand Up @@ -163,7 +163,7 @@
self.thread.start()

def _process_queue(self):
log.info("Started processing queue")
log.info("Started processing offline cache queue")
self.filecopy_cancel = Gio.Cancellable()

self._fill_queue_from_db()
Expand All @@ -174,12 +174,13 @@
self.emit_event_main_thread("start")

while len(self.queue) > 0:
log.info("Processing item")
self.current_batch_count += 1
item = self.queue[0]
if self.thread.stopped():
break

log.info("Processing item: %r", item)

query = OfflineCacheModel.select().where(OfflineCacheModel.id == item.id)
if not query.exists():
continue
Expand All @@ -192,7 +193,7 @@
self.current_book_processing = book.id

if not new_item.copied and os.path.exists(new_item.original_file.path):
log.info("Copying item")
log.info("Copying item: %r", new_item)
self.emit_event_main_thread("message",
_("Copying") + " " + tools.shorten_string(book.name, 30))
self.current = new_item
Expand All @@ -208,8 +209,7 @@
self.thread.stop()
break
reporter.exception("offline_cache", e)
log.error("Could not copy file to offline cache: " + new_item.original_file.path)
log.error(e)
log.error("Could not copy file %r to offline cache: %s", new_item.original_file.path, e)
self.queue.remove(item)
continue

Expand Down
4 changes: 1 addition & 3 deletions cozy/media/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import logging
import os
import time
import traceback
from enum import Enum, auto
from multiprocessing.pool import Pool as Pool
from typing import List, Set
Expand Down Expand Up @@ -115,9 +114,8 @@ def _execute_import(self, files_to_scan: List[str]) -> (Set[str], Set[str]):
try:
self._database_importer.insert_many(media_files)
except Exception as e:
log.error("Error while inserting new tracks to the database")
log.exception("Error while inserting new tracks to the database")
reporter.exception("importer", e)
log.error(traceback.format_exc())
self._toast.show("{}: {}".format(_("Error while importing new files"), str(e.__class__)))

if self._progress >= self._files_count:
Expand Down
Loading