-
-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathexample-trace.js
More file actions
37 lines (31 loc) · 1.31 KB
/
example-trace.js
File metadata and controls
37 lines (31 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
'use strict';
const asyncWrap = process.binding('async_wrap');
asyncWrap.setupHooks(init, before, after);
asyncWrap.enable();
// global state variable, that contains the current stack trace
let currentStack = '';
function init(provider, parent) {
// When a handle is created, collect the stack trace such that we later
// can see what involved the handle constructor.
const localStack = (new Error()).stack.split('\n').slice(1).join('\n');
// Compute the full stack and store it as a property on the handle object,
// such that it can be fetched later.
const extraStack = parent ? parent._full_init_stack : currentStack;
this._full_init_stack = localStack + '\n' + extraStack;
}
function before() {
// A callback is about to be called, update the `currentStack` such that
// it is correct for when another handle is initialized or `getStack` is called.
currentStack = this._full_init_stack;
}
function after() {
// At the time of writing there are some odd cases where there is no handle
// context, this line prevents that from resulting in wrong stack trace. But
// the stack trace will be shorter compared to what ideally should happen.
currentStack = '';
}
function getStack(message) {
const localStack = new Error(message);
return localStack.stack + '\n' + currentStack;
}
module.exports = getStack;