logs.py :  » Game-2D-3D » PyOpenGL » PyOpenGL-3.0.1 » OpenGL » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Game 2D 3D » PyOpenGL 
PyOpenGL » PyOpenGL 3.0.1 » OpenGL » logs.py
"""Fix missing-API problems in logging module (circa Python 2.3)

Adds constants to the log objects.
Adds getException(err) to log objects to retrieve 
formatted exception or err if traceback not available.
"""
try:
    from cStringIO import StringIO
except ImportError, err:
    from StringIO import StringIO
import traceback, logging

getLog = logging.getLogger
from OpenGL import ERROR_LOGGING,FULL_LOGGING

if not hasattr( traceback, 'format_exc' ):
    # Python 2.3 and below... do we care any more?
    def format_exc( limit ):
        file = StringIO()
        try:
            traceback.print_exc( limit=10, file = file )
            exception = file.getvalue()
        finally:
            file.close()
        return exception
else:
    format_exc = traceback.format_exc

def getException(error):
    """Get formatted traceback from exception"""
    try:
        return format_exc( limit=10 )
    except Exception, err:
        return str( error )

logging.Logger.getException = staticmethod( getException )
logging.Logger.err = logging.Logger.error
logging.Logger.DEBUG = logging.DEBUG 
logging.Logger.WARN = logging.WARN 
logging.Logger.INFO = logging.INFO 
logging.Logger.ERR = logging.Logger.ERROR = logging.ERROR

if FULL_LOGGING:
    getLog( 'OpenGL.calltrace' ).setLevel( logging.INFO )

class _LoggedFunction( object ):
    """Proxy that overrides __call__ to log arguments"""
    def __init__( self, base, log ):
        self.__dict__[''] = base 
        self.__dict__['log'] = log
    def __setattr__( self, key, value ):
        if key != '':
            setattr( self.__dict__[''], key, value )
        else:
            self.__dict__[''] = value 
    def __getattr__( self, key ):
        if key == '':
            return self.__dict__['']
        else:
            return getattr( self.__dict__[''], key )
class _FullLoggedFunction( _LoggedFunction ):
    """Fully-logged function wrapper (logs all call params to OpenGL.calltrace)"""
    _callTrace = getLog( 'OpenGL.calltrace' )
    def __call__( self, *args, **named ):
        argRepr = []
        function = getattr( self, '' )
        for arg in args:
            argRepr.append( repr(arg) )
        for key,value in named.items():
            argRepr.append( '%s = %s'%( key,repr(value)) )
        argRepr = ",".join( argRepr )
        self._callTrace.info( '%s( %s )', function.__name__, argRepr )
        try:
            return function( *args, **named )
        except Exception, err:
            self.log.warn(
                """Failure on %s: %s""", function.__name__, self.log.getException( err )
            )
            raise
class _ErrorLoggedFunction ( _LoggedFunction ):
    """On-error-logged function wrapper"""
    def __call__( self, *args, **named ):
        function = getattr( self, '' )
        try:
            return function( *args, **named )
        except Exception, err:
            self.log.warn(
                """Failure on %s: %s""", function.__name__, self.log.getException( err )
            )
            raise
    

def logOnFail( function, log ):
    """Produce possible log-wrapped version of function

    function -- callable object to be wrapped
    log -- the log to which to log information
    
    Uses ERROR_LOGGING and FULL_LOGGING
    to determine whether/how to wrap the function.
    """
    if ERROR_LOGGING or FULL_LOGGING:
        if FULL_LOGGING:
            loggedFunction = _FullLoggedFunction( function, log )
        else:
            loggedFunction = _ErrorLoggedFunction( function, log )
        return loggedFunction
    else:
        return function
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.