Ignore:
Timestamp:
Jun 30, 2009, 2:07:41 PM (15 years ago)
Author:
ole
Message:

Merged numpy branch back into the trunk.

In ~/sandpit/anuga/anuga_core/source
svn merge -r 6246:HEAD ../../branches/numpy .

In ~/sandpit/anuga/anuga_validation
svn merge -r 6417:HEAD ../branches/numpy_anuga_validation .

In ~/sandpit/anuga/misc
svn merge -r 6809:HEAD ../branches/numpy_misc .

For all merges, I used numpy version where conflicts existed

The suites test_all.py (in source/anuga) and validate_all.py passed using Python2.5 with numpy on my Ubuntu Linux box.

File:
1 edited

Legend:

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

    r7036 r7276  
    77Use it this way:
    88    import anuga.utilities.log as log
    9     log.console_logging_level = log.DEBUG
     9
     10    # configure my logging
     11    log.console_logging_level = log.INFO
     12    log.log_logging_level = log.DEBUG
     13    log.log_filename('./my.log')
     14
     15    # log away!
    1016    log.debug('A message at DEBUG level')
    1117    log.info('Another message, INFO level')
    1218
    1319This class uses the 'borg' pattern - there is never more than one instance
    14 of log data.  See
     20of log data.  See the URL for the basic idea used here: modules *are*
     21singletons!
     22
    1523<http://www.suttoncourtenay.org.uk/duncan/accu/pythonpatterns.html>
    16 for the basic idea used here: modules *are* singletons!
    1724
    1825Until the first call to log() the user is free to play with the module data
    1926to configure the logging.
    2027
    21 Note that this module uses features of the logging package that were introduced
    22 in python2.5.  If running on earlier versions, these features are disabled:
     28Note that this module uses some features of the logging package that were
     29introduced in python2.5.  If running on earlier versions, the following
     30features are disabled:
    2331    . Calling module name + line number
    2432'''
     
    2836import traceback
    2937import logging
     38
     39DefaultConsoleLogLevel = logging.CRITICAL
     40DefaultFileLogLevel = logging.INFO
    3041
    3142
     
    3748# the console, but ensure that everything that goes to the console *will* also
    3849# appear in the log file.  There is code to ensure log <= console levels.
     50#
     51# If console logging level is set to CRITICAL+1 then nothing will print on the
     52# console.
    3953################################################################################
    4054
     
    4357
    4458# logging level for the console
    45 console_logging_level = logging.CRITICAL
     59console_logging_level = DefaultConsoleLogLevel
    4660
    4761# logging level for the logfile
    48 log_logging_level = logging.INFO
     62log_logging_level = DefaultFileLogLevel
    4963
    5064# The default name of the file to log to.
     
    5973NOTSET = logging.NOTSET
    6074
    61 # set new_python to True if python version 2.5 or later
    62 (version_major, version_minor, _, _, _) = sys.version_info
    63 new_python = ((version_major == 2 and version_minor >= 5) or version_major > 2)
     75# set _new_python to True if python version 2.5 or later
     76_new_python = (sys.version_info >= 0x02050000)      # 2.5.x.x
    6477
    6578
     
    6881################################################################################
    6982
    70 ##
    71 # @brief Log a message at a specified level.
    72 # @param level The loglevel to log with (logging.DEBUG, etc).
    73 # @param msg Message string to log.
    74 # @note First call of this method initializes the logging system.
    75 def log(level, msg):
     83def log(msg, level=None):
    7684    '''Log a message at a particular loglevel.
     85
     86    msg:    The message string to log.
     87    level:  The logging level to log with (defaults to console level).
    7788
    7889    The first call to this method (by anybody) initializes logging and
     
    89100
    90101        # setup the file logging system
    91         if new_python:
     102        if _new_python:
    92103            fmt = '%(asctime)s %(levelname)-8s %(mname)25s:%(lnum)-4d|%(message)s'
    93104        else:
     
    112123                        logging.getLevelName(log_logging_level),
    113124                        logging.getLevelName(console_logging_level)))
    114         if new_python:
     125        if _new_python:
    115126            logging.log(logging.CRITICAL, start_msg,
    116127                        extra={'mname': __name__, 'lnum': 0})
     
    120131        # mark module as *setup*
    121132        _setup = True
     133
     134    # if logging level not supplied, assume console level
     135    if level is None:
     136        level = console_logging_level
    122137
    123138    # get caller information - look back for first module != <this module name>
     
    133148            break
    134149
    135     if new_python:
     150    # why are we here? ... Oh yes! Log the message!
     151    if _new_python:
    136152        logging.log(level, msg, extra={'mname': fname, 'lnum': lnum})
    137153    else:
    138154        logging.log(level, msg)
    139155
    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()
     156
    146157def log_exception_hook(type, value, tb):
    147     msg = ''.join(traceback.format_exception(type, value, tb))
     158    '''Hook function to process uncaught exceptions.
     159
     160    type:   Type of exception.
     161    value:  The exception data.
     162    tb:     Traceback object.
     163
     164    This has the same interface as sys.excepthook().
     165    '''
     166
     167    msg = '\n' + ''.join(traceback.format_exception(type, value, tb))
    148168    critical(msg)
    149169
     
    153173################################################################################
    154174
    155 ##
    156 # @brief Shortcut for log(DEBUG, msg).
    157 # @param msg Message string to log at logging.DEBUG level.
    158175def debug(msg=''):
    159     log(logging.DEBUG, msg)
    160 
    161 ##
    162 # @brief Shortcut for log(INFO, msg).
    163 # @param msg Message string to log at logging.INFO level.
     176    '''Shortcut for log(DEBUG, msg).'''
     177
     178    log(msg, logging.DEBUG)
     179
     180
    164181def info(msg=''):
    165     log(logging.INFO, msg)
    166 
    167 ##
    168 # @brief Shortcut for log(WARNING, msg).
    169 # @param msg Message string to log at logging.WARNING level.
     182    '''Shortcut for log(INFO, msg).'''
     183
     184    log(msg, logging.INFO)
     185
     186
    170187def warning(msg=''):
    171     log(logging.WARNING, msg)
    172 
    173 ##
    174 # @brief Shortcut for log(ERROR, msg).
    175 # @param msg Message string to log at logging.ERROR level.
     188    '''Shortcut for log(WARNING, msg).'''
     189
     190    log(msg, logging.WARNING)
     191
     192
    176193def error(msg=''):
    177     log(logging.ERROR, msg)
    178 
    179 ##
    180 # @brief Shortcut for log(CRITICAL, msg).
    181 # @param msg Message string to log at logging.CRITICAL level.
     194    '''Shortcut for log(ERROR, msg).'''
     195
     196    log(msg, logging.ERROR)
     197
     198
    182199def critical(msg=''):
    183     log(logging.CRITICAL, msg)
    184 
    185 ##
    186 # @brief Log memory usage at time of call.
    187 # @param level Override the default INFO logging level.
    188 # @note From http://code.activestate.com/recipes/286222/.
     200    '''Shortcut for log(CRITICAL, msg).'''
     201
     202    log(msg, logging.CRITICAL)
     203
     204
    189205def resource_usage(level=logging.INFO):
    190206    '''Log memory usage at given log level.'''
    191207
     208    _scale = {'KB': 1024, 'MB': 1024*1024, 'GB': 1024*1024*1024,
     209              'kB': 1024, 'mB': 1024*1024, 'gB': 1024*1024*1024}
     210
    192211    if sys.platform != 'win32':
    193212        _proc_status = '/proc/%d/status' % os.getpid()
    194         _scale = {'KB': 1024, 'MB': 1024*1024, 'GB': 1024*1024*1024,
    195                   'kB': 1024, 'mB': 1024*1024, 'gB': 1024*1024*1024}
    196 
     213       
    197214        def _VmB(VmKey):
    198215            '''Get number of virtual bytes used.'''
     
    233250               % (memory()/_scale['MB'], resident()/_scale['MB'],
    234251                  stacksize()/_scale['MB']))
    235         log(level, msg)
     252        log(msg, level)
    236253    else:
    237         msg = ('Sorry, no memory statistics for Windows (yet).')
    238         log(level, msg)
    239 
     254        # Windows code from: http://code.activestate.com/recipes/511491/
     255        try:
     256            import ctypes
     257            import _winreg
     258        except:
     259            log(level, 'Windows resource usage not available')
     260            return
     261
     262        kernel32 = ctypes.windll.kernel32
     263        c_ulong = ctypes.c_ulong
     264        c_ulonglong = ctypes.c_ulonglong
     265        class MEMORYSTATUSEX(ctypes.Structure):
     266            _fields_ = [('dwLength', c_ulong),
     267                        ('dwMemoryLoad', c_ulong),
     268                        ('ullTotalPhys', c_ulonglong),
     269                        ('ullAvailPhys', c_ulonglong),
     270                        ('ullTotalPageFile', c_ulonglong),
     271                        ('ullAvailPageFile', c_ulonglong),
     272                        ('ullTotalVirtual', c_ulonglong),
     273                        ('ullAvailVirtual', c_ulonglong),
     274                        ('ullAvailExtendedVirtual', c_ulonglong)
     275                       ]
     276
     277        memoryStatusEx = MEMORYSTATUSEX()
     278        memoryStatusEx.dwLength = ctypes.sizeof(MEMORYSTATUSEX)
     279        kernel32.GlobalMemoryStatusEx(ctypes.byref(memoryStatusEx))
     280
     281        msg = ('Resource usage: total memory=%.1fMB free memory=%.1fMB'
     282               % (memoryStatusEx.ullTotalPhys/_scale['MB'],
     283                  memoryStatusEx.ullAvailPhys/_scale['MB']))
     284        log(msg, level)
     285
     286
     287################################################################################
    240288
    241289if __name__ == '__main__':
    242 ##    critical('Testing exception capturing')
     290    critical('#' * 80)
     291    warning('Test of logging...')
     292    log('CRITICAL+1', CRITICAL+1)
     293    log('CRITICAL', CRITICAL)
     294    log('CRITICAL-1', CRITICAL-1)
     295    log('CRITICAL-2', CRITICAL-2)
     296    log('default - CRITICAL?')
     297
    243298    def test_it(num=100):
    244299        if num > 0:
     
    251306    a = num.zeros((1000,1000), num.float)
    252307
     308    info('sys.version_info=%s, _new_python=%s'
     309         % (str(sys.version_info), str(_new_python)))
    253310    test_it()
Note: See TracChangeset for help on using the changeset viewer.