Last active
December 22, 2022 01:23
-
-
Save LMBernardo/8f7b0bc2730ea09b90c29601056e589c to your computer and use it in GitHub Desktop.
Defines functions that can be used for introspection when logging in JS V8 and NodeJS. Originally found here: https://stackoverflow.com/questions/14172455/get-name-and-line-of-calling-function-in-node-js
This file contains 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
if (!global.hasOwnProperty("__DEBUG_LOG_INTROSPECTION__")) { | |
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, '__function', { | |
get: function () { | |
return __stack[1].getFunctionName(); | |
} | |
}); | |
Object.defineProperty(global, '__fileName', { | |
get: function () { | |
let fullpath = __stack[1].getFileName().trim().split(/\/|\\/); | |
return fullpath[fullpath.length - 1]; | |
} | |
}); | |
Object.defineProperty(global, "__DEBUG_LOG_INTROSPECTION__", { | |
"Type": "FLAG" | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment