@jdeighan/coffee-utils 7.0.60 → 7.0.61

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  // Generated by CoffeeScript 2.7.0
2
2
  // debug_utils.coffee
3
- var debugDebug, doDebugDebug, lFuncList, lFunctions, reMethod, strFuncList,
3
+ var lFuncList, reMethod, strFuncList,
4
4
  indexOf = [].indexOf;
5
5
 
6
6
  import {
@@ -13,6 +13,7 @@ import {
13
13
  isString,
14
14
  isFunction,
15
15
  isBoolean,
16
+ patchStr,
16
17
  OL,
17
18
  escapeStr,
18
19
  isNumber,
@@ -38,17 +39,18 @@ import {
38
39
  } from '@jdeighan/coffee-utils/stack';
39
40
 
40
41
  import {
41
- getPrefix,
42
+ prefix,
42
43
  addArrow,
43
- removeLastVbar
44
+ removeLastVbar,
45
+ vbar
44
46
  } from '@jdeighan/coffee-utils/arrow';
45
47
 
46
48
  import {
47
49
  log,
48
50
  logItem,
51
+ logBareItem,
49
52
  LOG,
50
- shortEnough,
51
- dashes
53
+ shortEnough
52
54
  } from '@jdeighan/coffee-utils/log';
53
55
 
54
56
  // --- set in resetDebugging() and setDebugging()
@@ -58,122 +60,70 @@ export var shouldLog = function() {
58
60
  return undef;
59
61
  };
60
62
 
61
- lFuncList = [];
63
+ lFuncList = []; // names of functions being debugged
62
64
 
63
65
  strFuncList = undef; // original string
64
66
 
65
67
 
66
- // --- internal debugging
67
- doDebugDebug = false;
68
-
69
- lFunctions = undef; // --- only used when doDebugDebug is true
70
-
71
-
72
- // ---------------------------------------------------------------------------
73
- export var dumpCallStack = function() {
74
- LOG(callStack.dump());
75
- };
76
-
77
- // ---------------------------------------------------------------------------
78
- export var setDebugDebugging = function(value = true) {
79
- // --- value can be a boolean or string of words
80
- if (isBoolean(value)) {
81
- doDebugDebug = value;
82
- } else if (isString(value)) {
83
- doDebugDebug = true;
84
- lFunctions = words(value);
85
- } else {
86
- croak(`Bad value: ${OL(value)}`);
87
- }
88
- };
89
-
90
68
  // ---------------------------------------------------------------------------
91
- debugDebug = function(label, ...lObjects) {
92
- var doLog, dolog, funcName, nObjects, type;
93
- if (!doDebugDebug) {
94
- return;
95
- }
96
- // --- At this point, doDebugDebug is true
97
- doDebugDebug = false; // temp - reset before returning
98
- assert(isString(label), `1st arg ${OL(label)} should be a string`);
99
- nObjects = lObjects.length;
100
- [type, funcName] = getType(label, nObjects);
69
+ export var debug = function(orgLabel, ...lObjects) {
70
+ var funcName, label, type;
71
+ assert(isString(orgLabel), `1st arg ${OL(orgLabel)} should be a string`);
72
+ [type, funcName] = getType(orgLabel, lObjects);
73
+ label = shouldLog(orgLabel, type, funcName, callStack);
101
74
  switch (type) {
102
75
  case 'enter':
103
- assert(defined(funcName), "type enter, funcName = undef");
104
- callStack.enter(funcName, lObjects);
105
- doLog = (lFunctions === undef) || (indexOf.call(lFunctions, funcName) >= 0);
76
+ if (defined(label)) {
77
+ doTheLogging(type, label, lObjects);
78
+ }
79
+ callStack.enter(funcName, lObjects, defined(label));
80
+ debug2("enter debug()", orgLabel, lObjects);
81
+ debug2(`type = ${OL(type)}, funcName = ${OL(funcName)}`);
82
+ debug2("return from debug()");
106
83
  break;
107
84
  case 'return':
108
- assert(defined(funcName), "type return, funcName = undef");
109
- doLog = (lFunctions === undef) || (indexOf.call(lFunctions, funcName) >= 0);
85
+ debug2("enter debug()", orgLabel, lObjects);
86
+ debug2(`type = ${OL(type)}, funcName = ${OL(funcName)}`);
87
+ debug2("return from debug()");
88
+ if (defined(label)) {
89
+ doTheLogging(type, label, lObjects);
90
+ }
91
+ callStack.returnFrom(funcName);
110
92
  break;
111
93
  case 'string':
112
- assert(funcName === undef, "type string, funcName defined");
113
- assert(nObjects === 0, `Objects not allowed for ${OL(type)}`);
114
- doLog = true;
115
- break;
116
- case 'objects':
117
- assert(funcName === undef, "type objects, funcName defined");
118
- assert(nObjects > 0, `Objects required for ${OL(type)}`);
119
- dolog = true;
120
- }
121
- if (doLog) {
122
- doTheLogging(type, label, lObjects);
123
- }
124
- if ((type === 'enter') && doLog) {
125
- callStack.logCurFunc(funcName);
126
- } else if (type === 'return') {
127
- callStack.returnFrom(funcName);
94
+ debug2("enter debug()", orgLabel, lObjects);
95
+ debug2(`type = ${OL(type)}, funcName = ${OL(funcName)}`);
96
+ if (defined(label)) {
97
+ doTheLogging(type, label, lObjects);
98
+ }
99
+ debug2("return from debug()");
128
100
  }
129
- doDebugDebug = true;
101
+ return true; // allow use in boolean expressions
130
102
  };
131
103
 
104
+
132
105
  // ---------------------------------------------------------------------------
133
- export var debug = function(label, ...lObjects) {
134
- var doLog, funcName, nObjects, type;
135
- assert(isString(label), `1st arg ${OL(label)} should be a string`);
136
- // --- If label is "enter <funcname>, we need to put that on the stack
137
- // BEFORE we do any internal logging
138
- nObjects = lObjects.length;
139
- [type, funcName] = getType(label, nObjects);
140
- if (type === 'enter') {
141
- callStack.enter(funcName, lObjects);
142
- }
143
- debugDebug(`enter debug(${OL(label)})`, ...lObjects);
144
- debugDebug(`type = ${OL(type)}`);
145
- debugDebug(`funcName = ${OL(funcName)}`);
146
- // --- function shouldLog() returns the (possibly modified) label
147
- // if we should log this, else it returns undef
106
+ export var debug2 = function(orgLabel, ...lObjects) {
107
+ var funcName, label, type;
108
+ [type, funcName] = getType(orgLabel, lObjects);
109
+ label = shouldLog(orgLabel, type, funcName, callStack);
148
110
  switch (type) {
149
111
  case 'enter':
150
- label = shouldLog(label, type, funcName, callStack);
112
+ if (defined(label)) {
113
+ doTheLogging('enter', label, lObjects);
114
+ }
115
+ callStack.enter(funcName, lObjects, defined(label));
151
116
  break;
152
117
  case 'return':
153
- label = shouldLog(label, type, funcName, callStack);
118
+ if (defined(label)) {
119
+ doTheLogging('return', label, lObjects);
120
+ }
121
+ callStack.returnFrom(funcName);
154
122
  break;
155
123
  case 'string':
156
- label = shouldLog(label, type, undef, callStack);
157
- assert(nObjects === 0, `Objects not allowed for ${OL(type)}`);
158
- break;
159
- case 'objects':
160
- label = shouldLog(label, type, undef, callStack);
161
- assert(nObjects > 0, `Objects required for ${OL(type)}`);
162
- }
163
- assert((label === undef) || isString(label), `label not a string: ${OL(label)}`);
164
- doLog = defined(label);
165
- debugDebug(`doLog = ${OL(doLog)}`);
166
- debugDebug(`${nObjects} objects`);
167
- if (doLog) {
168
- doTheLogging(type, label, lObjects);
169
- }
170
- if ((type === 'enter') && doLog && (label.indexOf('call') === -1)) {
171
- callStack.logCurFunc(funcName);
172
- }
173
- // --- This must be called BEFORE we return from funcName
174
- debugDebug("return from debug()");
175
- if (type === 'return') {
176
- callStack.returnFrom(funcName);
124
+ if (defined(label)) {
125
+ doTheLogging('string', label, lObjects);
126
+ }
177
127
  }
178
128
  return true; // allow use in boolean expressions
179
129
  };
@@ -181,54 +131,44 @@ export var debug = function(label, ...lObjects) {
181
131
 
182
132
  // ---------------------------------------------------------------------------
183
133
  export var doTheLogging = function(type, label, lObjects) {
184
- var i, itemPrefix, j, k, l, len, len1, len2, level, obj, prefix, sep;
134
+ var i, itemPre, j, k, l, len, len1, len2, level, obj, pre, sep;
135
+ assert(isString(label), `non-string label ${OL(label)}`);
185
136
  level = callStack.getLevel();
186
- prefix = getPrefix(level);
187
- itemPrefix = removeLastVbar(prefix);
188
- sep = dashes(itemPrefix, 40);
189
- assert(isString(sep), "sep is not a string");
190
- debugDebug("callStack", callStack);
191
- debugDebug(`level = ${OL(level)}`);
192
- debugDebug(`prefix = ${OL(prefix)}`);
193
- debugDebug(`itemPrefix = ${OL(itemPrefix)}`);
194
- debugDebug(`sep = ${OL(sep)}`);
137
+ sep = '-'.repeat(40);
195
138
  switch (type) {
196
139
  case 'enter':
197
- log(label, {prefix});
140
+ log(label, prefix(level));
141
+ pre = prefix(level + 1);
142
+ itemPre = prefix(level + 2, 'noLastVbar');
198
143
  for (i = j = 0, len = lObjects.length; j < len; i = ++j) {
199
144
  obj = lObjects[i];
200
- if (i > 0) {
201
- log(sep);
202
- }
203
- logItem(undef, obj, {itemPrefix});
145
+ logItem(`arg[${i}]`, obj, pre, itemPre);
204
146
  }
205
147
  break;
206
148
  case 'return':
207
- log(label, {
208
- prefix: addArrow(prefix)
209
- });
149
+ log(label, prefix(level, 'withArrow'));
150
+ pre = prefix(level, 'noLastVbar');
151
+ itemPre = prefix(level + 1, 'noLast2Vbars');
210
152
  for (i = k = 0, len1 = lObjects.length; k < len1; i = ++k) {
211
153
  obj = lObjects[i];
212
- if (i > 0) {
213
- log(sep);
214
- }
215
- logItem(undef, obj, {itemPrefix});
154
+ logItem(`ret[${i}]`, obj, pre, itemPre);
216
155
  }
217
156
  break;
218
157
  case 'string':
219
- log(label, {prefix});
220
- break;
221
- case 'objects':
222
- if ((lObjects.length === 1) && shortEnough(label, lObjects[0])) {
223
- logItem(label, lObjects[0], {prefix});
158
+ pre = prefix(level);
159
+ itemPre = prefix(level + 1, 'noLastVbar');
160
+ if (lObjects.length === 0) {
161
+ log(label, pre);
162
+ } else if ((lObjects.length === 1) && shortEnough(label, lObjects[0])) {
163
+ logItem(label, lObjects[0], pre);
224
164
  } else {
225
165
  if (label.indexOf(':') !== label.length - 1) {
226
166
  label += ':';
227
167
  }
228
- log(label, {prefix});
168
+ log(label, pre);
229
169
  for (l = 0, len2 = lObjects.length; l < len2; l++) {
230
170
  obj = lObjects[l];
231
- logItem(undef, obj, {prefix});
171
+ logBareItem(obj, itemPre);
232
172
  }
233
173
  }
234
174
  }
@@ -236,59 +176,54 @@ export var doTheLogging = function(type, label, lObjects) {
236
176
 
237
177
  // ---------------------------------------------------------------------------
238
178
  export var stdShouldLog = function(label, type, funcName, stack) {
239
- var prevLogged, result;
179
+ var result;
240
180
  // --- if type is 'enter', then funcName won't be on the stack yet
241
181
  // returns the (possibly modified) label to log
242
-
243
- // --- If we're logging now,
244
- // but we won't be logging when funcName is activated
245
- // then change 'enter' to 'call'
246
182
  assert(isString(label), `label ${OL(label)} not a string`);
247
183
  assert(isString(type), `type ${OL(type)} not a string`);
184
+ assert(stack instanceof CallStack, "not a call stack object");
248
185
  if ((type === 'enter') || (type === 'return')) {
249
186
  assert(isString(funcName), `func name ${OL(funcName)} not a string`);
250
187
  } else {
251
188
  assert(funcName === undef, `func name ${OL(funcName)} not undef`);
252
189
  }
253
- assert(stack instanceof CallStack, "not a call stack object");
254
- debugDebug(`enter stdShouldLog(${OL(label)}, ${OL(type)}, ${OL(funcName)}, stack)`);
255
- switch (type) {
256
- case 'enter':
257
- if (funcMatch()) {
258
- debugDebug(`return ${OL(label)} from stdShouldLog() - funcMatch`);
259
- return label;
260
- } else {
261
- // --- As a special case, if we enter a function where we will not
262
- // be logging, but we were logging in the calling function,
263
- // we'll log out the call itself
264
- prevLogged = stack.isLoggingPrev();
265
- if (prevLogged) {
266
- result = label.replace('enter', 'call');
267
- debugDebug(`return ${OL(result)} from stdShouldLog() - s/enter/call/`);
268
- return result;
269
- }
270
- }
271
- break;
272
- default:
273
- if (funcMatch()) {
274
- debugDebug(`return ${OL(label)} from stdShouldLog()`);
275
- return label;
276
- }
190
+ if (funcMatch(funcName || stack.curFunc())) {
191
+ return label;
192
+ }
193
+ if ((type === 'enter') && !isMyFunc(funcName)) {
194
+ // --- As a special case, if we enter a function where we will not
195
+ // be logging, but we were logging in the calling function,
196
+ // we'll log out the call itself
197
+ if (funcMatch(stack.curFunc())) {
198
+ result = label.replace('enter', 'call');
199
+ return result;
200
+ }
277
201
  }
278
- debugDebug("return undef from stdShouldLog()");
279
202
  return undef;
280
203
  };
281
204
 
205
+ // ---------------------------------------------------------------------------
206
+ export var isMyFunc = function(funcName) {
207
+ return indexOf.call(words('debug debug2 doTheLogging stdShouldLog setDebugging getFuncList funcMatch getType dumpCallStack'), funcName) >= 0;
208
+ };
209
+
210
+ // ---------------------------------------------------------------------------
211
+ export var trueShouldLog = function(label, type, funcName, stack) {
212
+ if (isMyFunc(funcName || stack.curFunc())) {
213
+ return undef;
214
+ } else {
215
+ return label;
216
+ }
217
+ };
218
+
282
219
  // ---------------------------------------------------------------------------
283
220
  export var setDebugging = function(option) {
284
221
  callStack.reset();
285
222
  if (isBoolean(option)) {
286
223
  if (option) {
287
- shouldLog = function(label, type, funcName, stack) {
288
- return label;
289
- };
224
+ shouldLog = trueShouldLog;
290
225
  } else {
291
- shouldLog = function(label, type, funcName, stack) {
226
+ shouldLog = function() {
292
227
  return undef;
293
228
  };
294
229
  }
@@ -334,40 +269,31 @@ export var getFuncList = function(str) {
334
269
 
335
270
  // ---------------------------------------------------------------------------
336
271
  // --- export only to allow unit tests
337
- export var funcMatch = function() {
338
- var curFunc, h, j, len, name, object, plus;
272
+ export var funcMatch = function(funcName) {
273
+ var h, j, len, name, object, plus;
339
274
  assert(isArray(lFuncList), `not an array ${OL(lFuncList)}`);
340
- debugDebug("enter funcMatch()");
341
- curFunc = callStack.curFunc();
342
- debugDebug(`curFunc = ${OL(curFunc)}`);
343
- debugDebug(`lFuncList = ${strFuncList}`);
344
275
  for (j = 0, len = lFuncList.length; j < len; j++) {
345
276
  h = lFuncList[j];
346
277
  ({name, object, plus} = h);
347
- if (name === curFunc) {
348
- debugDebug("return from funcMatch() - curFunc in lFuncList");
278
+ if (name === funcName) {
349
279
  return true;
350
280
  }
351
281
  if (plus && callStack.isActive(name)) {
352
- debugDebug(`return from funcMatch() - func ${OL(name)} is active`);
353
282
  return true;
354
283
  }
355
284
  }
356
- debugDebug("return from funcMatch() - no match");
357
285
  return false;
358
286
  };
359
287
 
360
288
  // ---------------------------------------------------------------------------
361
- // --- type is one of: 'enter', 'return', 'string', 'object'
362
- export var getType = function(str, nObjects) {
289
+ // --- type is one of: 'enter', 'return', 'string'
290
+ export var getType = function(str, lObjects) {
363
291
  var lMatches;
364
292
  if (lMatches = str.match(/^\s*enter\s+([A-Za-z_][A-Za-z0-9_\.]*)/)) {
365
293
  // --- We are entering function curFunc
366
294
  return ['enter', lMatches[1]];
367
295
  } else if (lMatches = str.match(/^\s*return.+from\s+([A-Za-z_][A-Za-z0-9_\.]*)/)) {
368
296
  return ['return', lMatches[1]];
369
- } else if (nObjects > 0) {
370
- return ['objects', undef];
371
297
  } else {
372
298
  return ['string', undef];
373
299
  }
@@ -377,3 +303,23 @@ export var getType = function(str, nObjects) {
377
303
  reMethod = /^([A-Za-z_][A-Za-z0-9_]*)\.([A-Za-z_][A-Za-z0-9_]*)$/;
378
304
 
379
305
  // ---------------------------------------------------------------------------
306
+ export var dumpDebugGlobals = function() {
307
+ var funcName, j, len;
308
+ LOG('='.repeat(40));
309
+ LOG(callStack.dump());
310
+ if (shouldLog === stdShouldLog) {
311
+ LOG("using stdShouldLog");
312
+ } else if (shouldLog === trueShouldLog) {
313
+ LOG("using trueShouldLog");
314
+ } else {
315
+ LOG("using custom shouldLog");
316
+ }
317
+ LOG("lFuncList:");
318
+ for (j = 0, len = lFuncList.length; j < len; j++) {
319
+ funcName = lFuncList[j];
320
+ LOG(` ${OL(funcName)}`);
321
+ }
322
+ return LOG('='.repeat(40));
323
+ };
324
+
325
+ // ---------------------------------------------------------------------------
@@ -20,12 +20,6 @@ fourSpaces = ' '
20
20
 
21
21
  # ---------------------------------------------------------------------------
22
22
 
23
- export dashes = (prefix, totalLen=64, ch='-') ->
24
-
25
- return prefix + ch.repeat(totalLen - prefix.length)
26
-
27
- # ---------------------------------------------------------------------------
28
-
29
23
  export debugLog = (flag=true) ->
30
24
 
31
25
  doDebugLog = flag
@@ -62,6 +56,15 @@ export DEBUG = LOG # synonym
62
56
 
63
57
  # ---------------------------------------------------------------------------
64
58
 
59
+ export LOGLINES = (label, lLines) ->
60
+
61
+ LOG "#{label}:"
62
+ for line in lLines
63
+ LOG "#{OL(line)}"
64
+ return
65
+
66
+ # ---------------------------------------------------------------------------
67
+
65
68
  export setStringifier = (func) ->
66
69
 
67
70
  orgStringifier = stringify
@@ -109,7 +112,7 @@ export tamlStringify = (obj, escape=false) ->
109
112
  lineWidth: -1
110
113
  replacer: if escape then escReplacer else (name,value) -> value
111
114
  })
112
- return "---\n" + tabify(str, 1)
115
+ return "---\n" + str
113
116
 
114
117
  # ---------------------------------------------------------------------------
115
118
 
@@ -123,7 +126,7 @@ export orderedStringify = (obj, escape=false) ->
123
126
  replacer: if escape then escReplacer else (name,value) -> value
124
127
  })
125
128
 
126
- return "---\n" + tabify(str, 1)
129
+ return "---\n" + str
127
130
 
128
131
  # ---------------------------------------------------------------------------
129
132
 
@@ -131,14 +134,12 @@ maxOneLine = 32
131
134
 
132
135
  # ---------------------------------------------------------------------------
133
136
 
134
- export log = (str, hOptions={}) ->
135
- # --- valid options:
136
- # prefix
137
+ export log = (str, prefix='') ->
137
138
 
139
+ assert isString(prefix), "not a string: #{OL(prefix)}"
138
140
  assert isString(str), "log(): not a string: #{OL(str)}"
139
141
  assert isFunction(putstr), "putstr not properly set"
140
- assert isHash(hOptions), "log(): arg 2 not a hash: #{OL(hOptions)}"
141
- prefix = fixForTerminal(hOptions.prefix)
142
+ prefix = fixForTerminal(prefix)
142
143
 
143
144
  if doDebugLog
144
145
  LOG "CALL log(#{OL(str)}), prefix = #{OL(prefix)}"
@@ -148,42 +149,51 @@ export log = (str, hOptions={}) ->
148
149
 
149
150
  # ---------------------------------------------------------------------------
150
151
 
151
- export logItem = (label, item, hOptions={}) ->
152
- # --- valid options:
153
- # prefix
152
+ export logBareItem = (item, pre='') ->
154
153
 
154
+ logItem undef, item, pre
155
+ return
156
+
157
+ # ---------------------------------------------------------------------------
158
+
159
+ export logItem = (label, item, pre='', itemPre=undef) ->
160
+
161
+ assert isString(pre), "not a string: #{OL(pre)}"
155
162
  assert isFunction(putstr), "putstr not properly set"
156
163
  assert !label || isString(label), "label a non-string"
157
- assert isHash(hOptions), "arg 3 not a hash"
164
+ if (itemPre == undef)
165
+ itemPre = pre
158
166
 
159
- label = fixForTerminal(label)
160
- prefix = fixForTerminal(hOptions.prefix)
161
- assert prefix.indexOf("\t") == -1, "prefix has TAB"
167
+ assert pre.indexOf("\t") == -1, "pre has TAB"
168
+ assert itemPre.indexOf("\t") == -1, "itemPre has TAB"
162
169
 
163
170
  if doDebugLog
164
171
  LOG "CALL logItem(#{OL(label)}, #{OL(item)})"
165
- LOG "prefix = #{OL(prefix)}"
172
+ LOG "pre = #{OL(pre)}"
173
+ LOG "itemPre = #{OL(itemPre)}"
166
174
 
167
175
  labelStr = if label then "#{label} = " else ""
168
176
 
169
177
  if (item == undef)
170
- putstr "#{prefix}#{labelStr}undef"
178
+ putstr "#{pre}#{labelStr}undef"
171
179
  else if (item == null)
172
- putstr "#{prefix}#{labelStr}null"
180
+ putstr "#{pre}#{labelStr}null"
181
+ else if isNumber(item)
182
+ putstr "#{pre}#{labelStr}#{item}"
173
183
  else if isString(item)
174
184
  if (item.length <= maxOneLine)
175
- putstr "#{prefix}#{labelStr}'#{escapeStr(item)}'"
185
+ putstr "#{pre}#{labelStr}'#{escapeStr(item)}'"
176
186
  else
177
187
  if label
178
- putstr "#{prefix}#{label}:"
179
- putBlock item, prefix + fourSpaces
180
- else if isNumber(item)
181
- putstr "#{prefix}#{labelStr}#{item}"
188
+ putstr "#{pre}#{label}:"
189
+ putBlock item, itemPre
182
190
  else
183
191
  if label
184
- putstr "#{prefix}#{label}:"
185
- for str in blockToArray(stringify(item, true)) # escape special chars
186
- putstr "#{prefix + fourSpaces}#{fixForTerminal(str)}"
192
+ putstr "#{pre}#{label}:"
193
+
194
+ # --- escape special chars
195
+ for str in blockToArray(stringify(item, true))
196
+ putstr "#{itemPre}#{str}"
187
197
 
188
198
  return true
189
199