After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 750686 - Improve the logging code
Improve the logging code
Status: RESOLVED FIXED
Product: gnome-music
Classification: Applications
Component: general
unspecified
Other All
: Normal normal
: ---
Assigned To: gnome-music-maint
gnome-music-maint
Depends on:
Blocks:
 
 
Reported: 2015-06-10 08:12 UTC by Mathieu Bridon
Modified: 2016-05-11 21:49 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Log better (13.09 KB, patch)
2015-06-10 08:12 UTC, Mathieu Bridon
committed Details | Review
Reduce the overhead of the @log decorator (2.01 KB, patch)
2015-06-10 08:12 UTC, Mathieu Bridon
committed Details | Review

Description Mathieu Bridon 2015-06-10 08:12:42 UTC
.
Comment 1 Mathieu Bridon 2015-06-10 08:12:46 UTC
Created attachment 304920 [details] [review]
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.
Comment 2 Mathieu Bridon 2015-06-10 08:12:51 UTC
Created attachment 304921 [details] [review]
Reduce the overhead of the @log decorator

With this, if the log level is set to anything else than logging.DEBUG,
the decorator does absolutely nothing, it just returns the decorated
function unmodified.

Given how often this decorator is used, this could improve performance
a bit, avoiding to compute the artefacts used in the debug log message
if they are not going to be outputted anyway.
Comment 3 Vadim Rutkovsky 2015-06-10 08:14:56 UTC
Agreed. We should also add a parameter to save the log directly to file in order to avoid 'gnome-music -d &> log' invocations
Comment 4 Mathieu Bridon 2015-06-10 08:26:09 UTC
Vadim, does your "agreed" mean I can push the patches?

I'm asking because usually maintainers for other GNOME projects will edit the patch status to "accepted-commitnow" when that's the case. :)
Comment 5 Vadim Rutkovsky 2015-06-10 08:33:43 UTC
(In reply to Mathieu Bridon from comment #4)
> Vadim, does your "agreed" mean I can push the patches?

Agreed on the idea itself, haven't reviewed the patch yet.

> I'm asking because usually maintainers for other GNOME projects will edit
> the patch status to "accepted-commitnow" when that's the case. :)

I'll take a look at this today and will push the patch soon
Comment 6 Mathieu Bridon 2015-06-10 08:50:34 UTC
(In reply to Vadim Rutkovsky from comment #5)
> (In reply to Mathieu Bridon from comment #4)
> > Vadim, does your "agreed" mean I can push the patches?
> 
> Agreed on the idea itself, haven't reviewed the patch yet.

Got it.

Glad I asked then, otherwise I would have pushed unreviewed patches. ^_^

> > I'm asking because usually maintainers for other GNOME projects will edit
> > the patch status to "accepted-commitnow" when that's the case. :)
> 
> I'll take a look at this today and will push the patch soon

Thanks!
Comment 9 Vadim Rutkovsky 2015-06-10 15:10:23 UTC
Pushed the patches (thanks, Mathieu!), will keep this open for 'write log to file' functionality
Comment 10 Marinus Schraal 2016-05-11 21:49:07 UTC
Suggested enhancement has been implemented, keeping this open for a minor enhancement seems counter-productive.

Logging to file would be nice to have, patches can be filed as a new bug.