Skip to content

Instantly share code, notes, and snippets.

@ReedD
Created November 10, 2015 18:05
Show Gist options
  • Save ReedD/1e8257e61493eced9af0 to your computer and use it in GitHub Desktop.
Save ReedD/1e8257e61493eced9af0 to your computer and use it in GitHub Desktop.
Helpful node/javascript debug globals
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, '__line', {
get: function () {
return __stack[1].getLineNumber();
}
});
Object.defineProperty(global, '__basename', {
get: function () {
return require('path').basename(__stack[1].getFileName());
}
});
Object.defineProperty(global, '__relative', {
get: function () {
return require('path').relative(process.cwd(), __stack[1].getFileName());
}
});
Object.defineProperty(global, '__function', {
get: function () {
return __stack[1].getFunctionName();
}
});
@jmillerdesign
Copy link

// Usage
console.log('[%s] %s - %s: line %s', new Date(), __relative, __function, __line);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment