Skip to content

Instantly share code, notes, and snippets.

@Unitech
Last active August 29, 2015 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Unitech/5a7e099d8c99b1b16d7c to your computer and use it in GitHub Desktop.
Save Unitech/5a7e099d8c99b1b16d7c to your computer and use it in GitHub Desktop.
Log for Node with filename + line
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();
}
});
function test() {
console.log('[%s : %s() (%s,%s)] %s',
__stack[0].getFileName(),
__stack[0].getFunctionName(),
__stack[0].getLineNumber(),
__stack[0].getColumnNumber(),
'hello message');
}
test();
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();
}
});
console.log(__line);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment