From 36e174f8f34d841d147e900007ccddad07ee0eca Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sat, 9 Feb 2019 12:41:02 +0100 Subject: [PATCH] donate-cpu-server.py: Add logging of uncaught exceptions (to file and console) (#1646) Two logging handler are added. One just prints all output with at least INFO severity to the console. The other only prints ERROR severity and above to a rotating file. The file size is limited to 100 kB. Since one backup file is used that results in a maximum of 200 kB disk usage. The log file is saved in the directory where the server script is. Hopefully this way some issues can be found more easily. Tested locally. --- tools/donate-cpu-server.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tools/donate-cpu-server.py b/tools/donate-cpu-server.py index 45667d925..6ec97288f 100644 --- a/tools/donate-cpu-server.py +++ b/tools/donate-cpu-server.py @@ -10,10 +10,40 @@ import time from threading import Thread import sys import urllib +import logging +import logging.handlers OLD_VERSION = '1.86' +# Set up logging +logger = logging.getLogger() +logger.setLevel(logging.INFO) +# Logging to console +handler_stream = logging.StreamHandler() +logger.addHandler(handler_stream) +# Log errors to a rotating file +logfile = sys.path[0] +if logfile: + logfile += '/' +logfile += 'donate-cpu-server.log' +handler_file = logging.handlers.RotatingFileHandler(filename=logfile, maxBytes=100*1024, backupCount=1) +handler_file.setLevel(logging.ERROR) +logger.addHandler(handler_file) + + +# Set up an exception hook for all uncaught exceptions so they can be logged +def handle_uncaught_exception(exc_type, exc_value, exc_traceback): + if issubclass(exc_type, KeyboardInterrupt): + sys.__excepthook__(exc_type, exc_value, exc_traceback) + return + + logging.error("Uncaught exception", exc_info=(exc_type, exc_value, exc_traceback)) + + +sys.excepthook = handle_uncaught_exception + + def strDateTime(): d = datetime.date.strftime(datetime.datetime.now().date(), '%Y-%m-%d') t = datetime.time.strftime(datetime.datetime.now().time(), '%H:%M')