[gnome-music/gnome-3-16] Log better



commit eb4b2db2b9f350f45eab591bea3dde6a0687ec5e
Author: Mathieu Bridon <bochecha daitauha fr>
Date:   Tue Jun 9 17:43:18 2015 +0200

    Log better
    
    There are two ways in Python to log a message with the logging module.
    Either you pass a fully formatted message:
    
        logger.debug("Error: %s" % e)
    
    Or you pass a format string followed by arguments, in the printf style:
    
        logger.debug("Error: %s", e)
    
    The first one looks more idiomatic to Python, as it's how we generally
    output strings with the print() function.
    
    However, there is a difference between the two: with the second method,
    the string is formatted inside the logging method, **after** the level
    is checked.
    
    That means that if the level for the logger was set to logging.WARNING,
    the message as passed to logger.debug() in the first example above would
    be formatted anyway, before calling logger.debug(), which would discard
    the string anyway.
    
    But in the second example, logger.debug() would first check that the
    level is set to logger.WARNING, and just return directly, so that the
    string would not get formatted for nothing.
    
    This is a potential performance boost, for objects with a costly
    __str__() method, or for repeated calls of logger.debug() when the level
    is set to something higher.
    
    And in gnome-music, pretty much every function call will generate a call
    to logger.debug() because of the @log decorator. This in itself could
    end up being quite costly, but formatting the strings in advance (the
    first example above) makes it even worse.
    
    This patch moves all the logging calls from the first style above to the
    second one.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=750686

 gnomemusic/__init__.py      |    6 +++---
 gnomemusic/albumArtCache.py |   20 ++++++++++----------
 gnomemusic/grilo.py         |   14 +++++++-------
 gnomemusic/player.py        |   12 ++++++------
 gnomemusic/query.py         |    4 ++--
 gnomemusic/widgets.py       |    2 +-
 gnomemusic/window.py        |    2 +-
 7 files changed, 30 insertions(+), 30 deletions(-)
---
diff --git a/gnomemusic/__init__.py b/gnomemusic/__init__.py
index a4eb16e..9666dd7 100644
--- a/gnomemusic/__init__.py
+++ b/gnomemusic/__init__.py
@@ -40,11 +40,11 @@ def log(fn):
         module = fn.__module__
         params = ", ".join(map(repr, chain(v, k.values())))
 
-        logger.debug("%s%s.%s(%s)" % ('|' * tabbing, module, name, params))
+        logger.debug("%s%s.%s(%s)", '|' * tabbing, module, name, params)
         tabbing += 1
         retval = fn(*v, **k)
         tabbing -= 1
-        logger.debug("%sreturned %s" % ('|' * tabbing, retval))
+        logger.debug("%sreturned %s", '|' * tabbing, retval)
 
         return retval
     return wrapped
@@ -57,7 +57,7 @@ class TrackerWrapper:
                 self.tracker = Tracker.SparqlConnection.get(None)
             except Exception as e:
                 from sys import exit
-                logger.error("Cannot connect to tracker, error '%s'\Exiting" % str(e))
+                logger.error("Cannot connect to tracker, error '%s'\Exiting", str(e))
                 exit(1)
 
         def __str__(self):
diff --git a/gnomemusic/albumArtCache.py b/gnomemusic/albumArtCache.py
index 05fb8ca..bdf97c2 100644
--- a/gnomemusic/albumArtCache.py
+++ b/gnomemusic/albumArtCache.py
@@ -124,7 +124,7 @@ class AlbumArtCache:
                 item.join(30)
                 self.thread_queue.task_done()
             except Exception as e:
-                logger.warn("worker %d item %s: error %s" % (id, item, str(e)))
+                logger.warn("worker %d item %s: error %s", id, item, str(e))
 
     @log
     def __init__(self):
@@ -133,7 +133,7 @@ class AlbumArtCache:
             if not os.path.exists(self.cacheDir):
                 Gio.file_new_for_path(self.cacheDir).make_directory(None)
         except Exception as e:
-            logger.warn("Error: %s" % e)
+            logger.warn("Error: %s", e)
 
         try:
             self.thread_queue = Queue()
@@ -142,7 +142,7 @@ class AlbumArtCache:
                 t.setDaemon(True)
                 t.start()
         except Exception as e:
-            logger.warn("Error: %s" % e)
+            logger.warn("Error: %s", e)
 
     @log
     def get_default_icon(self, width, height, is_loading=False):
