Last active
January 3, 2016 15:18
-
-
Save rosko/8481587 to your computer and use it in GitHub Desktop.
Function for object debugging
This file contains hidden or 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
window.console.object = function (obj, options) { | |
var defaultOptions = { | |
'label': 'obj', | |
'consoleFunction': 'log', | |
'showArguments': true, | |
'exclude': [], | |
'meterTime': false | |
}; | |
options = _.defaults(options || {}, defaultOptions); | |
obj._consoleObjectLevel = options['startLevel'] || 0; | |
var functions = _.difference(_.functions(obj), options.exclude); | |
_.each(functions, function (funcName) { | |
var oldFunc = obj[funcName]; | |
obj[funcName] = function () { | |
var prefix = (new Array(obj._consoleObjectLevel + 1)).join(' '); | |
if (options.showArguments) { | |
console[options.consoleFunction](prefix, options.label, funcName, arguments); | |
} else { | |
console[options.consoleFunction](prefix, options.label, funcName); | |
} | |
obj._consoleObjectLevel++; | |
if (options.meterTime) { | |
var startTime = new Date().getTime(); | |
} | |
var ret = oldFunc.apply(obj, arguments); | |
if (options.meterTime) { | |
console.log(prefix, new Date().getTime() - startTime + 'ms'); | |
} | |
obj._consoleObjectLevel--; | |
return ret; | |
}; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment