From b9f38e7d1ffae5c8285b9962fa27cb1935675be0 Mon Sep 17 00:00:00 2001 From: Sambhav Kothari Date: Tue, 13 Feb 2018 21:20:01 +0530 Subject: [PATCH 1/2] Remove unnecessary code and use pathlib to figure out logger name --- picard/log.py | 72 ++++----------------------------------------------- 1 file changed, 5 insertions(+), 67 deletions(-) diff --git a/picard/log.py b/picard/log.py index 8833ea9ae..df68ef51c 100644 --- a/picard/log.py +++ b/picard/log.py @@ -17,11 +17,9 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. -import io import logging import os -import sys -import traceback +import pathlib from collections import deque, namedtuple, OrderedDict from PyQt5 import QtCore @@ -56,67 +54,8 @@ TailLogTuple = namedtuple( 'TailLogTuple', ['pos', 'message', 'level']) -def _DummyFn(*args, **kwargs): - """Placeholder function. - - Raises: - NotImplementedError - """ - _, _ = args, kwargs - raise NotImplementedError() - - -# _srcfile is used when walking the stack to check when we've got the first -# caller stack frame, by skipping frames whose filename is that of this -# module's source. It therefore should contain the filename of this module's -# source file. -_srcfile = os.path.normcase(_DummyFn.__code__.co_filename) -if hasattr(sys, '_getframe'): - def currentframe(): - return sys._getframe(3) -else: # pragma: no cover - def currentframe(): - """Return the frame object for the caller's stack frame.""" - try: - raise Exception - except Exception: - return sys.exc_info()[2].tb_frame.f_back - - class OurLogger(logging.getLoggerClass()): - - - # copied from https://github.com/python/cpython/blob/3.5/Lib/logging/__init__.py#L1353-L1381 - # see https://stackoverflow.com/questions/4957858/how-to-write-own-logging-methods-for-own-logging-levels - def findCaller(self, stack_info=False): - """ - Find the stack frame of the caller so that we can note the source - file name, line number and function name. - """ - f = currentframe() - # On some versions of IronPython, currentframe() returns None if - # IronPython isn't run with -X:Frames. - if f is not None: - f = f.f_back - rv = "(unknown file)", 0, "(unknown function)", None - while hasattr(f, "f_code"): - co = f.f_code - filename = os.path.normcase(co.co_filename) - if filename == _srcfile: - f = f.f_back - continue - sinfo = None - if stack_info: - sio = io.StringIO() - sio.write('Stack (most recent call last):\n') - traceback.print_stack(f, file=sio) - sinfo = sio.getvalue() - if sinfo[-1] == '\n': - sinfo = sinfo[:-1] - sio.close() - rv = (co.co_filename, f.f_lineno, co.co_name, sinfo) - break - return rv + pass class TailLogHandler(logging.Handler): @@ -165,11 +104,10 @@ main_logger.setLevel(logging.INFO) def name_filter(record): # provide a significant name, because module sucks - prefix = os.path.dirname(os.path.normcase(_srcfile)) - name = record.pathname + name, _ = os.path.splitext(os.path.normpath(record.pathname)) + prefix = os.path.normpath(__package__) if name.startswith(prefix): - name = name[len(prefix) + 1:].replace(os.path.sep, '.').replace('.__init__', '') - record.name, _ = os.path.splitext(name) + record.name = name[len(prefix) + 1:].replace(os.sep, ".").replace('.__init__', '') return True From 9d82269342b733fab471daf4c0d8c1fead312dc4 Mon Sep 17 00:00:00 2001 From: Sambhav Kothari Date: Wed, 14 Feb 2018 00:49:37 +0530 Subject: [PATCH 2/2] Add comment and fix logger name behaviour --- picard/log.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/picard/log.py b/picard/log.py index df68ef51c..1dafd8f3a 100644 --- a/picard/log.py +++ b/picard/log.py @@ -19,7 +19,6 @@ import logging import os -import pathlib from collections import deque, namedtuple, OrderedDict from PyQt5 import QtCore @@ -54,10 +53,6 @@ TailLogTuple = namedtuple( 'TailLogTuple', ['pos', 'message', 'level']) -class OurLogger(logging.getLoggerClass()): - pass - - class TailLogHandler(logging.Handler): def __init__(self, log_queue, tail_logger): @@ -95,19 +90,20 @@ class TailLogger(QtCore.QObject): # MAIN LOGGER -logging.setLoggerClass(OurLogger) - main_logger = logging.getLogger('main') main_logger.setLevel(logging.INFO) def name_filter(record): - # provide a significant name, because module sucks + # provide a significant name from the filepath of the module name, _ = os.path.splitext(os.path.normpath(record.pathname)) prefix = os.path.normpath(__package__) + # In case the module exists within picard, remove the picard prefix + # else, in case of something like a plugin, keep the path as it is. if name.startswith(prefix): - record.name = name[len(prefix) + 1:].replace(os.sep, ".").replace('.__init__', '') + name = name[len(prefix) + 1:].replace(os.sep, ".").replace('.__init__', '') + record.name = name return True