Last active
August 29, 2015 14:04
-
-
Save Unitech/5a7e099d8c99b1b16d7c to your computer and use it in GitHub Desktop.
Log for Node with filename + line
This file contains hidden or 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
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(); |
This file contains hidden or 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
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
from http://stackoverflow.com/questions/11386492/accessing-line-number-in-v8-javascript-chrome-node-js