Skip to content

Instantly share code, notes, and snippets.

@rmax
Created January 2, 2010 17:54
Show Gist options
  • Save rmax/267582 to your computer and use it in GitHub Desktop.
Save rmax/267582 to your computer and use it in GitHub Desktop.
import functools
import logging
import traceback
import os
def log_calls(f):
"""
displays arguments and return val for debugging purposes
based on:
http://caines.ca/blog/programming/the-debuggerator-a-practical-intro-to-decorators-in-python/
"""
@functools.wraps(f)
def wrapped(*args, **kwargs):
"""
"""
call_string = "%s called with *args: %r, **kwargs: %r " \
% (f.__name__, args, kwargs)
try:
retval = f(*args, **kwargs)
call_string += " --> " + repr(retval)
logging.debug(call_string)
return retval
except Exception, e:
top = traceback.extract_stack()[-1]
call_string += " RAISED EXCEPTION: "
call_string += ", ".join([type(e).__name__,
os.path.basename(top[0]),
str(top[1]),
str(e)])
logging.debug(call_string)
raise
return wrapped
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
@log_calls
def test(msg):
msg + 1
test('hola')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment