Last active
May 24, 2016 15:47
-
-
Save kjaquier/05fd58d651520f3a8fc94d6ba8968e91 to your computer and use it in GitHub Desktop.
Prints name, arguments and result when decorated function is called.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function trace(f) { | |
return function() { | |
var args = Array.from(arguments); | |
var res = f.apply(null, args); | |
console.debug("%s(%O) = %O", f.name, args, res); | |
return res; | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def trace(func): | |
def wrapper(*args, **kwargs): | |
s = "{func_name}({args}{sep}{kwargs})".format( | |
func_name=func.__name__, | |
args=", ".join("{}".format(repr(x)) for x in args), | |
sep=(", " if args and kwargs else ""), | |
kwargs=", ".join("{}={}".format(k, repr(v)) for k, v in kwargs.items()) | |
) | |
res = "" | |
try: | |
res = func(*args, **kwargs) | |
print(s, "=", repr(res)) | |
return res | |
except: | |
print(s) | |
raise | |
wrapper.__name__ = func.__name__ | |
return wrapper |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment