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);
The above will log 19
.
Combined with arguments.callee.caller
you can get closer to the type of useful logging you get in C via macros.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…