Skip to content

Instantly share code, notes, and snippets.

@loicknuchel
Last active December 3, 2015 13:35
Show Gist options
  • Save loicknuchel/7cbaf6d1635e91d7fc9a to your computer and use it in GitHub Desktop.
Save loicknuchel/7cbaf6d1635e91d7fc9a to your computer and use it in GitHub Desktop.
// inspired from https://github.com/stacktracejs/stacktrace.js/blob/master/stacktrace.js
var Log = (function(){
return {
instrumentObj: instrumentObj
};
// This is working on my tests, do I made some "dangerous" things ?
function instrumentObj(tag, obj){
for(var i in obj){
if(typeof obj[i] === 'function'){
var fn = obj[i];
var instrumented = function Log$$instrumented(){
var jsonArgs = [];
for(var j in arguments){
jsonArgs.push(JSON.stringify(arguments[j]));
}
console.log(tag+'.'+i+'('+jsonArgs.join(', ')+')');
return fn.apply(this, arguments);
}
instrumented.__riginalFn = fn;
obj[i] = instrumented;
}
}
}
})();
var myObj = {};
myObj.myFunction = function(arg){
alert('Salut '+arg);
}
Log.instrumentObj('myObj', myObj);
myObj.myFunction('Toto');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment