Changeset 7006


Ignore:
Timestamp:
May 11, 2009, 9:38:27 AM (11 years ago)
Author:
rwilson
Message:

Added code to capture and log unhandled exceptions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/utilities/log.py

    r6892 r7006  
    2424'''
    2525
    26 import sys
    2726import os
    2827import sys
     
    6059NOTSET = logging.NOTSET
    6160
    62 # get True if python version 2.5 or later
     61# set True if python version 2.5 or later
    6362(version_major, version_minor, _, _, _) = sys.version_info
    6463new_python = ((version_major == 2 and version_minor >= 5) or version_major > 2)
     
    103102        console.setFormatter(formatter)
    104103        logging.getLogger('').addHandler(console)
     104
     105        # catch exceptions
     106        sys.excepthook = log_exception_hook
    105107
    106108        # tell the world how we are set up
     
    122124    frames = traceback.extract_stack()
    123125    frames.reverse()
    124     (_, mod_name) = __name__.rsplit('.', 1)
     126    try:
     127        (_, mod_name) = __name__.rsplit('.', 1)
     128    except ValueError:
     129        mod_name = __name__
    125130    for (fpath, lnum, mname, _) in frames:
    126131        (fname, _) = os.path.basename(fpath).rsplit('.', 1)
     
    133138        logging.log(level, msg)
    134139
     140##
     141# @brief Hook function to process uncaught exceptions.
     142# @param type
     143# @param value
     144# @param traceback
     145# @note Same interface as sys.excepthook()
     146def log_exception_hook(type, value, tb):
     147    msg = ''.join(traceback.format_exception(type, value, tb))
     148    critical(msg)
     149
     150   
    135151################################################################################
    136152# Shortcut routines to make for simpler user code.
     
    219235        log(level, msg)
    220236    else:
    221         pass
     237        msg = ('Sorry, no memory statistics for Windows (yet).')
     238        log(level, msg)
     239
     240
     241if __name__ == '__main__':
     242##    critical('Testing exception capturing')
     243    resource_usage()
Note: See TracChangeset for help on using the changeset viewer.