diff options
| author | Michael E Brown <michael_e_brown@dell.com> | 2007-10-17 11:31:30 -0500 |
|---|---|---|
| committer | Michael E Brown <michael_e_brown@dell.com> | 2007-10-17 11:31:30 -0500 |
| commit | 06faade86a4bcdd823da33da4fd9a159139010d7 (patch) | |
| tree | 629374ebac3c3894ddf423ca356354cb65745f6c /src | |
| parent | 3c9ab9ea8f046911b52c1139addcf816ecd629eb (diff) | |
| download | mock-06faade86a4bcdd823da33da4fd9a159139010d7.tar.gz mock-06faade86a4bcdd823da33da4fd9a159139010d7.tar.xz mock-06faade86a4bcdd823da33da4fd9a159139010d7.zip | |
add traceLog decorator which allows specifying the logger to use.
Diffstat (limited to 'src')
| -rwxr-xr-x | src/py-libs/trace_decorator.py | 39 |
1 files changed, 27 insertions, 12 deletions
diff --git a/src/py-libs/trace_decorator.py b/src/py-libs/trace_decorator.py index b6322a1..9110020 100755 --- a/src/py-libs/trace_decorator.py +++ b/src/py-libs/trace_decorator.py @@ -4,30 +4,45 @@ import types from decorator import decorator import logging -log = logging.getLogger("mock.trace_decorator") +moduleLog = logging.getLogger("mock.trace_decorator") -#@decorator @decorator def trace(f, *args, **kw): - log.debug("ENTER: %s(%s, %s)" % (f.func_name, args, kw)) + moduleLog.debug("ENTER: %s(%s, %s)" % (f.func_name, args, kw)) try: result = "Bad exception raised: Exception was not a derived class of 'Exception'" try: result = f(*args, **kw) except Exception, e: result = "EXCEPTION RAISED" - log.debug( "EXCEPTION: %s\n" % e, exc_info=1) + moduleLog.debug( "EXCEPTION: %s\n" % e, exc_info=1) raise finally: - log.debug( "LEAVE %s --> %s\n" % (f.func_name, result)) + moduleLog.debug( "LEAVE %s --> %s\n" % (f.func_name, result)) return result -# helper function so we can use back-compat format but not be ugly -def decorateAllFunctions(module): - methods = [ method for method in dir(module) - if isinstance(getattr(module, method), types.FunctionType) - ] - for i in methods: - setattr(module, i, trace(getattr(module,i))) +def traceLog(logger = moduleLog): + log = logger + @decorator + def trace(f, *args, **kw): + # default to logger that was passed by module, but + # can override by passing logger=foo as function parameter. + # make sure this doesnt conflict with one of the parameters + # you are expecting + l2 = kw.get('logger', log) + l2.debug("ENTER: %s(%s, %s)" % (f.func_name, args, kw)) + try: + result = "Bad exception raised: Exception was not a derived class of 'Exception'" + try: + result = f(*args, **kw) + except Exception, e: + result = "EXCEPTION RAISED" + l2.debug( "EXCEPTION: %s\n" % e, exc_info=1) + raise + finally: + l2.debug( "LEAVE %s --> %s\n" % (f.func_name, result)) + + return result + return trace |
