Skip to content

Commit e44c237

Browse files
[debug] pretty print async context tree when extending a stack trace
1 parent 1672a02 commit e44c237

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

trace.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ chain.extend.attach(function (error, frames) {
4545
if (lastTrace) {
4646
if (DEBUG) {
4747
debug(`extending: ${asyncId}`);
48+
printRootTraces();
4849
}
4950
appendExtendedFrames(frames, lastTrace);
5051
}
@@ -201,3 +202,54 @@ function asyncPromiseResolve(asyncId) {
201202
ancestorTrace.recordDescendant(trace);
202203
}
203204
}
205+
206+
// Pretty printer, for debugging only
207+
208+
function printRootTraces() {
209+
const rootAsyncIds = findRootAsyncIds();
210+
for (const asyncId of rootAsyncIds) {
211+
printTree(traces.get(asyncId));
212+
}
213+
}
214+
215+
function findRootAsyncIds() {
216+
const asyncIds = new Set(traces.keys());
217+
for (const trace of traces.values()) {
218+
for (const notRootTrace of trace.descendants) {
219+
asyncIds.delete(notRootTrace.asyncId);
220+
}
221+
}
222+
return asyncIds;
223+
}
224+
225+
function printTree(trace, indent='', isLast=true, visited=new Set()) {
226+
let line = indent;
227+
228+
if (isLast) {
229+
line += '\\-';
230+
indent += ' ';
231+
} else {
232+
line += '|-';
233+
indent += '| ';
234+
}
235+
236+
line += trace.asyncId;
237+
if (trace.disabled) {
238+
line += ' (disabled)';
239+
}
240+
241+
if (visited.has(trace.asyncId)) {
242+
line += ' (duplicate)';
243+
}
244+
245+
fs.writeSync(1, line + '\n');
246+
247+
if (visited.has(trace.asyncId)) {
248+
return;
249+
}
250+
visited.add(trace.asyncId);
251+
252+
for (let i = 0; i < trace.descendants.length; ++i) {
253+
printTree(trace.descendants[i], indent, i === trace.descendants.length, visited);
254+
}
255+
}

0 commit comments

Comments
 (0)