added 'append' option to exceptionLogged to print out before or after stack trace

This commit is contained in:
Darren Ranalli 2006-12-27 19:58:18 +00:00
parent d7b0ba8510
commit a0307f90a0
2 changed files with 32 additions and 23 deletions

View File

@ -191,7 +191,7 @@ class DoCollectionManager:
else: else:
self.notify.warning('handleSetLocation: object %s not present' % self.getMsgChannel()) self.notify.warning('handleSetLocation: object %s not present' % self.getMsgChannel())
@exceptionLogged @exceptionLogged()
def storeObjectLocation(self, object, parentId, zoneId): def storeObjectLocation(self, object, parentId, zoneId):
oldParentId = object.parentId oldParentId = object.parentId
oldZoneId = object.zoneId oldZoneId = object.zoneId

View File

@ -2491,17 +2491,22 @@ def superFlattenShip(ship):
#PHASE 5: flatten strong! #PHASE 5: flatten strong!
return ship.flattenStrong() return ship.flattenStrong()
def exceptionLogged(f): def exceptionLogged(append=True):
"""decorator that appends the function name and all arguments to the """decorator that outputs the function name and all arguments
__str__ output of the exception if an exception passes back through if an exception passes back through the stack frame
the stack frame if append is true, string is appended to the __str__ output of
the exception. if append is false, string is printed to the log
directly. If the output will take up many lines, it's recommended
to set append to False so that the exception stack is not hidden
by the output of this decorator.
""" """
def _decoratorFunc(f, append=append):
def _exceptionLogged(*args, **kArgs): def _exceptionLogged(*args, **kArgs):
try: try:
return f(*args, **kArgs) return f(*args, **kArgs)
except Exception, e: except Exception, e:
try: try:
s = '\nSTACK UNWIND: %s(' % f.func_name s = 'STACK UNWIND: %s(' % f.func_name
for arg in args: for arg in args:
s += '%s, ' % arg s += '%s, ' % arg
for key, value in kArgs.items(): for key, value in kArgs.items():
@ -2509,12 +2514,16 @@ def exceptionLogged(f):
if len(args) or len(kArgs): if len(args) or len(kArgs):
s = s[:-2] s = s[:-2]
s += ')' s += ')'
appendStr(e, s) if append:
appendStr(e, '\n%s' % s)
else:
print s
except: except:
print 'exceptionLogged(%s): ERROR IN PRINTING' % f.func_name print 'exceptionLogged(%s): ERROR IN PRINTING' % f.func_name
raise raise
_exceptionLogged.__doc__ = f.__doc__ _exceptionLogged.__doc__ = f.__doc__
return _exceptionLogged return _exceptionLogged
return _decoratorFunc
# class 'decorator' that records the stack at the time of creation # class 'decorator' that records the stack at the time of creation
# be careful with this, it creates a StackTrace, and that can take a # be careful with this, it creates a StackTrace, and that can take a