[gnue] r9766 - in trunk/gnue-common/src: apps base

[email protected]
Newsgroups gmane.comp.cms.gnue.cvs
Message-ID <[email protected]>
Author: reinhard
Date: 2007-07-24 17:26:45 -0500 (Tue, 24 Jul 2007)
New Revision: 9766

Modified:
   trunk/gnue-common/src/apps/GBaseApp.py
   trunk/gnue-common/src/base/log.py
Log:
Added exception hook.

issue123 in-progress


Modified: trunk/gnue-common/src/apps/GBaseApp.py
===================================================================
--- trunk/gnue-common/src/apps/GBaseApp.py	2007-07-24 20:56:18 UTC (rev 9765)
+++ trunk/gnue-common/src/apps/GBaseApp.py	2007-07-24 22:26:45 UTC (rev 9766)
@@ -39,6 +39,7 @@
 
 from gnue import paths
 
+from gnue.common.base import log
 from gnue.common.apps import i18n, errors, GConfig, GDebug
 from gnue.common.datasources import GConnections
 from gnue.common.apps.CommandOption import CommandOption
@@ -759,6 +760,7 @@
     overriden by a descendant.
     """
     sys.excepthook = sys.__excepthook__
+    log.excepthook(etype, value, traceback)
     self._showException (*errors.getException (None, etype, value, traceback))
     sys.excepthook = self.excepthook
 

Modified: trunk/gnue-common/src/base/log.py
===================================================================
--- trunk/gnue-common/src/base/log.py	2007-07-24 20:56:18 UTC (rev 9765)
+++ trunk/gnue-common/src/base/log.py	2007-07-24 22:26:45 UTC (rev 9766)
@@ -32,6 +32,8 @@
 import inspect
 import logging
 import logging.config
+import sys
+import traceback
 
 from gnue.common.base import utils
 
@@ -43,7 +45,6 @@
         'critical_n', 'exception_n']
 
 # TODO:
-# - Exception hook
 # - allow for __gnue_logger__ module global variable to use as a logger name
 #   instead of the module name (make a function in utils.py to search and cache
 #   module global variables in the module hierarchy)
@@ -292,6 +293,31 @@
 
 
 # -----------------------------------------------------------------------------
+# Exception hooks
+# -----------------------------------------------------------------------------
+
+def excepthook(etype, evalue, etraceback):
+    """
+    Exception hook to log an exception to the "exception" logger family.
+
+    @param etype: Exception class
+    @type etype: class
+    @param evalue: Exception instance
+    @type evalue: instance
+    @param etraceback: Exception traceback
+    @type etraceback: traceback
+    """
+    # TODO: log to exception.system, exception.admin, or exception.application,
+    # depending on the exception group of gException.
+    # TODO: use getException to receive exception details instead of formatting
+    # the exception here. Could be implemented by overwriting
+    # Formatter.formatException().
+    # TODO: use a separate logger per exception class, like
+    # exception.admin.gnue.common.datasources.GDataSource.ResourceNotFoundError?
+    error_n('exception', "Unhandled exception occured",
+            exc_info=(etype, evalue, etraceback))
+
+# -----------------------------------------------------------------------------
 # Log to the logger named after the calling module
 # -----------------------------------------------------------------------------
 
@@ -378,6 +404,9 @@
     """
     Log an exception to the logger named after the calling module.
 
+    This function can be used in a try...except block to log an exception
+    before it is discarded.
+
     @param msg: Message to log.
     @type msg: string or unicode
     @return: always True, so the call can be prefixed with assert.
@@ -497,6 +526,9 @@
     """
     Log an exception to the logger defined through the C{name} parameter.
 
+    This function can be used in a try...except block to log an exception
+    before it is discarded.
+
     @param name: Logger name to log to.
     @type name: string
     @param msg: Message to log.
@@ -533,7 +565,22 @@
             frame = frame.f_back
         return ("(unknown file)", 0, "(unknown function)")
 
+    def makeRecord(self, name, level, fn, lno, msg, args, exc_info, *vargs):
+        """
+        Create the LogRecord.
 
+        In case of an exception, the filename, line number and function name of
+        the code line causing the exception is substituted.
+        """
+        if exc_info is not None:
+            (fn, lno, func, text) = traceback.extract_tb(exc_info[2])[-1]
+            # This parameter only exists with Python 2.5+
+            if vargs:
+                vargs[0] = func
+        return logging.Logger.makeRecord(self, name, level, fn, lno, msg, args,
+                exc_info, *vargs)
+
+
 # -----------------------------------------------------------------------------
 # Module initialization
 # -----------------------------------------------------------------------------
@@ -551,3 +598,6 @@
     logging.config.fileConfig(_CONFIG_FILES)
 else:
     logging.basicConfig()
+
+# Use our exception hook
+sys.excepthook = excepthook
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.