[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnue] r6982 - trunk/gnue-common/src/apps
From: |
johannes |
Subject: |
[gnue] r6982 - trunk/gnue-common/src/apps |
Date: |
Wed, 9 Feb 2005 13:34:28 -0600 (CST) |
Author: johannes
Date: 2005-02-09 13:34:27 -0600 (Wed, 09 Feb 2005)
New Revision: 6982
Modified:
trunk/gnue-common/src/apps/GDebug.py
Log:
Added gBeginFunc () and gEndFunc ()
Modified: trunk/gnue-common/src/apps/GDebug.py
===================================================================
--- trunk/gnue-common/src/apps/GDebug.py 2005-02-09 16:57:49 UTC (rev
6981)
+++ trunk/gnue-common/src/apps/GDebug.py 2005-02-09 19:34:27 UTC (rev
6982)
@@ -1,6 +1,9 @@
+# GNU Enterprise Common - Application Services - Debugging support
#
-# This file is part of GNU Enterprise.
+# Copyright 2001-2005 Free Software Foundation
#
+# This file is part of GNU Enterprise
+#
# GNU Enterprise is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either
@@ -16,23 +19,14 @@
# write to the Free Software Foundation, Inc., 59 Temple Place
# - Suite 330, Boston, MA 02111-1307, USA.
#
-# Copyright 2000-2005 Free Software Foundation
-#
-# FILE:
-# GDebug.py
-#
-# DESCRIPTION:
-# Intelligently handles traces and exception handling for Client Apps
-#
-# NOTES:
-#
-# HISTORY:
-#
+# $Id: $
import string
import sys
import time
from traceback import *
+import inspect
+import os
import __builtin__
_fh = sys.__stderr__
@@ -103,7 +97,7 @@
def _noPrintMesg(level, message, dropToDebugger=0):
pass
-def _printMesg(level, message, dropToDebugger=0):
+def _printMesg (level, message, dropToDebugger = 0):
if ( level <= _DEBUG_LEVEL ):
global _fh, _DEBUGGER
if type(message)==type(u''):
@@ -117,22 +111,165 @@
except:
file = ""
- s = time.time () - __starttime
- (m, s) = divmod (s, 60)
- (h, m) = divmod (m, 60)
- datetime = "%d:%02d:%06.3f" % (h, m, s)
+ __dumpMessage (level, file, message, dropToDebugger)
- lines = string.split("%s" % message, '\n')
- for line in lines:
- _fh.write("DB%03d: %s %s%s\n" % (level, datetime, file, line))
- if dropToDebugger and _DEBUGGER:
- _DEBUGGER.set_trace()
+# -----------------------------------------------------------------------------
+# Dump a message to the debug-output
+# -----------------------------------------------------------------------------
+def __dumpMessage (level, filename, message, dropToDebugger = False):
+ """
+ This function writes a message to the debug-output adding the time elapsed
+ since __starttime.
+
+ @param level: the debug-level the message will be logged in
+ @param filename: the filename the message originated from
+ @param message: the message to be logged
+ @param dropToDebugger: if set to True, the message will be dropped to the
+ currently set debugger.
+ """
+
+ s = time.time () - __starttime
+ (m, s) = divmod (s, 60)
+ (h, m) = divmod (m, 60)
+ stamp = "%d:%02d:%06.3f" % (h, m, s)
+
+ lines = "%s" % message
+ for line in lines.splitlines ():
+ _fh.write ("DB%03d: %s %s%s%s" % (level, stamp, filename, line,
os.linesep))
+
+ if dropToDebugger and _DEBUGGER:
+ _DEBUGGER.set_trace()
+
+
+
printMesg = _noPrintMesg
__builtin__.__dict__['gDebug'] = printMesg
+
+# -----------------------------------------------------------------------------
+# Add a function-signature to the debug output
+# -----------------------------------------------------------------------------
+
+def gBeginFunc (level = 1):
+ """
+ This function adds another line to the debug-output, describing the caller's
+ function with all it's arguments.
+
+ @param level: the debug-level the message will be logged in
+ """
+
+ if level > _DEBUG_LEVEL:
+ return
+
+ # Get the caller's frame
+ frame = sys._getframe (1)
+
+ try:
+ (args, vargs, vkw, flocals) = inspect.getargvalues (frame)
+
+ # If the function has a 'self' argument we add the class referenced by self
+ # to the name of the function
+ funcName = frame.f_code.co_name
+ if 'self' in args:
+ funcName = "%s.%s" % (flocals ['self'].__class__, funcName)
+
+ params = []
+
+ # First add all 'normal' arguments
+ for item in args:
+ value = item == 'self' and hex (id (flocals ['self'])) or flocals [item]
+ params.append (repr (value))
+
+ # Next, add all variable arguments (*arg)
+ if vargs:
+ params.extend ([repr (i) for i in flocals [vargs]])
+
+ # and finally add all keyword arguments (**kwarg)
+ if vkw is not None:
+ params.extend (["%s = %s" % (repr (k), repr (v)) \
+ for (k, v) in flocals [vkw].items ()])
+
+ message = "%s (%s)" % (funcName, string.join (params, ", "))
+
+ path = frame.f_code.co_filename
+ if path [-3:] == '.py':
+ path = string.split (path [:-3], '/') [-1]
+ else:
+ path = string.split (path, '/') [-1]
+
+ filename = "[%s:%s] " % (path, frame.f_code.co_firstlineno)
+
+ __dumpMessage (level, filename, message)
+
+ finally:
+ # Make sure to release the reference to the frame object. This keeps
+ # garbage collection doing a fine job :)
+ del frame
+
+
+# -----------------------------------------------------------------------------
+# Add a line to debug-output describing the end of a function call
+# -----------------------------------------------------------------------------
+
+def gEndFunc (level = 1, result = None, hasResult = True):
+ """
+ This function adds a line to the debug-output describing the end of a
+ function call, optionally containing the function's result.
+
+ @param level: debug-level to send the message to
+ @param result: the function's result or None
+ @param hasResult: if set to True, the debug-message will contain the
+ function's result (even if it's None). If set to False the result won't
+ be in the debug-message, unless the result is not None.
+ """
+
+ if level > _DEBUG_LEVEL:
+ return
+
+ # Get the caller's frame
+ frame = sys._getframe (1)
+
+ try:
+ (args, vargs, vkw, flocals) = inspect.getargvalues (frame)
+
+ # If the function has a 'self' argument we add the class referenced by self
+ # to the name of the function
+ fName = frame.f_code.co_name
+ hId = ''
+ if 'self' in args:
+ fName = "%s.%s" % (flocals ['self'].__class__, fName)
+ hId = repr (hex (id (flocals ['self'])))
+
+ # Override the output-flag if there's a result value given.
+ pr = hasResult or result is not None
+
+ message = "%s (%s)%s" % (fName, hId, pr and ' == %s' % repr (result) or '')
+
+ path = frame.f_code.co_filename
+ if path [-3:] == '.py':
+ path = string.split (path [:-3], '/') [-1]
+ else:
+ path = string.split (path, '/') [-1]
+
+ filename = "[%s:%s] " % (path, frame.f_code.co_firstlineno)
+
+ __dumpMessage (level, filename, message)
+
+ finally:
+ # Make sure to release the reference to the frame object. This keeps
+ # garbage collection doing a fine job :)
+ del frame
+
+
+
+
+__builtin__.__dict__ ['gBeginFunc'] = gBeginFunc
+__builtin__.__dict__ ['gEndFunc'] = gEndFunc
+
+
class GETrace(Exception):
#
#Exception class representing a debug message
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [gnue] r6982 - trunk/gnue-common/src/apps,
johannes <=