commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnue] r9764 - trunk/gnue-common/src/base


From: reinhard
Subject: [gnue] r9764 - trunk/gnue-common/src/base
Date: Tue, 24 Jul 2007 15:40:04 -0500 (CDT)

Author: reinhard
Date: 2007-07-24 15:40:03 -0500 (Tue, 24 Jul 2007)
New Revision: 9764

Modified:
   trunk/gnue-common/src/base/log.py
Log:
Added decorator for logged function that enables the logger to be explicitly
set.

issue123 in-progress


Modified: trunk/gnue-common/src/base/log.py
===================================================================
--- trunk/gnue-common/src/base/log.py   2007-07-19 06:53:29 UTC (rev 9763)
+++ trunk/gnue-common/src/base/log.py   2007-07-24 20:40:03 UTC (rev 9764)
@@ -35,12 +35,11 @@
 
 from gnue.common.base import utils
 
-__all__ = ['logged_f', 'debug', 'info', 'warning', 'deprecated', 'error',
-        'critical', 'exception', 'debug_n', 'info_n', 'warning_n',
+__all__ = ['logged_f', 'logged_f_n', 'debug', 'info', 'warning', 'deprecated',
+        'error', 'critical', 'exception', 'debug_n', 'info_n', 'warning_n',
         'deprecated_n', 'error_n', 'critical_n', 'exception_n']
 
 # TODO:
-# - allow for logger name to be passed to logged_f
 # - function to declare function as deprecated, maybe as decorator?
 # - Exception hook
 # - allow for __gnue_logger__ module global variable to use as a logger name
@@ -68,6 +67,11 @@
         myfunction(self, foo, bar):
             :
         myfunction=logged_f(myfunction)
+
+    @param func: Function to put the logging wrapper around.
+    @type func: function
+    @return: Function with the logging wrapper.
+    @rtype: function
     """
     def __new_f(*args, **kwargs):
         name = func.func_globals['__name__']
@@ -79,6 +83,41 @@
 
 # -----------------------------------------------------------------------------
 
+def logged_f_n(name):
+    """
+    Decorator to activate logging for a function using a specific logger.
+
+    If this decorator is applied to a function, every call to that function is
+    logged on C{DEBUG} level.
+
+    Usage with Python 2.4 or later::
+        @logged_f_n('my.logger.name')
+        myfunction(self, foo, bar):
+            :
+
+    Usage with Python 2.3::
+        myfunction(self, foo, bar):
+            :
+        myfunction=logged_f_n('my.logger.name')(myfunction)
+
+    @param name: Logger name to use.
+    @type name: string
+    @return: A function that accepts a function as argument and returns the
+        function with a logger wrapper around it (look at the usage example
+        above to understand this).
+    @rtype: function
+    """
+    def __logged_f(func):
+        def __new_f(*args, **kwargs):
+            __enter(name, func)
+            result = func(*args, **kwargs)
+            __leave(name, func, result)
+            return result
+        return __new_f
+    return __logged_f
+
+# -----------------------------------------------------------------------------
+
 def __enter(name, func):
 
     # Get the caller's frame
@@ -174,7 +213,10 @@
     """
     Write a debug message to the logger named after the calling module.
 
-    @return: always True, so the call can be prefixed with assert.
+    @param msg: Message to log.
+    @type msg: string or unicode
+    @return: Always True, so the call can be prefixed with assert.
+    @rtype: bool
     """
     return debug_n(__caller(), msg, *args, **kwargs)
 
@@ -184,7 +226,10 @@
     """
     Write an info message to the logger named after the calling module.
 
+    @param msg: Message to log.
+    @type msg: string or unicode
     @return: always True, so the call can be prefixed with assert.
+    @rtype: bool
     """
     return info_n(__caller(), msg, *args, **kwargs)
 
@@ -194,7 +239,10 @@
     """
     Write a warning message to the logger named after the calling module.
 
+    @param msg: Message to log.
+    @type msg: string or unicode
     @return: always True, so the call can be prefixed with assert.
+    @rtype: bool
     """
     return warning_n(__caller(), msg, *args, **kwargs)
 
@@ -205,7 +253,10 @@
     Write a deprecation warning message to the logger named after the calling
     module.
 
+    @param msg: Message to log.
+    @type msg: string or unicode
     @return: always True, so the call can be prefixed with assert.
+    @rtype: bool
     """
     return deprecated_n(__caller(), msg, *args, **kwargs)
 
@@ -215,7 +266,10 @@
     """
     Write an error message to the logger named after the calling module.
 
+    @param msg: Message to log.
+    @type msg: string or unicode
     @return: always True, so the call can be prefixed with assert.
+    @rtype: bool
     """
     return error_n(__caller(), msg, *args, **kwargs)
 
@@ -225,7 +279,10 @@
     """
     Write a critical error message to the logger named after the calling 
module.
 
+    @param msg: Message to log.
+    @type msg: string or unicode
     @return: always True, so the call can be prefixed with assert.
+    @rtype: bool
     """
     return critical_n(__caller(), msg, *args, **kwargs)
 
@@ -235,7 +292,10 @@
     """
     Log an exception to the logger named after the calling module.
 
+    @param msg: Message to log.
+    @type msg: string or unicode
     @return: always True, so the call can be prefixed with assert.
+    @rtype: bool
     """
     return exception_n(__caller(), msg, *args, **kwargs)
 
@@ -253,7 +313,12 @@
     """
     Write a debug message to the logger defined through the C{name} parameter.
 
+    @param name: Logger name to log to.
+    @type name: string
+    @param msg: Message to log.
+    @type msg: string or unicode
     @return: always True, so the call can be prefixed with assert.
+    @rtype: bool
     """
     logging.getLogger(name).debug(msg, *args, **kwargs)
     return True
@@ -264,7 +329,12 @@
     """
     Write an info message to the logger defined through the C{name} parameter.
 
+    @param name: Logger name to log to.
+    @type name: string
+    @param msg: Message to log.
+    @type msg: string or unicode
     @return: always True, so the call can be prefixed with assert.
+    @rtype: bool
     """
     logging.getLogger(name).info(msg, *args, **kwargs)
     return True
@@ -275,7 +345,12 @@
     """
     Write a warning message to the logger defined through the C{name} 
parameter.
 
+    @param name: Logger name to log to.
+    @type name: string
+    @param msg: Message to log.
+    @type msg: string or unicode
     @return: always True, so the call can be prefixed with assert.
+    @rtype: bool
     """
     logging.getLogger(name).warning(msg, *args, **kwargs)
     return True
@@ -287,7 +362,12 @@
     Write a deprecation warning message to the logger defined through the
     C{name} parameter.
 
+    @param name: Logger name to log to.
+    @type name: string
+    @param msg: Message to log.
+    @type msg: string or unicode
     @return: always True, so the call can be prefixed with assert.
+    @rtype: bool
     """
     logging.getLogger(name).log(31, msg, *args, **kwargs)
     return True
@@ -298,7 +378,12 @@
     """
     Write an error message to the logger defined through the C{name} parameter.
 
+    @param name: Logger name to log to.
+    @type name: string
+    @param msg: Message to log.
+    @type msg: string or unicode
     @return: always True, so the call can be prefixed with assert.
+    @rtype: bool
     """
     logging.getLogger(name).error(msg, *args, **kwargs)
     return True
@@ -310,7 +395,12 @@
     Write a critical error message to the logger defined through the C{name}
     parameter.
 
+    @param name: Logger name to log to.
+    @type name: string
+    @param msg: Message to log.
+    @type msg: string or unicode
     @return: always True, so the call can be prefixed with assert.
+    @rtype: bool
     """
     logging.getLogger(name).critical(msg, *args, **kwargs)
     return True
@@ -321,7 +411,12 @@
     """
     Log an exception to the logger defined through the C{name} parameter.
 
+    @param name: Logger name to log to.
+    @type name: string
+    @param msg: Message to log.
+    @type msg: string or unicode
     @return: always True, so the call can be prefixed with assert.
+    @rtype: bool
     """
     logging.getLogger(name).exception(msg, *args, **kwargs)
     return True





reply via email to

[Prev in Thread] Current Thread [Next in Thread]