Last active
December 3, 2015 13:35
-
-
Save loicknuchel/7cbaf6d1635e91d7fc9a 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
// 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