@@ -187,7 +187,7 @@ class AlbumArtCache:
             t = Thread(target=self.lookup_worker, args=(item, width, height, callback, itr, artist, album))
             self.thread_queue.put(t)
         except Exception as e:
-            logger.warn("Error: %s, %s" % (e.__class__, e))
+            logger.warn("Error: %s, %s", e.__class__, e)
 
     @log
     def lookup_worker(self, item, width, height, callback, itr, artist, album):
@@ -212,7 +212,7 @@ class AlbumArtCache:
             pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_scale(path, width, height, True)
             self.finish(item, _make_icon_frame(pixbuf), path, callback, itr, width, height)
         except Exception as e:
-            logger.warn("Error: %s" % e)
+            logger.warn("Error: %s", e)
 
     @log
     def finish(self, item, pixbuf, path, callback, itr, width=-1, height=-1, artist=None, album=None):
@@ -230,7 +230,7 @@ class AlbumArtCache:
                 item.set_thumbnail(GLib.filename_to_uri(path, None))
             GLib.idle_add(callback, pixbuf, path, itr)
         except Exception as e:
-            logger.warn("Error: %s" % e)
+            logger.warn("Error: %s", e)
 
     @log
     def cached_thumb_not_found(self, item, width, height, path, callback, itr, artist, album):
@@ -244,7 +244,7 @@ class AlbumArtCache:
             t = Thread(target=self.download_worker, args=(item, width, height, path, callback, itr, artist, 
album, uri))
             self.thread_queue.put(t)
         except Exception as e:
-            logger.warn("Error: %s" % e)
+            logger.warn("Error: %s", e)
             self.finish(item, None, None, callback, itr, width, height, artist, album)
 
     @log
@@ -256,14 +256,14 @@ class AlbumArtCache:
 
             uri = item.get_thumbnail()
             if uri is None:
-                logger.warn("can't find artwork for album '%s' by %s" % (album, artist))
+                logger.warn("can't find artwork for album '%s' by %s", album, artist)
                 self.finish(item, None, None, callback, itr, width, height, artist, album)
                 return
 
             t = Thread(target=self.download_worker, args=(item, width, height, path, callback, itr, artist, 
album, uri))
             self.thread_queue.put(t)
         except Exception as e:
-            logger.warn("Error: %s" % e)
+            logger.warn("Error: %s", e)
             self.finish(item, None, None, callback, itr, width, height, artist, album)
 
     @log
@@ -274,5 +274,5 @@ class AlbumArtCache:
             src.copy(dest, Gio.FileCopyFlags.OVERWRITE)
             self.lookup_worker(item, width, height, callback, itr, artist, album)
         except Exception as e:
-            logger.warn("Error: %s" % e)
+            logger.warn("Error: %s", e)
             self.finish(item, None, None, callback, itr, width, height, artist, album)
diff --git a/gnomemusic/grilo.py b/gnomemusic/grilo.py
index ec76e1b..2da0e04 100644
--- a/gnomemusic/grilo.py
+++ b/gnomemusic/grilo.py
@@ -117,12 +117,12 @@ class Grilo(GObject.GObject):
                         try:
                             self.changed_media_ids.append(media.get_id())
                         except Exception as e:
-                            logger.warn("Skipping %s" % media)
+                            logger.warn("Skipping %s", media)
 
                 if self.changed_media_ids == []:
                     return
                 self.changed_media_ids = list(set(self.changed_media_ids))
-                logger.debug("Changed medias: %s" % self.changed_media_ids)
+                logger.debug("Changed medias: %s", self.changed_media_ids)
 
                 if len(self.changed_media_ids) >= self.CHANGED_MEDIA_MAX_ITEMS:
                     self.emit_change_signal()
@@ -132,7 +132,7 @@ class Grilo(GObject.GObject):
                         self.pending_event_id = 0
                     self.pending_event_id = GLib.timeout_add(self.CHANGED_MEDIA_SIGNAL_TIMEOUT, 
self.emit_change_signal)
         except Exception as e:
-            logger.warn("Exception in _on_content_changed: %s" % e)
+            logger.warn("Exception in _on_content_changed: %s", e)
 
     @log
     def emit_change_signal(self):
@@ -147,7 +147,7 @@ class Grilo(GObject.GObject):
     @log
     def _on_source_added(self, pluginRegistry, mediaSource):
         id = mediaSource.get_id()
-        logger.debug("new grilo source %s was added" % id)
+        logger.debug("new grilo source %s was added", id)
         try:
             ops = mediaSource.supported_operations()
 
