@jdeighan/coffee-utils 7.0.59 → 7.0.62

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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(label) {
74
- LOG(callStack.dump('', label));
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);
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);
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,48 @@ 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;
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)}`);
195
137
  switch (type) {
196
138
  case 'enter':
197
- log(label, {prefix});
139
+ log(label, prefix(level));
140
+ if (label.match(/^\s*call/)) {
141
+ pre = prefix(level + 1, 'noLastVbar');
142
+ itemPre = prefix(level + 2, 'noLast2Vbars');
143
+ } else {
144
+ pre = prefix(level + 1);
145
+ itemPre = prefix(level + 2, 'noLastVbar');
146
+ }
198
147
  for (i = j = 0, len = lObjects.length; j < len; i = ++j) {
199
148
  obj = lObjects[i];
200
- if (i > 0) {
201
- log(sep);
202
- }
203
- logItem(undef, obj, {itemPrefix});
149
+ logItem(`arg[${i}]`, obj, pre, itemPre);
204
150
  }
205
151
  break;
206
152
  case 'return':
207
- log(label, {
208
- prefix: addArrow(prefix)
209
- });
153
+ log(label, prefix(level, 'withArrow'));
154
+ pre = prefix(level, 'noLastVbar');
155
+ itemPre = prefix(level + 1, 'noLast2Vbars');
210
156
  for (i = k = 0, len1 = lObjects.length; k < len1; i = ++k) {
211
157
  obj = lObjects[i];
212
- if (i > 0) {
213
- log(sep);
214
- }
215
- logItem(undef, obj, {itemPrefix});
158
+ logItem(`ret[${i}]`, obj, pre, itemPre);
216
159
  }
217
160
  break;
218
161
  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});
162
+ pre = prefix(level);
163
+ itemPre = prefix(level + 1, 'noLastVbar');
164
+ if (lObjects.length === 0) {
165
+ log(label, pre);
166
+ } else if ((lObjects.length === 1) && shortEnough(label, lObjects[0])) {
167
+ logItem(label, lObjects[0], pre);
224
168
  } else {
225
169
  if (label.indexOf(':') !== label.length - 1) {
226
170
  label += ':';
227
171
  }
228
- log(label, {prefix});
172
+ log(label, pre);
229
173
  for (l = 0, len2 = lObjects.length; l < len2; l++) {
230
174
  obj = lObjects[l];
231
- logItem(undef, obj, {prefix});
175
+ logBareItem(obj, itemPre);
232
176
  }
233
177
  }
234
178
  }
@@ -236,59 +180,54 @@ export var doTheLogging = function(type, label, lObjects) {
236
180
 
237
181
  // ---------------------------------------------------------------------------
238
182
  export var stdShouldLog = function(label, type, funcName, stack) {
239
- var prevLogged, result;
183
+ var result;
240
184
  // --- if type is 'enter', then funcName won't be on the stack yet
241
185
  // 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
186
  assert(isString(label), `label ${OL(label)} not a string`);
247
187
  assert(isString(type), `type ${OL(type)} not a string`);
188
+ assert(stack instanceof CallStack, "not a call stack object");
248
189
  if ((type === 'enter') || (type === 'return')) {
249
190
  assert(isString(funcName), `func name ${OL(funcName)} not a string`);
250
191
  } else {
251
192
  assert(funcName === undef, `func name ${OL(funcName)} not undef`);
252
193
  }
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
- }
194
+ if (funcMatch(funcName || stack.curFunc())) {
195
+ return label;
196
+ }
197
+ if ((type === 'enter') && !isMyFunc(funcName)) {
198
+ // --- As a special case, if we enter a function where we will not
199
+ // be logging, but we were logging in the calling function,
200
+ // we'll log out the call itself
201
+ if (funcMatch(stack.curFunc())) {
202
+ result = label.replace('enter', 'call');
203
+ return result;
204
+ }
277
205
  }
278
- debugDebug("return undef from stdShouldLog()");
279
206
  return undef;
280
207
  };
281
208
 
209
+ // ---------------------------------------------------------------------------
210
+ export var isMyFunc = function(funcName) {
211
+ return indexOf.call(words('debug debug2 doTheLogging stdShouldLog setDebugging getFuncList funcMatch getType dumpCallStack'), funcName) >= 0;
212
+ };
213
+
214
+ // ---------------------------------------------------------------------------
215
+ export var trueShouldLog = function(label, type, funcName, stack) {
216
+ if (isMyFunc(funcName || stack.curFunc())) {
217
+ return undef;
218
+ } else {
219
+ return label;
220
+ }
221
+ };
222
+
282
223
  // ---------------------------------------------------------------------------
283
224
  export var setDebugging = function(option) {
284
225
  callStack.reset();
285
226
  if (isBoolean(option)) {
286
227
  if (option) {
287
- shouldLog = function(label, type, funcName, stack) {
288
- return label;
289
- };
228
+ shouldLog = trueShouldLog;
290
229
  } else {
291
- shouldLog = function(label, type, funcName, stack) {
230
+ shouldLog = function() {
292
231
  return undef;
293
232
  };
294
233
  }
@@ -334,40 +273,31 @@ export var getFuncList = function(str) {
334
273
 
335
274
  // ---------------------------------------------------------------------------
336
275
  // --- export only to allow unit tests
337
- export var funcMatch = function() {
338
- var curFunc, h, j, len, name, object, plus;
276
+ export var funcMatch = function(funcName) {
277
+ var h, j, len, name, object, plus;
339
278
  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
279
  for (j = 0, len = lFuncList.length; j < len; j++) {
345
280
  h = lFuncList[j];
346
281
  ({name, object, plus} = h);
347
- if (name === curFunc) {
348
- debugDebug("return from funcMatch() - curFunc in lFuncList");
282
+ if (name === funcName) {
349
283
  return true;
350
284
  }
351
285
  if (plus && callStack.isActive(name)) {
352
- debugDebug(`return from funcMatch() - func ${OL(name)} is active`);
353
286
  return true;
354
287
  }
355
288
  }
356
- debugDebug("return from funcMatch() - no match");
357
289
  return false;
358
290
  };
359
291
 
360
292
  // ---------------------------------------------------------------------------
361
- // --- type is one of: 'enter', 'return', 'string', 'object'
362
- export var getType = function(str, nObjects) {
293
+ // --- type is one of: 'enter', 'return', 'string'
294
+ export var getType = function(str, lObjects) {
363
295
  var lMatches;
364
296
  if (lMatches = str.match(/^\s*enter\s+([A-Za-z_][A-Za-z0-9_\.]*)/)) {
365
297
  // --- We are entering function curFunc
366
298
  return ['enter', lMatches[1]];
367
299
  } else if (lMatches = str.match(/^\s*return.+from\s+([A-Za-z_][A-Za-z0-9_\.]*)/)) {
368
300
  return ['return', lMatches[1]];
369
- } else if (nObjects > 0) {
370
- return ['objects', undef];
371
301
  } else {
372
302
  return ['string', undef];
373
303
  }
@@ -377,3 +307,23 @@ export var getType = function(str, nObjects) {
377
307
  reMethod = /^([A-Za-z_][A-Za-z0-9_]*)\.([A-Za-z_][A-Za-z0-9_]*)$/;
378
308
 
379
309
  // ---------------------------------------------------------------------------
310
+ export var dumpDebugGlobals = function() {
311
+ var funcName, j, len;
312
+ LOG('='.repeat(40));
313
+ LOG(callStack.dump());
314
+ if (shouldLog === stdShouldLog) {
315
+ LOG("using stdShouldLog");
316
+ } else if (shouldLog === trueShouldLog) {
317
+ LOG("using trueShouldLog");
318
+ } else {
319
+ LOG("using custom shouldLog");
320
+ }
321
+ LOG("lFuncList:");
322
+ for (j = 0, len = lFuncList.length; j < len; j++) {
323
+ funcName = lFuncList[j];
324
+ LOG(` ${OL(funcName)}`);
325
+ }
326
+ return LOG('='.repeat(40));
327
+ };
328
+
329
+ // ---------------------------------------------------------------------------
@@ -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