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 be recorded 19 .
When combined with arguments.callee.caller you can come close to the type of useful logging you get in C through macros.
james_womack Jul 08 2018-12-12T00: 00Z
source share