@jdeighan/coffee-utils 7.0.50 → 7.0.51
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +1 -1
- package/src/arrow.coffee +40 -4
- package/src/arrow.js +46 -5
- package/src/call_stack.coffee +35 -42
- package/src/call_stack.js +40 -42
- package/src/coffee_utils.coffee +12 -0
- package/src/coffee_utils.js +11 -0
- package/src/debug_utils.coffee +113 -128
- package/src/debug_utils.js +148 -127
- package/src/log_utils.coffee +47 -24
- package/src/log_utils.js +46 -25
package/src/debug_utils.coffee
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
import {
|
4
4
|
assert, undef, error, croak, warn, defined,
|
5
|
-
isString, isFunction, isBoolean,
|
5
|
+
isString, isFunction, isBoolean, sep_dash,
|
6
6
|
OL, escapeStr, isNumber, isArray, words, pass,
|
7
7
|
} from '@jdeighan/coffee-utils'
|
8
8
|
import {blockToArray} from '@jdeighan/coffee-utils/block'
|
@@ -10,16 +10,15 @@ import {untabify} from '@jdeighan/coffee-utils/indent'
|
|
10
10
|
import {slurp} from '@jdeighan/coffee-utils/fs'
|
11
11
|
import {CallStack} from '@jdeighan/coffee-utils/stack'
|
12
12
|
import {
|
13
|
-
|
13
|
+
getPrefix, addArrow, removeLastVbar,
|
14
|
+
} from '@jdeighan/coffee-utils/arrow'
|
15
|
+
import {
|
16
|
+
log, logItem, LOG, shortEnough,
|
14
17
|
} from '@jdeighan/coffee-utils/log'
|
15
18
|
|
16
|
-
|
17
|
-
export debugging = false
|
18
|
-
shouldLogFunc = (func) -> return debugging
|
19
|
-
shouldLogString = (str) -> return debugging
|
20
|
-
|
21
|
-
stack = new CallStack()
|
19
|
+
callStack = new CallStack()
|
22
20
|
doDebugDebug = false
|
21
|
+
shouldLog = undef # set in resetDebugging() and setDebugging()
|
23
22
|
|
24
23
|
# ---------------------------------------------------------------------------
|
25
24
|
|
@@ -34,49 +33,47 @@ export debugDebug = (flag=true) ->
|
|
34
33
|
|
35
34
|
resetDebugging = () ->
|
36
35
|
|
37
|
-
debugging = false
|
38
36
|
if doDebugDebug
|
39
|
-
LOG "resetDebugging()
|
40
|
-
|
41
|
-
|
42
|
-
shouldLogString = (str) -> return debugging
|
37
|
+
LOG "resetDebugging()"
|
38
|
+
callStack.reset()
|
39
|
+
shouldLog = (type, str, stack) -> false
|
43
40
|
return
|
44
41
|
|
45
42
|
# ---------------------------------------------------------------------------
|
46
43
|
|
47
|
-
export setDebugging = (
|
44
|
+
export setDebugging = (option) ->
|
48
45
|
|
49
46
|
resetDebugging()
|
50
|
-
if isBoolean(
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
47
|
+
if isBoolean(option)
|
48
|
+
shouldLog = (type, str, stack) -> option
|
49
|
+
else if isString(option)
|
50
|
+
shouldLog = (type, str, stack) ->
|
51
|
+
lFuncs = words(option)
|
52
|
+
switch type
|
53
|
+
when 'enter'
|
54
|
+
return funcMatch(stack, lFuncs, str)
|
55
|
+
else
|
56
|
+
return funcMatch(stack, lFuncs)
|
60
57
|
if doDebugDebug
|
61
|
-
LOG "setDebugging FUNCS: #{
|
62
|
-
else if isFunction(
|
63
|
-
|
58
|
+
LOG "setDebugging FUNCS: #{option}"
|
59
|
+
else if isFunction(option)
|
60
|
+
shouldLog = option
|
64
61
|
if doDebugDebug
|
65
62
|
LOG "setDebugging to custom func"
|
66
63
|
else
|
67
|
-
croak "setDebugging(): bad parameter #{OL(
|
68
|
-
|
69
|
-
if isFunction(funcDoLog)
|
70
|
-
assert isFunction(funcDoLog), "setDebugging: arg 2 not a function"
|
71
|
-
shouldLogString = funcDoLog
|
64
|
+
croak "setDebugging(): bad parameter #{OL(option)}"
|
72
65
|
return
|
73
66
|
|
74
67
|
# ---------------------------------------------------------------------------
|
75
68
|
# --- export only to allow unit tests
|
76
69
|
|
77
|
-
export funcMatch = (
|
70
|
+
export funcMatch = (stack, lFuncNames, enteringFunc=undef) ->
|
71
|
+
|
72
|
+
if defined(enteringFunc) && (enteringFunc in lFuncNames)
|
73
|
+
return true
|
78
74
|
|
79
|
-
|
75
|
+
curFunc = stack.curFunc()
|
76
|
+
assert isString(curFunc), "funcMatch(): not a string #{OL(curFunc)}"
|
80
77
|
assert isArray(lFuncNames), "funcMatch(): bad array #{lFuncNames}"
|
81
78
|
if lFuncNames.includes(curFunc)
|
82
79
|
return true
|
@@ -88,128 +85,116 @@ export funcMatch = (curFunc, lFuncNames) ->
|
|
88
85
|
return false
|
89
86
|
|
90
87
|
# ---------------------------------------------------------------------------
|
91
|
-
#
|
92
|
-
# 2. adjust debugging flag
|
93
|
-
# 3. return [mainPrefix, auxPrefix, hEnv, type] - hEnv can be undef
|
94
|
-
# 4. disable logging by setting type to undef
|
88
|
+
# --- type is one of: 'enter', 'return', 'string', 'object'
|
95
89
|
|
96
|
-
|
90
|
+
export getType = (str, nObjects) ->
|
97
91
|
|
98
|
-
if
|
92
|
+
if lMatches = str.match(///^
|
99
93
|
\s*
|
100
94
|
enter
|
101
95
|
\s+
|
102
96
|
([A-Za-z_][A-Za-z0-9_\.]*)
|
103
|
-
///)
|
97
|
+
///)
|
104
98
|
|
105
99
|
# --- We are entering function curFunc
|
106
|
-
|
100
|
+
return ['enter', lMatches[1]]
|
107
101
|
|
108
|
-
|
109
|
-
debugging
|
110
|
-
shouldLogFunc
|
111
|
-
shouldLogString
|
112
|
-
}
|
113
|
-
|
114
|
-
debugging = shouldLogFunc(curFunc)
|
115
|
-
if doDebugDebug
|
116
|
-
trans = "#{hEnv.debugging} => #{debugging}"
|
117
|
-
LOG " ENTER #{curFunc}, debugging: #{trans}"
|
118
|
-
[mainPre, auxPre] = stack.doCall(curFunc, hEnv, debugging)
|
119
|
-
return [
|
120
|
-
mainPre
|
121
|
-
auxPre
|
122
|
-
undef
|
123
|
-
if shouldLogFunc(curFunc) then 'enter' else undef
|
124
|
-
]
|
125
|
-
else if (lMatches = str.match(///^
|
102
|
+
else if lMatches = str.match(///^
|
126
103
|
\s*
|
127
104
|
return
|
128
105
|
.+
|
129
106
|
from
|
130
107
|
\s+
|
131
108
|
([A-Za-z_][A-Za-z0-9_\.]*)
|
132
|
-
///)
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
return [
|
138
|
-
mainPre
|
139
|
-
auxPre
|
140
|
-
hEnv
|
141
|
-
if shouldLogFunc(curFunc) then 'return' else undef
|
142
|
-
]
|
109
|
+
///)
|
110
|
+
return ['return', lMatches[1]]
|
111
|
+
|
112
|
+
else if (nObjects > 0)
|
113
|
+
return ['objects', undef]
|
143
114
|
else
|
144
|
-
[
|
145
|
-
return [
|
146
|
-
mainPre
|
147
|
-
auxPre
|
148
|
-
undef
|
149
|
-
if shouldLogString(str) then 'string' else undef
|
150
|
-
]
|
115
|
+
return ['string', undef]
|
151
116
|
|
152
117
|
# ---------------------------------------------------------------------------
|
153
118
|
|
154
|
-
export debug = (
|
119
|
+
export debug = (label, lObjects...) ->
|
155
120
|
|
156
|
-
|
157
|
-
# distinguish between 1 arg sent vs. 2 args sent
|
158
|
-
nArgs = lArgs.length
|
159
|
-
assert (nArgs==1) || (nArgs==2), "debug(): #{nArgs} args"
|
121
|
+
assert isString(label), "1st arg #{OL(label)} should be a string"
|
160
122
|
|
161
|
-
# ---
|
162
|
-
#
|
163
|
-
|
164
|
-
assert isString(label),
|
165
|
-
"debug(): 1st arg #{OL(label)} should be a string"
|
166
|
-
|
167
|
-
if doDebugDebug
|
168
|
-
if nArgs==1
|
169
|
-
LOG "debug('#{escapeStr(label)}') - 1 arg"
|
170
|
-
else
|
171
|
-
LOG "debug('#{escapeStr(label)}', #{typeof item}) - 2 args"
|
172
|
-
LOG "debugging flag = #{OL(debugging)}"
|
173
|
-
|
174
|
-
# --- We always need to manipulate the stack when we encounter
|
175
|
-
# either "enter X" or "return from X", so we can't short-circuit
|
176
|
-
# when debugging is off
|
177
|
-
|
178
|
-
lResult = adjustStack(label)
|
179
|
-
if doDebugDebug
|
180
|
-
LOG 'lResult', lResult
|
181
|
-
[mainPre, auxPre, hEnv, type] = lResult
|
182
|
-
if doDebugDebug && (type == undef)
|
183
|
-
LOG "type is undef - NOT LOGGING"
|
184
|
-
|
185
|
-
hOptions = {
|
186
|
-
prefix: mainPre
|
187
|
-
itemPrefix: auxPre
|
188
|
-
}
|
123
|
+
# --- We want to allow objects to be undef. Therefore, we need to
|
124
|
+
# distinguish between 1 arg sent vs. 2 or more args sent
|
125
|
+
nObjects = lObjects.length
|
189
126
|
|
127
|
+
# --- funcName is only set for types 'enter' and 'return'
|
128
|
+
[type, funcName] = getType(label, nObjects)
|
190
129
|
switch type
|
191
130
|
when 'enter'
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
131
|
+
doLog = shouldLog(type, funcName, callStack)
|
132
|
+
|
133
|
+
# --- If we won't be logging when funcName is activated
|
134
|
+
# then change 'enter' to 'call'
|
135
|
+
callStack.enter funcName # add to call stack
|
136
|
+
if ! shouldLog('string', 'abc', callStack)
|
137
|
+
label = label.replace('enter', 'call')
|
138
|
+
callStack.returnFrom funcName # remove from call stack
|
196
139
|
when 'return'
|
197
|
-
|
198
|
-
if (nArgs==2)
|
199
|
-
# --- don't repeat the label
|
200
|
-
logItem undef, item, hOptions
|
140
|
+
doLog = shouldLog(type, funcName, callStack)
|
201
141
|
when 'string'
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
142
|
+
doLog = shouldLog(type, label, callStack)
|
143
|
+
assert (nObjects == 0),
|
144
|
+
"multiple objects only not allowed for #{OL(type)}"
|
145
|
+
when 'objects'
|
146
|
+
doLog = shouldLog(type, label, callStack)
|
147
|
+
assert (nObjects > 0),
|
148
|
+
"multiple objects only not allowed for #{OL(type)}"
|
149
|
+
|
150
|
+
if doDebugDebug
|
151
|
+
if nObjects == 0
|
152
|
+
LOG "debug(#{OL(label)}) - 1 arg"
|
153
|
+
else
|
154
|
+
LOG "debug(#{OL(label)}), #{nObjects} args"
|
155
|
+
LOG "doLog = #{OL(doLog)}"
|
156
|
+
LOG "type = #{OL(type)}"
|
157
|
+
LOG "funcName = #{OL(funcName)}"
|
158
|
+
|
159
|
+
if doLog
|
160
|
+
level = callStack.getLevel()
|
161
|
+
prefix = getPrefix(level)
|
206
162
|
|
207
|
-
if hEnv
|
208
|
-
orgDebugging = debugging
|
209
|
-
{debugging, shouldLogFunc, shouldLogString} = hEnv
|
210
163
|
if doDebugDebug
|
211
|
-
|
212
|
-
LOG "
|
164
|
+
LOG "callStack", callStack
|
165
|
+
LOG "level = #{OL(level)}"
|
166
|
+
LOG "prefix = #{OL(prefix)}"
|
167
|
+
|
168
|
+
switch type
|
169
|
+
when 'enter'
|
170
|
+
log label, {prefix}
|
171
|
+
for obj,i in lObjects
|
172
|
+
if (i > 0)
|
173
|
+
log sep_dash, {prefix: removeLastVbar(prefix)}
|
174
|
+
logItem undef, obj, {prefix: removeLastVbar(prefix)}
|
175
|
+
when 'return'
|
176
|
+
log label, {prefix: addArrow(prefix)}
|
177
|
+
for obj,i in lObjects
|
178
|
+
if (i > 0)
|
179
|
+
log sep_dash, {prefix: removeLastVbar(prefix)}
|
180
|
+
logItem undef, obj, {prefix: removeLastVbar(prefix)}
|
181
|
+
when 'string'
|
182
|
+
log label, {prefix}
|
183
|
+
when 'objects'
|
184
|
+
if (nObjects==1) && shortEnough(label, lObjects[0])
|
185
|
+
logItem label, lObjects[0], {prefix}
|
186
|
+
else
|
187
|
+
if (label.indexOf(':') != label.length - 1)
|
188
|
+
label += ':'
|
189
|
+
log label, {prefix}
|
190
|
+
for obj in lObjects
|
191
|
+
logItem undef, obj, {prefix}
|
192
|
+
|
193
|
+
if (type == 'enter')
|
194
|
+
callStack.enter funcName, doLog
|
195
|
+
else if (type == 'return')
|
196
|
+
callStack.returnFrom funcName
|
197
|
+
|
213
198
|
return true # allow use in boolean expressions
|
214
199
|
|
215
200
|
# ---------------------------------------------------------------------------
|
package/src/debug_utils.js
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
// Generated by CoffeeScript 2.7.0
|
2
|
-
// debug_utils.coffee
|
3
|
-
var
|
2
|
+
// debug_utils.coffee
|
3
|
+
var callStack, doDebugDebug, reMethod, resetDebugging, shouldLog,
|
4
|
+
indexOf = [].indexOf;
|
4
5
|
|
5
6
|
import {
|
6
7
|
assert,
|
@@ -12,6 +13,7 @@ import {
|
|
12
13
|
isString,
|
13
14
|
isFunction,
|
14
15
|
isBoolean,
|
16
|
+
sep_dash,
|
15
17
|
OL,
|
16
18
|
escapeStr,
|
17
19
|
isNumber,
|
@@ -36,28 +38,25 @@ import {
|
|
36
38
|
CallStack
|
37
39
|
} from '@jdeighan/coffee-utils/stack';
|
38
40
|
|
41
|
+
import {
|
42
|
+
getPrefix,
|
43
|
+
addArrow,
|
44
|
+
removeLastVbar
|
45
|
+
} from '@jdeighan/coffee-utils/arrow';
|
46
|
+
|
39
47
|
import {
|
40
48
|
log,
|
41
49
|
logItem,
|
42
50
|
LOG,
|
43
|
-
|
44
|
-
orderedStringify
|
51
|
+
shortEnough
|
45
52
|
} from '@jdeighan/coffee-utils/log';
|
46
53
|
|
47
|
-
|
48
|
-
export var debugging = false;
|
54
|
+
callStack = new CallStack();
|
49
55
|
|
50
|
-
|
51
|
-
return debugging;
|
52
|
-
};
|
53
|
-
|
54
|
-
shouldLogString = function(str) {
|
55
|
-
return debugging;
|
56
|
-
};
|
56
|
+
doDebugDebug = false;
|
57
57
|
|
58
|
-
|
58
|
+
shouldLog = undef; // set in resetDebugging() and setDebugging()
|
59
59
|
|
60
|
-
doDebugDebug = false;
|
61
60
|
|
62
61
|
// ---------------------------------------------------------------------------
|
63
62
|
export var debugDebug = function(flag = true) {
|
@@ -69,57 +68,55 @@ export var debugDebug = function(flag = true) {
|
|
69
68
|
|
70
69
|
// ---------------------------------------------------------------------------
|
71
70
|
resetDebugging = function() {
|
72
|
-
debugging = false;
|
73
71
|
if (doDebugDebug) {
|
74
|
-
LOG("resetDebugging()
|
72
|
+
LOG("resetDebugging()");
|
75
73
|
}
|
76
|
-
|
77
|
-
|
78
|
-
return
|
79
|
-
};
|
80
|
-
shouldLogString = function(str) {
|
81
|
-
return debugging;
|
74
|
+
callStack.reset();
|
75
|
+
shouldLog = function(type, str, stack) {
|
76
|
+
return false;
|
82
77
|
};
|
83
78
|
};
|
84
79
|
|
85
80
|
// ---------------------------------------------------------------------------
|
86
|
-
export var setDebugging = function(
|
87
|
-
var lFuncNames;
|
81
|
+
export var setDebugging = function(option) {
|
88
82
|
resetDebugging();
|
89
|
-
if (isBoolean(
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
83
|
+
if (isBoolean(option)) {
|
84
|
+
shouldLog = function(type, str, stack) {
|
85
|
+
return option;
|
86
|
+
};
|
87
|
+
} else if (isString(option)) {
|
88
|
+
shouldLog = function(type, str, stack) {
|
89
|
+
var lFuncs;
|
90
|
+
lFuncs = words(option);
|
91
|
+
switch (type) {
|
92
|
+
case 'enter':
|
93
|
+
return funcMatch(stack, lFuncs, str);
|
94
|
+
default:
|
95
|
+
return funcMatch(stack, lFuncs);
|
96
|
+
}
|
100
97
|
};
|
101
98
|
if (doDebugDebug) {
|
102
|
-
LOG(`setDebugging FUNCS: ${
|
99
|
+
LOG(`setDebugging FUNCS: ${option}`);
|
103
100
|
}
|
104
|
-
} else if (isFunction(
|
105
|
-
|
101
|
+
} else if (isFunction(option)) {
|
102
|
+
shouldLog = option;
|
106
103
|
if (doDebugDebug) {
|
107
104
|
LOG("setDebugging to custom func");
|
108
105
|
}
|
109
106
|
} else {
|
110
|
-
croak(`setDebugging(): bad parameter ${OL(
|
111
|
-
}
|
112
|
-
if (isFunction(funcDoLog)) {
|
113
|
-
assert(isFunction(funcDoLog), "setDebugging: arg 2 not a function");
|
114
|
-
shouldLogString = funcDoLog;
|
107
|
+
croak(`setDebugging(): bad parameter ${OL(option)}`);
|
115
108
|
}
|
116
109
|
};
|
117
110
|
|
118
111
|
// ---------------------------------------------------------------------------
|
119
112
|
// --- export only to allow unit tests
|
120
|
-
export var funcMatch = function(
|
121
|
-
var _, cls, lMatches, meth;
|
122
|
-
|
113
|
+
export var funcMatch = function(stack, lFuncNames, enteringFunc = undef) {
|
114
|
+
var _, cls, curFunc, lMatches, meth;
|
115
|
+
if (defined(enteringFunc) && (indexOf.call(lFuncNames, enteringFunc) >= 0)) {
|
116
|
+
return true;
|
117
|
+
}
|
118
|
+
curFunc = stack.curFunc();
|
119
|
+
assert(isString(curFunc), `funcMatch(): not a string ${OL(curFunc)}`);
|
123
120
|
assert(isArray(lFuncNames), `funcMatch(): bad array ${lFuncNames}`);
|
124
121
|
if (lFuncNames.includes(curFunc)) {
|
125
122
|
return true;
|
@@ -131,99 +128,123 @@ export var funcMatch = function(curFunc, lFuncNames) {
|
|
131
128
|
};
|
132
129
|
|
133
130
|
// ---------------------------------------------------------------------------
|
134
|
-
//
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
adjustStack = function(str) {
|
139
|
-
var _, auxPre, curFunc, hEnv, lMatches, mainPre, trans;
|
140
|
-
if ((lMatches = str.match(/^\s*enter\s+([A-Za-z_][A-Za-z0-9_\.]*)/))) {
|
131
|
+
// --- type is one of: 'enter', 'return', 'string', 'object'
|
132
|
+
export var getType = function(str, nObjects) {
|
133
|
+
var lMatches;
|
134
|
+
if (lMatches = str.match(/^\s*enter\s+([A-Za-z_][A-Za-z0-9_\.]*)/)) {
|
141
135
|
// --- We are entering function curFunc
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
LOG(` ENTER ${curFunc}, debugging: ${trans}`);
|
148
|
-
}
|
149
|
-
[mainPre, auxPre] = stack.doCall(curFunc, hEnv, debugging);
|
150
|
-
return [mainPre, auxPre, undef, shouldLogFunc(curFunc) ? 'enter' : undef];
|
151
|
-
} else if ((lMatches = str.match(/^\s*return.+from\s+([A-Za-z_][A-Za-z0-9_\.]*)/))) {
|
152
|
-
curFunc = lMatches[1];
|
153
|
-
[mainPre, auxPre, hEnv] = stack.returnFrom(curFunc);
|
154
|
-
if (doDebugDebug) {
|
155
|
-
LOG(` RETURN FROM ${curFunc}`);
|
156
|
-
}
|
157
|
-
return [mainPre, auxPre, hEnv, shouldLogFunc(curFunc) ? 'return' : undef];
|
136
|
+
return ['enter', lMatches[1]];
|
137
|
+
} else if (lMatches = str.match(/^\s*return.+from\s+([A-Za-z_][A-Za-z0-9_\.]*)/)) {
|
138
|
+
return ['return', lMatches[1]];
|
139
|
+
} else if (nObjects > 0) {
|
140
|
+
return ['objects', undef];
|
158
141
|
} else {
|
159
|
-
[
|
160
|
-
return [mainPre, auxPre, undef, shouldLogString(str) ? 'string' : undef];
|
142
|
+
return ['string', undef];
|
161
143
|
}
|
162
144
|
};
|
163
145
|
|
164
146
|
// ---------------------------------------------------------------------------
|
165
|
-
export var debug = function(...
|
166
|
-
var
|
167
|
-
|
168
|
-
//
|
169
|
-
|
170
|
-
|
171
|
-
// ---
|
172
|
-
|
173
|
-
[label, item] = lArgs;
|
174
|
-
assert(isString(label), `debug(): 1st arg ${OL(label)} should be a string`);
|
175
|
-
if (doDebugDebug) {
|
176
|
-
if (nArgs === 1) {
|
177
|
-
LOG(`debug('${escapeStr(label)}') - 1 arg`);
|
178
|
-
} else {
|
179
|
-
LOG(`debug('${escapeStr(label)}', ${typeof item}) - 2 args`);
|
180
|
-
}
|
181
|
-
LOG(`debugging flag = ${OL(debugging)}`);
|
182
|
-
}
|
183
|
-
// --- We always need to manipulate the stack when we encounter
|
184
|
-
// either "enter X" or "return from X", so we can't short-circuit
|
185
|
-
// when debugging is off
|
186
|
-
lResult = adjustStack(label);
|
187
|
-
if (doDebugDebug) {
|
188
|
-
LOG('lResult', lResult);
|
189
|
-
}
|
190
|
-
[mainPre, auxPre, hEnv, type] = lResult;
|
191
|
-
if (doDebugDebug && (type === undef)) {
|
192
|
-
LOG("type is undef - NOT LOGGING");
|
193
|
-
}
|
194
|
-
hOptions = {
|
195
|
-
prefix: mainPre,
|
196
|
-
itemPrefix: auxPre
|
197
|
-
};
|
147
|
+
export var debug = function(label, ...lObjects) {
|
148
|
+
var doLog, funcName, i, j, k, l, len1, len2, len3, level, nObjects, obj, prefix, type;
|
149
|
+
assert(isString(label), `1st arg ${OL(label)} should be a string`);
|
150
|
+
// --- We want to allow objects to be undef. Therefore, we need to
|
151
|
+
// distinguish between 1 arg sent vs. 2 or more args sent
|
152
|
+
nObjects = lObjects.length;
|
153
|
+
// --- funcName is only set for types 'enter' and 'return'
|
154
|
+
[type, funcName] = getType(label, nObjects);
|
198
155
|
switch (type) {
|
199
156
|
case 'enter':
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
157
|
+
doLog = shouldLog(type, funcName, callStack);
|
158
|
+
// --- If we won't be logging when funcName is activated
|
159
|
+
// then change 'enter' to 'call'
|
160
|
+
callStack.enter(funcName); // add to call stack
|
161
|
+
if (!shouldLog('string', 'abc', callStack)) {
|
162
|
+
label = label.replace('enter', 'call');
|
204
163
|
}
|
164
|
+
callStack.returnFrom(funcName); // remove from call stack
|
205
165
|
break;
|
206
166
|
case 'return':
|
207
|
-
|
208
|
-
if (nArgs === 2) {
|
209
|
-
// --- don't repeat the label
|
210
|
-
logItem(undef, item, hOptions);
|
211
|
-
}
|
167
|
+
doLog = shouldLog(type, funcName, callStack);
|
212
168
|
break;
|
213
169
|
case 'string':
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
170
|
+
doLog = shouldLog(type, label, callStack);
|
171
|
+
assert(nObjects === 0, `multiple objects only not allowed for ${OL(type)}`);
|
172
|
+
break;
|
173
|
+
case 'objects':
|
174
|
+
doLog = shouldLog(type, label, callStack);
|
175
|
+
assert(nObjects > 0, `multiple objects only not allowed for ${OL(type)}`);
|
219
176
|
}
|
220
|
-
if (
|
221
|
-
|
222
|
-
|
177
|
+
if (doDebugDebug) {
|
178
|
+
if (nObjects === 0) {
|
179
|
+
LOG(`debug(${OL(label)}) - 1 arg`);
|
180
|
+
} else {
|
181
|
+
LOG(`debug(${OL(label)}), ${nObjects} args`);
|
182
|
+
}
|
183
|
+
LOG(`doLog = ${OL(doLog)}`);
|
184
|
+
LOG(`type = ${OL(type)}`);
|
185
|
+
LOG(`funcName = ${OL(funcName)}`);
|
186
|
+
}
|
187
|
+
if (doLog) {
|
188
|
+
level = callStack.getLevel();
|
189
|
+
prefix = getPrefix(level);
|
223
190
|
if (doDebugDebug) {
|
224
|
-
|
225
|
-
LOG(`
|
191
|
+
LOG("callStack", callStack);
|
192
|
+
LOG(`level = ${OL(level)}`);
|
193
|
+
LOG(`prefix = ${OL(prefix)}`);
|
226
194
|
}
|
195
|
+
switch (type) {
|
196
|
+
case 'enter':
|
197
|
+
log(label, {prefix});
|
198
|
+
for (i = j = 0, len1 = lObjects.length; j < len1; i = ++j) {
|
199
|
+
obj = lObjects[i];
|
200
|
+
if (i > 0) {
|
201
|
+
log(sep_dash, {
|
202
|
+
prefix: removeLastVbar(prefix)
|
203
|
+
});
|
204
|
+
}
|
205
|
+
logItem(undef, obj, {
|
206
|
+
prefix: removeLastVbar(prefix)
|
207
|
+
});
|
208
|
+
}
|
209
|
+
break;
|
210
|
+
case 'return':
|
211
|
+
log(label, {
|
212
|
+
prefix: addArrow(prefix)
|
213
|
+
});
|
214
|
+
for (i = k = 0, len2 = lObjects.length; k < len2; i = ++k) {
|
215
|
+
obj = lObjects[i];
|
216
|
+
if (i > 0) {
|
217
|
+
log(sep_dash, {
|
218
|
+
prefix: removeLastVbar(prefix)
|
219
|
+
});
|
220
|
+
}
|
221
|
+
logItem(undef, obj, {
|
222
|
+
prefix: removeLastVbar(prefix)
|
223
|
+
});
|
224
|
+
}
|
225
|
+
break;
|
226
|
+
case 'string':
|
227
|
+
log(label, {prefix});
|
228
|
+
break;
|
229
|
+
case 'objects':
|
230
|
+
if ((nObjects === 1) && shortEnough(label, lObjects[0])) {
|
231
|
+
logItem(label, lObjects[0], {prefix});
|
232
|
+
} else {
|
233
|
+
if (label.indexOf(':') !== label.length - 1) {
|
234
|
+
label += ':';
|
235
|
+
}
|
236
|
+
log(label, {prefix});
|
237
|
+
for (l = 0, len3 = lObjects.length; l < len3; l++) {
|
238
|
+
obj = lObjects[l];
|
239
|
+
logItem(undef, obj, {prefix});
|
240
|
+
}
|
241
|
+
}
|
242
|
+
}
|
243
|
+
}
|
244
|
+
if (type === 'enter') {
|
245
|
+
callStack.enter(funcName, doLog);
|
246
|
+
} else if (type === 'return') {
|
247
|
+
callStack.returnFrom(funcName);
|
227
248
|
}
|
228
249
|
return true; // allow use in boolean expressions
|
229
250
|
};
|
@@ -234,12 +255,12 @@ reMethod = /^([A-Za-z_][A-Za-z0-9_]*)\.([A-Za-z_][A-Za-z0-9_]*)$/;
|
|
234
255
|
|
235
256
|
// ---------------------------------------------------------------------------
|
236
257
|
export var checkTrace = function(block) {
|
237
|
-
var funcName,
|
258
|
+
var funcName, j, lMatches, lStack, len, len1, line, ref;
|
238
259
|
// --- export only to allow unit tests
|
239
260
|
lStack = [];
|
240
261
|
ref = blockToArray(block);
|
241
|
-
for (
|
242
|
-
line = ref[
|
262
|
+
for (j = 0, len1 = ref.length; j < len1; j++) {
|
263
|
+
line = ref[j];
|
243
264
|
if (lMatches = line.match(/enter\s+([A-Za-z_][A-Za-z0-9_\.]*)/)) {
|
244
265
|
funcName = lMatches[1];
|
245
266
|
lStack.push(funcName);
|