-
Notifications
You must be signed in to change notification settings - Fork 0
/
getJsStack.js
92 lines (60 loc) · 2.63 KB
/
getJsStack.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/***************************************************************
Github Repo: https://github.com/OrvilleChomer/getJsStack
Released: Monday, Jan 7, 2019
File: getJsStack.js
***************************************************************/
/***************************************************************
Call this function in your code when you need the call
stack data as it currently stands.
Returns an object which has these properties:
.timestamp - when this function was called for this info
.stackByIndex[] - an indexed array of {stackEntry} objects.
Each {stackEntry} object has the following properties:
.funcName - name of the function that was called.
.jsFileUrl - path to the file that contains the line
of code that is calling the function.
.stackDepth - what level down in the stack is this call.
.lineNum - the line# in the file where the function is being called.
.column - column# in file where function call starts at.
***************************************************************/
function getJsStack() {
try {
thisWillThrowAnError.everyTime();
} catch(err) {
var stackDta = err.stack.split("\n");
var stackInfo = {};
var stack = [];
var nMax = stackDta.length;
var sValue,nPos,sValue2;
var n,stackEntry;
stackInfo.timestamp = new Date();
if (nMax>1) {
for (n=1;n<nMax;n++) {
sValue = stackDta[n];
stackEntry = {};
stackEntry.funcName = sValue.split("@")[0];
sValue = sValue.split("@")[1];
stackEntry.jsFileUrl = "";
stackEntry.stackDepth = nMax - n;
nPos = sValue.indexOf("?");
if (nPos > -1) {
stackEntry.jsFileUrl = sValue.substr(0,nPos);
} // end if
sValue = sValue.split(":");
if (stackEntry.jsFileUrl === "") {
// protocol plus rest of main url
sValue2 = sValue[0] + ":" + sValue[1];
if (sValue.length-2 > 2) {
sValue2 = sValue2 + ":" + sValue[2]; // handle port if there
} // end if
stackEntry.jsFileUrl = sValue2;
} // end if
stackEntry.column = sValue[sValue.length-1]-0;
stackEntry.lineNum = sValue[sValue.length-2]-0;
stack[stack.length] = stackEntry;
} // next n
} // end if
stackInfo.stackByIndex = stack.reverse();
return stackInfo;
} // end try/catch
} // end of function getJsStack()