-
-
Save aglemann/434689 to your computer and use it in GitHub Desktop.
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
/* tracer.js - A tracer utility in 2kb | |
* | |
* @author Angus Croll | |
* http://javascriptweblog.wordpress.com/2010/06/01/a-tracer-utility-in-2kb/ | |
*/ | |
String.prototype.times = function(count){ | |
return count < 1 ? '' : new Array(count + 1).join(this); | |
} | |
var tracer = { | |
nativeCodeEx: /\[native code\]/, | |
traceEx: /function traceOn\(/, | |
indentCount: -4, | |
tracing: [], | |
traceMe: function(func, methodName){ | |
function traceOn(){ | |
var startTime = +new Date; | |
var indentString = ' '.times(tracer.indentCount += 4); | |
console.info(indentString + methodName + '(' + Array().slice.call(arguments).join(', ') + ')'); | |
var result = func.apply(this, arguments); | |
console.info(indentString + methodName, '-> ', result, '(', new Date - startTime, 'ms', ')'); | |
tracer.indentCount -= 4; | |
return result; | |
} | |
traceOn.traceOff = func; | |
for (var prop in func){ | |
traceOn[prop] = func[prop]; | |
} | |
console.log('tracing ' + methodName); | |
return traceOn; | |
}, | |
traceAll: function(root, recurse){ | |
if ((root == window) || !((typeof root == 'object') || (typeof root == 'function'))){ return; } | |
for (var key in root){ | |
if ((root.hasOwnProperty(key)) && (root[key] != root)){ | |
var thisObj = root[key]; | |
if (this.traceEx.test(thisObj)) continue; | |
if (typeof thisObj == 'function'){ | |
if ((this != root) && !this.nativeCodeEx.test(thisObj)){ | |
root[key] = this.traceMe(root[key], key); | |
this.tracing.push({ obj: root, methodName: key }); | |
} | |
} | |
recurse && this.traceAll(thisObj, true); | |
} | |
} | |
}, | |
untraceAll: function(){ | |
for (var i = 0; i < this.tracing.length; ++i){ | |
var thisTracing = this.tracing[i]; | |
thisTracing.obj[thisTracing.methodName] = thisTracing.obj[thisTracing.methodName].traceOff; | |
} | |
console.log('tracing disabled'); | |
tracer.tracing = []; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment