Skip to content

Commit

Permalink
Merge pull request TomFrost#12 from bartekn/fast-get-origin
Browse files Browse the repository at this point in the history
Use V8 JavaScriptStackTraceApi to find log origin
  • Loading branch information
TomFrost committed Oct 2, 2015
2 parents d85d8f5 + 8fef569 commit 4e0a8c7
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions lib/Bristol.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,30 +224,32 @@ Bristol.prototype._getGlobals = function() {
return globals;
};

var originalPrepareStackTrace = Error.prepareStackTrace;
var arrayPrepareStackTrace = function (err, stack) { return stack; };
/**
* Finds the origin of the Bristol log call, and supplies the file path and
* line number.
* This function is using JavaScriptStackTraceApi to be as fast as possible:
* https://code.google.com/p/v8-wiki/wiki/JavaScriptStackTraceApi
* @returns {null|{file, line}} An object containing the file path and line
* number of the originating Bristol call, or null if this information
* cannot be found.
* @private
*/
Bristol.prototype._getOrigin = function() {
var stack = (new Error()).stack.split(/\s*\n\s*(?:at\s)?/),
origin = null,
caller;
Error.prepareStackTrace = arrayPrepareStackTrace;
var stack = (new Error()).stack,
origin = null;
for (var i = 1; i < stack.length; i++) {
if (!stack[i].match(/^Bristol\./)) {
caller = stack[i].match(/([^:\s\(]+):(\d+):(\d+)\)?$/);
if (stack[i].getThis() != this) {
origin = {
file: stack[i].getFileName(),
line: stack[i].getLineNumber().toString()
};
break;
}
}
if (caller && caller.length == 4) {
origin = {
file: caller[1],
line: caller[2]
};
}
Error.prepareStackTrace = originalPrepareStackTrace;
return origin;
};

Expand Down

0 comments on commit 4e0a8c7

Please sign in to comment.