Implement stackprinter

This commit is contained in:
Keith Edmunds 2022-12-18 22:20:55 +00:00
parent 693e8f195d
commit bd9c8a84b9
5 changed files with 52 additions and 23 deletions

View File

@ -2,6 +2,7 @@
import logging
import logging.handlers
import stackprinter
import sys
import traceback
@ -55,25 +56,32 @@ stderr.addFilter(local_filter)
stderr.addFilter(debug_filter)
# create formatter and add it to the handlers
stderr_fmt = logging.Formatter('[%(asctime)s] %(leveltag)s: %(message)s',
datefmt='%H:%M:%S')
syslog_fmt = logging.Formatter(
'[%(name)s] %(module)s.%(funcName)s - %(leveltag)s: %(message)s'
)
stderr.setFormatter(stderr_fmt)
syslog.setFormatter(syslog_fmt)
# stderr_fmt = logging.Formatter('[%(asctime)s] %(leveltag)s: %(message)s',
# datefmt='%H:%M:%S')
# syslog_fmt = logging.Formatter(
# '[%(name)s] %(module)s.%(funcName)s - %(leveltag)s: %(message)s'
# )
# stderr.setFormatter(stderr_fmt)
# syslog.setFormatter(syslog_fmt)
class VerboseExceptionFormatter(logging.Formatter):
def formatException(self, exc_info):
msg = stackprinter.format(exc_info)
lines = msg.split('\n')
lines_indented = ["" + line + "\n" for line in lines]
msg_indented = "".join(lines_indented)
return msg_indented
stderr_fmt = '[%(asctime)s] %(leveltag)s: %(message)s'
stderr_formatter = VerboseExceptionFormatter(stderr_fmt, datefmt='%H:%M:%S')
stderr.setFormatter(stderr_formatter)
syslog_fmt = '[%(name)s] %(module)s.%(funcName)s - %(leveltag)s: %(message)s'
syslog_formatter = VerboseExceptionFormatter(syslog_fmt)
syslog.setFormatter(syslog_formatter)
# add the handlers to the log
log.addHandler(stderr)
log.addHandler(syslog)
def log_uncaught_exceptions(ex_cls, ex, tb):
print("\033[1;31;47m")
logging.critical(''.join(traceback.format_tb(tb)))
print("\033[1;37;40m")
logging.critical('{0}: {1}'.format(ex_cls, ex))
sys.excepthook = log_uncaught_exceptions

View File

@ -2,6 +2,7 @@
from log import log
import argparse
import stackprinter
import subprocess
import sys
import threading
@ -1557,6 +1558,13 @@ if __name__ == "__main__":
win = Window()
win.show()
sys.exit(app.exec())
except Exception:
msg = "Unhandled Exception caught by musicmuster.main()"
log.exception(msg, exc_info=True, stack_info=True)
except Exception as exc:
from helpers import send_mail
msg = stackprinter.format(exc)
send_mail(Config.ERRORS_TO, Config.ERRORS_FROM,
"Exception from musicmuster", msg)
print("\033[1;31;47mUnhandled exception starts\033[1;37;40m")
stackprinter.show(style="darkbg2")
print("\033[1;31;47mUnhandled exception ends\033[1;37;40m")

View File

@ -1,4 +1,5 @@
import re
import stackprinter
import subprocess
import threading
@ -1954,8 +1955,10 @@ class PlaylistTab(QTableWidget):
# FIXME temporary workaround to issue #147
try:
self.item(playlist_row.row_number, column).setText(new_text)
except AttributeError:
except AttributeError as exc:
msg = f"Issue 147 occurred. {playlist_row=}, {additional_text=}"
msg += "\n\n"
msg += stackprinter.format(exc)
helpers.send_mail(Config.ERRORS_TO, Confit.ERRORS_FROM,
"Issue #147 from musicmuster", msg)

11
poetry.lock generated
View File

@ -624,6 +624,14 @@ pure-eval = "*"
[package.extras]
tests = ["pytest", "typeguard", "pygments", "littleutils", "cython"]
[[package]]
name = "stackprinter"
version = "0.2.10"
description = "Debug-friendly stack traces, with variable values and semantic highlighting"
category = "main"
optional = false
python-versions = ">=3.4"
[[package]]
name = "text-unidecode"
version = "1.3"
@ -708,7 +716,7 @@ python-versions = "*"
[metadata]
lock-version = "1.1"
python-versions = "^3.9"
content-hash = "91e055875df86707e1ce1544b1d29126265011d750897912daa37af3fe005498"
content-hash = "0fdda77377246e18b5e85459fa2c26173f14467f32e71c576b30fa0899ced8b0"
[metadata.files]
alembic = [
@ -1073,6 +1081,7 @@ stack-data = [
{file = "stack_data-0.2.0-py3-none-any.whl", hash = "sha256:999762f9c3132308789affa03e9271bbbe947bf78311851f4d485d8402ed858e"},
{file = "stack_data-0.2.0.tar.gz", hash = "sha256:45692d41bd633a9503a5195552df22b583caf16f0b27c4e58c98d88c8b648e12"},
]
stackprinter = []
text-unidecode = []
thefuzz = []
tinytag = [

View File

@ -23,6 +23,7 @@ thefuzz = "^0.19.0"
python-Levenshtein = "^0.12.2"
pyfzf = "^0.3.1"
pydymenu = "^0.5.2"
stackprinter = "^0.2.10"
[tool.poetry.dev-dependencies]
ipdb = "^0.13.9"