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

[email protected]
Newsgroups gmane.comp.cms.gnue.cvs
Message-ID <[email protected]>
Author: reinhard
Date: 2007-07-17 03:50:19 -0500 (Tue, 17 Jul 2007)
New Revision: 9761

Modified:
   trunk/gnue-common/src/base/log.py
Log:
Improved output of function logging.

issue123 in-progress


Modified: trunk/gnue-common/src/base/log.py
===================================================================
--- trunk/gnue-common/src/base/log.py	2007-07-16 21:48:40 UTC (rev 9760)
+++ trunk/gnue-common/src/base/log.py	2007-07-17 08:50:19 UTC (rev 9761)
@@ -40,6 +40,7 @@
         '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,13 +69,13 @@
             :
         myfunction=logged_f(myfunction)
     """
-    def new_f(*args, **kwargs):
+    def __new_f(*args, **kwargs):
         name = func.func_globals['__name__']
         __enter(name, func)
         result = func(*args, **kwargs)
         __leave(name, func, result)
         return result
-    return new_f
+    return __new_f
 
 # -----------------------------------------------------------------------------
 
@@ -82,19 +83,38 @@
 
     # Get the caller's frame
     frame = inspect.currentframe().f_back
-    while not frame.f_code.co_name == 'new_f':
+    while not frame.f_code.co_name == '__new_f':
         frame = frame.f_back
 
     try:
         fname = __func_name(func, frame)
 
+        # Note that the frame contains the __new_f function call, not the
+        # actual function call. So there are only 2 parameters, *args and
+        # **kwargs. We match them with the parameter list of the actual
+        # function.
         args = frame.f_locals['args']
-        kwargs = frame.f_locals['kwargs']
+        kwargs = frame.f_locals['kwargs'].copy()
 
         params = []
-        # Arguments
-        params.extend([repr(item) for item in args])
-        # Keyword arguments
+
+        # Fixed arguments.
+        code = func.func_code
+        for (k, v) in zip(code.co_varnames[:code.co_argcount], args):
+            params.append("%s=%r" % (k, v))
+
+        # Variable arguments.
+        params.extend([repr(item) for item in args[code.co_argcount:]])
+
+        # Variable keyword arguments.
+        # Try to sort keyword arguments according to function definition.
+        for k in code.co_varnames[:code.co_argcount]:
+            if kwargs.has_key(k):
+                params.append("%s=%r" % (k, kwargs[k]))
+                del kwargs[k]
+
+        # List the keyword arguments not given in the function definition at
+        # the very end.
         params.extend(["%s=%r" % (k, v) for (k, v) in kwargs.items()])
 
         msg = "Calling function %s(%s)" % (fname, ", ".join(params))
@@ -112,7 +132,7 @@
 
     # Get the caller's frame
     frame = inspect.currentframe().f_back
-    while not frame.f_code.co_name == 'new_f':
+    while not frame.f_code.co_name == '__new_f':
         frame = frame.f_back
 
     try:
@@ -133,7 +153,7 @@
     # inspect the 'self' parameter.
     fname = func.__name__
     code = func.func_code
-    if code.co_argcount > 0 and code.co_names[0] == 'self':
+    if code.co_argcount > 0 and code.co_varnames[0] == 'self':
         klass = frame.f_locals['args'][0].__class__
         if isinstance(klass, str):
             # Old stle class
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.