Skip to content

Instantly share code, notes, and snippets.

@j3lte
Last active December 16, 2015 11:49
Show Gist options
  • Select an option

  • Save j3lte/5429830 to your computer and use it in GitHub Desktop.

Select an option

Save j3lte/5429830 to your computer and use it in GitHub Desktop.
Little stacktrace code, can be implemented in your code to debug which functions are called, from where. Implemented in NodeJS. Easy to use!
/* LITTLE CODE DEBUGGER, call it with : console.log(__debug); */
Object.defineProperty(global, '__stack', { get: function() { var orig = Error.prepareStackTrace; Error.prepareStackTrace = function(_, stack) { return stack; }; var err = new Error; Error.captureStackTrace(err, arguments.callee); var stack = err.stack; Error.prepareStackTrace = orig; return stack; }});
Object.defineProperty(global, '__debug', { get: function(){ return "[+] Called (" + __stack[1].getLineNumber() + ' : ' + __stack[1].getFunctionName() + ") From ("+ __stack[2].getLineNumber() + ' : ' + __stack[2].getFunctionName() + ")"; }});
/* END DEBUG */
/* HOW TO USE
This can be traced, using
console.log(__debug);
In your console you will see lines like these:
[+] Called (55 : exports.getTables) From (63 : null)
[+] Called (20 : doQuery) From (56 : exports.getTables)
This states what function is called and from which function it is called ( linenumber : functionname )
Easy to use, works even in callbacks
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment