More compact stack logging

This commit is contained in:
shortcutme 2019-12-19 02:17:13 +01:00
parent 50bbe47bf2
commit 99e6326974
No known key found for this signature in database
GPG key ID: 5B63BAE6CB9613AE

View file

@ -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<br><small class='multiline'>%s</small>" % (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