From 99e6326974680ef1ad09b2a47cefe399ab9de26a Mon Sep 17 00:00:00 2001 From: shortcutme Date: Thu, 19 Dec 2019 02:17:13 +0100 Subject: [PATCH] More compact stack logging --- src/Debug/Debug.py | 67 +++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 22 deletions(-) diff --git a/src/Debug/Debug.py b/src/Debug/Debug.py index 2e38683e..b5ac3627 100644 --- a/src/Debug/Debug.py +++ b/src/Debug/Debug.py @@ -22,6 +22,47 @@ def formatExceptionMessage(err): return "%s: %s" % (err_type, err_message) +python_lib_dir = os.path.dirname(os.__file__) + + +def formatTraceback(items, limit=None, fold_builtin=True): + back = [] + i = 0 + prev_file_title = "" + for path, line in items: + i += 1 + is_last = i == len(items) + dir_name, file_name = os.path.split(path.replace("\\", "/")) + + plugin_match = re.match(".*/plugins/(.+)$", dir_name) + if plugin_match: + file_title = "%s/%s" % (plugin_match.group(1), file_name) + is_prev_builtin = False + elif path.startswith(python_lib_dir): + if is_prev_builtin and not is_last and fold_builtin: + if back[-1] != "...": + back.append("...") + continue + else: + file_title = path.replace(python_lib_dir, "").replace("\\", "/").strip("/").replace("site-packages/", "") + is_prev_builtin = True + else: + file_title = file_name + is_prev_builtin = False + + if file_title == prev_file_title: + back.append("%s" % line) + else: + back.append("%s line %s" % (file_title, line)) + + prev_file_title = file_title + + if limit and i >= limit: + back.append("...") + break + return back + + def formatException(err=None, format="text"): import traceback if type(err) == Notify: @@ -38,35 +79,17 @@ def formatException(err=None, format="text"): else: err = exc_obj - tb = [] - for frame in traceback.extract_tb(exc_tb): - path, line, function, text = frame - dir_name, file_name = os.path.split(path.replace("\\", "/")) - plugin_match = re.match(".*/plugins/(.+)$", dir_name) - if plugin_match: - file_title = "%s/%s" % (plugin_match.group(1), file_name) - else: - file_title = file_name - tb.append("%s line %s" % (file_title, line)) + tb = formatTraceback([[frame[0], frame[1]] for frame in traceback.extract_tb(exc_tb)]) if format == "html": return "%s: %s
%s" % (repr(err), err, " > ".join(tb)) else: return "%s: %s in %s" % (exc_type.__name__, err, " > ".join(tb)) -def formatStack(limit=99): +def formatStack(limit=None): import inspect - back = [] - i = 0 - for stack in inspect.stack(): - i += 1 - frame, path, line, function, source, index = stack - file = os.path.split(path)[1] - back.append("%s line %s" % (file, line)) - if i > limit: - back.append("...") - break - return " > ".join(back) + tb = formatTraceback([[frame[1], frame[2]] for frame in inspect.stack()[1:]], limit=limit) + return " > ".join(tb) # Test if gevent eventloop blocks