Skip to content

Commit

Permalink
[debug] pretty print async context tree when extending a stack trace
Browse files Browse the repository at this point in the history
  • Loading branch information
jbunton-atlassian committed Feb 20, 2018
1 parent 1672a02 commit 47108d5
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ chain.extend.attach(function (error, frames) {
if (lastTrace) {
if (DEBUG) {
debug(`extending: ${asyncId}`);
printRootTraces();
}
appendExtendedFrames(frames, lastTrace);
}
Expand Down Expand Up @@ -201,3 +202,51 @@ function asyncPromiseResolve(asyncId) {
ancestorTrace.recordDescendant(trace);
}
}

// Pretty printer, for debugging only

function printRootTraces() {
const rootAsyncIds = findRootAsyncIds();
for (const asyncId of rootAsyncIds) {
printTree(traces.get(asyncId));
}
}

function findRootAsyncIds() {
const asyncIds = new Set(traces.keys());
for (const trace of traces.values()) {
for (const notRootTrace of trace.descendants) {
asyncIds.delete(notRootTrace.asyncId);
}
}
return asyncIds;
}

function printTree(trace, indent='', isLast=true, visited=new Set()) {
let line = indent + '\\-' + trace.asyncId;

if (isLast) {
indent += ' ';
} else {
indent += '| ';
}

if (trace.disabled) {
line += ' (disabled)';
}

if (visited.has(trace.asyncId)) {
line += ' (duplicate)';
}

fs.writeSync(1, line + '\n');

if (visited.has(trace.asyncId)) {
return;
}
visited.add(trace.asyncId);

for (let i = 0; i < trace.descendants.length; ++i) {
printTree(trace.descendants[i], indent, i === trace.descendants.length - 1, visited);
}
}

0 comments on commit 47108d5

Please sign in to comment.