home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / lib / site-packages / OpenGL / logs.py < prev    next >
Encoding:
Python Source  |  2008-12-07  |  2.8 KB  |  100 lines

  1. """Fix silly lack-of-API problems in logging module
  2.  
  3. Adds constants to the log objects.
  4. Adds getException(err) to log objects to retrieve 
  5. formatted exception or err if traceback not available.
  6. """
  7. try:
  8.     from cStringIO import StringIO
  9. except ImportError, err:
  10.     from StringIO import StringIO
  11. import traceback, logging
  12.  
  13. getLog = logging.getLogger
  14. from OpenGL import ERROR_LOGGING, FULL_LOGGING
  15.  
  16. def getException(error):
  17.     """Get formatted traceback from exception"""
  18.     exception = str(error)
  19.     file = StringIO()
  20.     try:
  21.         traceback.print_exc( limit=10, file = file )
  22.         exception = file.getvalue()
  23.     finally:
  24.         file.close()
  25.     return exception
  26. logging.Logger.getException = staticmethod( getException )
  27. logging.Logger.err = logging.Logger.error
  28. logging.Logger.DEBUG = logging.DEBUG 
  29. logging.Logger.WARN = logging.WARN 
  30. logging.Logger.INFO = logging.INFO 
  31. logging.Logger.ERR = logging.Logger.ERROR = logging.ERROR
  32.  
  33. if FULL_LOGGING:
  34.     getLog( 'OpenGL.calltrace' ).setLevel( logging.INFO )
  35.  
  36. class _LoggedFunction( object ):
  37.     """Proxy that overrides __call__ to log arguments"""
  38.     def __init__( self, base, log ):
  39.         self.__dict__[''] = base 
  40.         self.__dict__['log'] = log
  41.     def __setattr__( self, key, value ):
  42.         if key != '':
  43.             setattr( self.__dict__[''], key, value )
  44.         else:
  45.             self.__dict__[''] = value 
  46.     def __getattr__( self, key ):
  47.         if key == '':
  48.             return self.__dict__['']
  49.         else:
  50.             return getattr( self.__dict__[''], key )
  51. class _FullLoggedFunction( _LoggedFunction ):
  52.     """Fully-logged function wrapper (logs all call params to OpenGL.calltrace)"""
  53.     _callTrace = getLog( 'OpenGL.calltrace' )
  54.     def __call__( self, *args, **named ):
  55.         argRepr = []
  56.         function = getattr( self, '' )
  57.         for arg in args:
  58.             argRepr.append( repr(arg) )
  59.         for key,value in named.items():
  60.             argRepr.append( '%s = %s'%( key,repr(value)) )
  61.         argRepr = ",".join( argRepr )
  62.         self._callTrace.info( '%s( %s )', function.__name__, argRepr )
  63.         try:
  64.             return function( *args, **named )
  65.         except Exception, err:
  66.             self.log.warn(
  67.                 """Failure on %s: %s""", function.__name__, self.log.getException( err )
  68.             )
  69.             raise
  70. class _ErrorLoggedFunction ( _LoggedFunction ):
  71.     """On-error-logged function wrapper"""
  72.     def __call__( self, *args, **named ):
  73.         function = getattr( self, '' )
  74.         try:
  75.             return function( *args, **named )
  76.         except Exception, err:
  77.             self.log.warn(
  78.                 """Failure on %s: %s""", function.__name__, self.log.getException( err )
  79.             )
  80.             raise
  81.     
  82.  
  83. def logOnFail( function, log ):
  84.     """Produce possible log-wrapped version of function
  85.  
  86.     function -- callable object to be wrapped
  87.     log -- the log to which to log information
  88.     
  89.     Uses ERROR_LOGGING and FULL_LOGGING
  90.     to determine whether/how to wrap the function.
  91.     """
  92.     if ERROR_LOGGING or FULL_LOGGING:
  93.         if FULL_LOGGING:
  94.             loggedFunction = _FullLoggedFunction( function, log )
  95.         else:
  96.             loggedFunction = _ErrorLoggedFunction( function, log )
  97.         return loggedFunction
  98.     else:
  99.         return function
  100.