@@ -165,18 +165,18 @@ class Grilo(GObject.GObject):
                             'content-changed', self._on_content_changed)
 
             elif (id.startswith('grl-upnp')):
-                logger.debug("found upnp source %s" % id)
+                logger.debug("found upnp source %s", id)
                 self.sources[id] = mediaSource
                 self.emit('new-source-added', mediaSource)
 
             elif (id not in self.blacklist) and (ops & Grl.SupportedOps.SEARCH) and \
                  (mediaSource.get_supported_media() & Grl.MediaType.AUDIO):
-                logger.debug("source %s is searchable" % id)
+                logger.debug("source %s is searchable", id)
                 self.sources[id] = mediaSource
                 self.emit('new-source-added', mediaSource)
 
         except Exception as e:
-            logger.debug("Source %s: exception %s" % (id, e))
+            logger.debug("Source %s: exception %s", id, e)
 
     @log
     def _on_source_removed(self, pluginRegistry, mediaSource):
diff --git a/gnomemusic/player.py b/gnomemusic/player.py
index c4f5985..f60b325 100644
--- a/gnomemusic/player.py
+++ b/gnomemusic/player.py
@@ -170,11 +170,11 @@ class Player(GObject.GObject):
     def discover_item(self, item, callback, data=None):
         url = item.get_url()
         if not url:
-            logger.warn("The item %s doesn't have a URL set" % item)
+            logger.warn("The item %s doesn't have a URL set", item)
             return
 
         if not url.startswith("file://"):
-            logger.debug("Skipping discovery of %s as not a local file" % url)
+            logger.debug("Skipping discovery of %s as not a local file", url)
             return
 
         obj = (callback, data)
@@ -223,13 +223,13 @@ class Player(GObject.GObject):
             uri = media.get_url()
         else:
             uri = 'none'
-        logger.warn('URI: ' + uri)
+        logger.warn('URI: %s', uri)
         error, debug = message.parse_error()
         debug = debug.split('\n')
         debug = [('     ') + line.lstrip() for line in debug]
         debug = '\n'.join(debug)
-        logger.warn('Error from element ' + message.src.get_name() + ': ' + error.message)
-        logger.warn('Debugging info:\n' + debug)
+        logger.warn('Error from element %s: %s', message.src.get_name(), error.message)
+        logger.warn('Debugging info:\n%s', debug)
         self.play_next()
         return True
 
@@ -753,7 +753,7 @@ class Player(GObject.GObject):
                         playlists.update_last_played(just_played_url)
                         playlists.update_all_static_playlists()
             except Exception as e:
-                logger.warn("Error: %s, %s" % (e.__class__, e))
+                logger.warn("Error: %s, %s", e.__class__, e)
         return True
 
     @log
diff --git a/gnomemusic/query.py b/gnomemusic/query.py
index fa51184..0c32305 100644
--- a/gnomemusic/query.py
+++ b/gnomemusic/query.py
@@ -49,10 +49,10 @@ class Query():
 
         for folder in [music_folder, download_folder]:
             if os.path.islink(folder):
-                logger.warn("%s is a symlink, this folder will be omitted" % folder)
+                logger.warn("%s is a symlink, this folder will be omitted", folder)
             else:
                 i = len(next(os.walk(folder))[2])
-                logger.debug("Found %d files in %s" % (i, folder))
+                logger.debug("Found %d files in %s", i, folder)
     except TypeError:
         logger.warn("XDG user dirs are not set")
 
diff --git a/gnomemusic/widgets.py b/gnomemusic/widgets.py
index 2010873..bffce41 100644
--- a/gnomemusic/widgets.py
+++ b/gnomemusic/widgets.py
@@ -51,7 +51,7 @@ try:
     MAX_TITLE_WIDTH = settings.get_int('max-width-chars')
 except Exception as e:
     MAX_TITLE_WIDTH = 20
-    logger.error("Error on setting widget max-width-chars: %s" % str(e))
+    logger.error("Error on setting widget max-width-chars: %s", str(e))
 
 playlists = Playlists.get_default()
 
diff --git a/gnomemusic/window.py b/gnomemusic/window.py
index 7129ac7..ef7c2b5 100644
--- a/gnomemusic/window.py
+++ b/gnomemusic/window.py
@@ -207,7 +207,7 @@ class Window(Gtk.ApplicationWindow):
                 if cursor is not None and cursor.next(None):
                     count = cursor.get_integer(0)
             except Exception as e:
-                logger.error("Tracker query crashed: %s" % e)
+                logger.error("Tracker query crashed: %s", e)
                 count = 0
 
             if count > 0:


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]