From e409859c35f20ef82b907d392748c9e6b3aa9db6 Mon Sep 17 00:00:00 2001 From: Bas Stottelaar Date: Tue, 1 Apr 2014 16:08:29 +0200 Subject: [PATCH] Add excepthook for verbose. Bugfixes Enabled exception hook for all cases --- Headphones.py | 2 +- headphones/__init__.py | 4 ++-- headphones/logger.py | 20 ++++++++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/Headphones.py b/Headphones.py index f982b2e9..480caf08 100755 --- a/Headphones.py +++ b/Headphones.py @@ -176,7 +176,7 @@ def main(): except KeyboardInterrupt: headphones.SIGNAL = 'shutdown' else: - logger.info('Received signal: %d', headphones.SIGNAL) + logger.info('Received signal: %s', headphones.SIGNAL) if headphones.SIGNAL == 'shutdown': headphones.shutdown() elif headphones.SIGNAL == 'restart': diff --git a/headphones/__init__.py b/headphones/__init__.py index ff7f8622..acdffb8e 100644 --- a/headphones/__init__.py +++ b/headphones/__init__.py @@ -775,8 +775,8 @@ def daemonize(): os.dup2(so.fileno(), sys.stdout.fileno()) os.dup2(se.fileno(), sys.stderr.fileno()) - pid = str(os.getpid()) - logger.info('Daemonized to PID: %s' % pid) + pid = os.getpid() + logger.info('Daemonized to PID: %d', pid) if CREATEPID: logger.info("Writing PID %d to %s", pid, PIDFILE) diff --git a/headphones/logger.py b/headphones/logger.py index f81ff405..a08a50c0 100644 --- a/headphones/logger.py +++ b/headphones/logger.py @@ -14,7 +14,9 @@ # along with Headphones. If not, see . import os +import sys import logging +import traceback import headphones from logging import handlers @@ -79,6 +81,24 @@ def initLogger(verbose=1): logger.addHandler(console_handler) + # Any exceptions uncaught will pass through this handle + sys.excepthook = excepthook + +def excepthook(*exception_info): + """ + Log uncaught exceptions via the logger.error() method. This is especially + useful for daemons. + """ + + # We should always catch this to prevent loops! + try: + logger.error("Uncaught excaption: %s", traceback.print_exception(*exception_info)) + except: + pass + + # Original excepthook + sys.__excepthook__(*exception_info) + # Expose logger methods info = logger.info warn = logger.warn