public
Last active — forked from livingston/tracer.js

  • Download Gist
tracer.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
/* 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 = [];
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.