Last active
December 10, 2015 03:58
-
-
Save nemtsov/4378073 to your computer and use it in GitHub Desktop.
Retrieves the filename of the caller of a function.
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
StackUtil = {}; | |
/** | |
* Get the filename of the caller of a function | |
* | |
* @param {Boolean} options.isLineNumberRendered | |
* @return {String} name | |
*/ | |
StackUtil.getCallerFilename = function (options) { | |
var name, callSite; | |
options = options || {}; | |
Error.prepareStackTrace = _getCallerCallSite; | |
callSite = new Error().stack; | |
Error.prepareStackTrace = undefined; | |
name = callSite.getFileName() | |
if (options.isLineNumberRendered) { | |
name += ':' + callSite.getLineNumber() | |
+ ':' + callSite.getColumnNumber(); | |
} | |
return name; | |
}; | |
function _getCallerCallSite(err, trace) { | |
// 0: stack_util, 1: caller, 2: caller's caller | |
return trace[2]; | |
} | |
module.exports = StackUtil; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Calling
Error.prepareStackTrace
has negative implications on performance. Consider using the following instead: