diff --git a/index.js b/index.js index 8557f79..32aef6a 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,9 @@ 'use strict'; -if(!process.addAsyncListener) +if(!process.addAsyncListener) require('async-listener'); -var util = require('util'); -var TraceModule = require('./trace')(Error, __dirname + '/node_modules/async-listener'); +var path = require('path'); +var TraceModule = require('./trace')(Error, path.dirname(require.resolve('async-listener'))); Error.stackTraceLimit = Infinity; @@ -12,17 +12,16 @@ var activeTrace; // -- async listener -- // -// each time an async event is scheduled, i.e. setImmediate, or -// process.nextTick, etc. we capture the stack at that exact point -// this is going to be a crap ton of stack traces, but it will be -// very accurate! -function asyncListener() { - return TraceModule.NewWithParent(activeTrace, asyncListener); -} - // the async handler glues the previous stack trace to // the next one var asyncHandlers = { + create : function create() { + // each time an async event is scheduled, i.e. setImmediate, or + // process.nextTick, etc. we capture the stack at that exact point + // this is going to be a crap ton of stack traces, but it will be + // very accurate! + return TraceModule.NewWithParent(activeTrace, create); + }, before : function before(context, trace) { // activeTrace is used to link child traces to their parents activeTrace = trace; @@ -41,4 +40,4 @@ var asyncHandlers = { } } -process.addAsyncListener(asyncListener, asyncHandlers); +process.addAsyncListener(asyncHandlers); diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..52bee8d --- /dev/null +++ b/package-lock.json @@ -0,0 +1,21 @@ +{ + "name": "stackup", + "version": "1.0.2", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "async-listener": { + "version": "0.4.7", + "resolved": "https://registry.npmjs.org/async-listener/-/async-listener-0.4.7.tgz", + "integrity": "sha1-DY45ALMY4tY8WHGCIRoe7zu1u4I=", + "requires": { + "shimmer": "1.0.0" + } + }, + "shimmer": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/shimmer/-/shimmer-1.0.0.tgz", + "integrity": "sha1-ScLXHGeDYLgCvhiyeDgtHLuAXDk=" + } + } +} diff --git a/package.json b/package.json index 07a0623..57b6718 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,15 @@ { "name": "stackup", - "version": "0.0.5", + "version": "1.0.2", "description": "Long stack traces based on async listeners", "main": "longtrace.js", "author": "Jacob Groundwater ", "license": "MIT", + "repository": { + "type": "git", + "url": "git://github.com/groundwater/node-stackup.git" + }, "dependencies": { - "async-listener": "~0.2.2" + "async-listener": "~0.4.7" } } diff --git a/trace.js b/trace.js index 7f33798..f2082af 100644 --- a/trace.js +++ b/trace.js @@ -1,6 +1,6 @@ var util = require('util'); -function Trace(module){ +function Trace(module) { this.module = module; this.parent = null; this.stack = null; @@ -9,16 +9,22 @@ function Trace(module){ Trace.prototype.toString = function toString(stack) { var out = []; - if (stack) out.push(this.__filter(stack.split(/\n+/)).join('\n')); - if (this.stack) out.push(this.__format(this.stack.stack)); - if (this.parent) out.push(this.parent.toString()); + if (stack) out.push(this.__filter(stack.split(/\n+/)).join('\n')); + if (this.stack) out.push(this.__format(this.stack.stack)); + + var current = this.parent; + + while (current) { + if (current.stack) out.push(current.__format(current.stack.stack)); + current = current.parent; + } return out.join('\n'); } Trace.prototype.__format = function __format(stack) { var split = stack.split(/\n+/); - + // get rid of [object Object] line split.shift(); split.unshift(' ---- async ----'); @@ -56,7 +62,7 @@ TraceModule.prototype.NewWithParent = function NewWithParent(parent, omit) { var trace = this.New(); var omitStack = omit || NewWithParent; var stack = {}; - + this.Error.captureStackTrace(stack, omitStack); trace.parent = parent; @@ -68,4 +74,3 @@ TraceModule.prototype.NewWithParent = function NewWithParent(parent, omit) { module.exports = function (Error, filter) { return new TraceModule(Error, filter); } -