diff --git a/picard/log.py b/picard/log.py index 8833ea9ae..1dafd8f3a 100644 --- a/picard/log.py +++ b/picard/log.py @@ -17,11 +17,8 @@ # 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 from collections import deque, namedtuple, OrderedDict from PyQt5 import QtCore @@ -56,69 +53,6 @@ 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 - - class TailLogHandler(logging.Handler): def __init__(self, log_queue, tail_logger): @@ -156,20 +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 - prefix = os.path.dirname(os.path.normcase(_srcfile)) - name = record.pathname + # 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): - name = name[len(prefix) + 1:].replace(os.path.sep, '.').replace('.__init__', '') - record.name, _ = os.path.splitext(name) + name = name[len(prefix) + 1:].replace(os.sep, ".").replace('.__init__', '') + record.name = name return True