@jdeighan/coffee-utils 7.0.58 → 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 callStack, doDebugDebug, lFunctions, logif, reMethod,
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,173 +39,136 @@ 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
- callStack = new CallStack();
55
-
56
56
  // --- set in resetDebugging() and setDebugging()
57
+ export var callStack = new CallStack();
58
+
57
59
  export var shouldLog = function() {
58
60
  return undef;
59
61
  };
60
62
 
61
- export var lFuncList = [];
63
+ lFuncList = []; // names of functions being debugged
62
64
 
63
- // --- internal debugging
64
- doDebugDebug = false;
65
-
66
- lFunctions = undef; // --- only used when doDebugDebug is true
65
+ strFuncList = undef; // original string
67
66
 
68
67
 
69
68
  // ---------------------------------------------------------------------------
70
- export var setDebugDebugging = function(value = true) {
71
- // --- value can be a boolean or string of words
72
- if (isBoolean(value)) {
73
- doDebugDebug = value;
74
- } else if (isString(value)) {
75
- doDebugDebug = true;
76
- lFunctions = words(value);
77
- } else {
78
- croak(`Bad value: ${OL(value)}`);
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);
74
+ switch (type) {
75
+ case 'enter':
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()");
83
+ break;
84
+ case 'return':
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);
92
+ break;
93
+ case 'string':
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()");
79
100
  }
101
+ return true; // allow use in boolean expressions
80
102
  };
81
103
 
82
- // ---------------------------------------------------------------------------
83
- logif = function(label, ...lObjects) {
84
- var funcName, itemPrefix, level, nObjects, prefix, sep, type;
85
- if (!doDebugDebug) {
86
- return;
87
- }
88
- assert(isString(label), `1st arg ${OL(label)} should be a string`);
89
- nObjects = lObjects.length;
90
- [type, funcName] = getType(label, nObjects);
91
- level = callStack.getLevel();
92
- prefix = getPrefix(level);
93
- itemPrefix = removeLastVbar(prefix);
94
- sep = dashes(itemPrefix, 40);
95
- if (type === 'enter') {
96
- if (defined(lFunctions) && (indexOf.call(lFunctions, funcName) < 0)) {
97
- return;
98
- }
99
- callStack.enter(funcName);
100
- } else if (type === 'return') {
101
- if (defined(lFunctions) && (indexOf.call(lFunctions, funcName) < 0)) {
102
- return;
103
- }
104
- }
105
- doTheLogging(type, label, lObjects);
106
- if (type === 'return') {
107
- callStack.returnFrom(funcName);
108
- }
109
- };
110
104
 
111
105
  // ---------------------------------------------------------------------------
112
- export var debug = function(label, ...lObjects) {
113
- var doLog, funcName, nObjects, type;
114
- logif(`enter debug(${OL(label)})`, ...lObjects);
115
- assert(isString(label), `1st arg ${OL(label)} should be a string`);
116
- // --- We want to allow objects to be undef. Therefore, we need to
117
- // distinguish between 1 arg sent vs. 2 or more args sent
118
- nObjects = lObjects.length;
119
- // --- funcName is only set for types 'enter' and 'return'
120
- [type, funcName] = getType(label, nObjects);
121
- logif(`type = ${OL(type)}`);
122
- logif(`funcName = ${OL(funcName)}`);
123
- // --- function shouldLog() returns the (possibly modified) label
124
- // 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);
125
110
  switch (type) {
126
111
  case 'enter':
127
- callStack.enter(funcName);
128
- label = shouldLog(label, type, funcName, callStack);
112
+ if (defined(label)) {
113
+ doTheLogging('enter', label, lObjects);
114
+ }
115
+ callStack.enter(funcName, lObjects, defined(label));
129
116
  break;
130
117
  case 'return':
131
- label = shouldLog(label, type, funcName, callStack);
118
+ if (defined(label)) {
119
+ doTheLogging('return', label, lObjects);
120
+ }
121
+ callStack.returnFrom(funcName);
132
122
  break;
133
123
  case 'string':
134
- label = shouldLog(label, type, undef, callStack);
135
- assert(nObjects === 0, `Objects not allowed for ${OL(type)}`);
136
- break;
137
- case 'objects':
138
- label = shouldLog(label, type, undef, callStack);
139
- assert(nObjects > 0, `Objects required for ${OL(type)}`);
140
- }
141
- assert((label === undef) || isString(label), `label not a string: ${OL(label)}`);
142
- doLog = defined(label);
143
- logif(`doLog = ${OL(doLog)}`);
144
- logif(`${nObjects} objects`);
145
- if (doLog) {
146
- doTheLogging(type, label, lObjects);
147
- }
148
- if ((type === 'enter') && doLog && (label.indexOf('call') === -1)) {
149
- callStack.logCurFunc();
150
- } else if (type === 'return') {
151
- callStack.returnFrom(funcName);
124
+ if (defined(label)) {
125
+ doTheLogging('string', label, lObjects);
126
+ }
152
127
  }
153
- logif("return from debug()");
154
128
  return true; // allow use in boolean expressions
155
129
  };
156
130
 
157
131
 
158
132
  // ---------------------------------------------------------------------------
159
133
  export var doTheLogging = function(type, label, lObjects) {
160
- 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)}`);
161
136
  level = callStack.getLevel();
162
- prefix = getPrefix(level);
163
- itemPrefix = removeLastVbar(prefix);
164
- sep = dashes(itemPrefix, 40);
165
- assert(isString(sep), "sep is not a string");
166
- logif("callStack", callStack);
167
- logif(`level = ${OL(level)}`);
168
- logif(`prefix = ${OL(prefix)}`);
169
- logif(`itemPrefix = ${OL(itemPrefix)}`);
170
- logif(`sep = ${OL(sep)}`);
137
+ sep = '-'.repeat(40);
171
138
  switch (type) {
172
139
  case 'enter':
173
- log(label, {prefix});
140
+ log(label, prefix(level));
141
+ pre = prefix(level + 1);
142
+ itemPre = prefix(level + 2, 'noLastVbar');
174
143
  for (i = j = 0, len = lObjects.length; j < len; i = ++j) {
175
144
  obj = lObjects[i];
176
- if (i > 0) {
177
- log(sep);
178
- }
179
- logItem(undef, obj, {itemPrefix});
145
+ logItem(`arg[${i}]`, obj, pre, itemPre);
180
146
  }
181
147
  break;
182
148
  case 'return':
183
- log(label, {
184
- prefix: addArrow(prefix)
185
- });
149
+ log(label, prefix(level, 'withArrow'));
150
+ pre = prefix(level, 'noLastVbar');
151
+ itemPre = prefix(level + 1, 'noLast2Vbars');
186
152
  for (i = k = 0, len1 = lObjects.length; k < len1; i = ++k) {
187
153
  obj = lObjects[i];
188
- if (i > 0) {
189
- log(sep);
190
- }
191
- logItem(undef, obj, {itemPrefix});
154
+ logItem(`ret[${i}]`, obj, pre, itemPre);
192
155
  }
193
156
  break;
194
157
  case 'string':
195
- log(label, {prefix});
196
- break;
197
- case 'objects':
198
- if ((lObjects.length === 1) && shortEnough(label, lObjects[0])) {
199
- 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);
200
164
  } else {
201
165
  if (label.indexOf(':') !== label.length - 1) {
202
166
  label += ':';
203
167
  }
204
- log(label, {prefix});
168
+ log(label, pre);
205
169
  for (l = 0, len2 = lObjects.length; l < len2; l++) {
206
170
  obj = lObjects[l];
207
- logItem(undef, obj, {prefix});
171
+ logBareItem(obj, itemPre);
208
172
  }
209
173
  }
210
174
  }
@@ -212,56 +176,54 @@ export var doTheLogging = function(type, label, lObjects) {
212
176
 
213
177
  // ---------------------------------------------------------------------------
214
178
  export var stdShouldLog = function(label, type, funcName, stack) {
215
- var prevLogged;
179
+ var result;
216
180
  // --- if type is 'enter', then funcName won't be on the stack yet
217
181
  // returns the (possibly modified) label to log
218
-
219
- // --- If we're logging now,
220
- // but we won't be logging when funcName is activated
221
- // then change 'enter' to 'call'
222
182
  assert(isString(label), `label ${OL(label)} not a string`);
223
183
  assert(isString(type), `type ${OL(type)} not a string`);
184
+ assert(stack instanceof CallStack, "not a call stack object");
224
185
  if ((type === 'enter') || (type === 'return')) {
225
186
  assert(isString(funcName), `func name ${OL(funcName)} not a string`);
226
187
  } else {
227
188
  assert(funcName === undef, `func name ${OL(funcName)} not undef`);
228
189
  }
229
- assert(stack instanceof CallStack, "not a call stack object");
230
- logif(`stdShouldLog(${OL(label)}, ${OL(type)}, ${OL(funcName)}, stack)`);
231
- logif("stack", stack);
232
- logif("lFuncList", lFuncList);
233
- switch (type) {
234
- case 'enter':
235
- if (funcMatch(stack, lFuncList)) {
236
- return label;
237
- } else {
238
- // --- As a special case, if we enter a function where we will not
239
- // be logging, but we were logging in the calling function,
240
- // we'll log out the call itself
241
- prevLogged = stack.isLoggingPrev();
242
- if (prevLogged) {
243
- return label.replace('enter', 'call');
244
- }
245
- }
246
- break;
247
- default:
248
- if (funcMatch(stack, lFuncList)) {
249
- return label;
250
- }
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
+ }
251
201
  }
252
202
  return undef;
253
203
  };
254
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
+
255
219
  // ---------------------------------------------------------------------------
256
220
  export var setDebugging = function(option) {
257
221
  callStack.reset();
258
222
  if (isBoolean(option)) {
259
223
  if (option) {
260
- shouldLog = function(label, type, funcName, stack) {
261
- return label;
262
- };
224
+ shouldLog = trueShouldLog;
263
225
  } else {
264
- shouldLog = function(label, type, funcName, stack) {
226
+ shouldLog = function() {
265
227
  return undef;
266
228
  };
267
229
  }
@@ -279,6 +241,7 @@ export var setDebugging = function(option) {
279
241
  // --- export only to allow unit tests
280
242
  export var getFuncList = function(str) {
281
243
  var _, ident1, ident2, j, lMatches, len, plus, ref, word;
244
+ strFuncList = str; // store original string for debugging
282
245
  lFuncList = [];
283
246
  ref = words(str);
284
247
  for (j = 0, len = ref.length; j < len; j++) {
@@ -306,40 +269,31 @@ export var getFuncList = function(str) {
306
269
 
307
270
  // ---------------------------------------------------------------------------
308
271
  // --- export only to allow unit tests
309
- export var funcMatch = function(stack, lFuncList) {
310
- var curFunc, h, j, len, name, object, plus;
272
+ export var funcMatch = function(funcName) {
273
+ var h, j, len, name, object, plus;
311
274
  assert(isArray(lFuncList), `not an array ${OL(lFuncList)}`);
312
- curFunc = stack.curFunc();
313
- logif(`funcMatch(): curFunc = ${OL(curFunc)}`);
314
- logif(stack.dump(' '));
315
- logif('lFuncList', lFuncList);
316
275
  for (j = 0, len = lFuncList.length; j < len; j++) {
317
276
  h = lFuncList[j];
318
277
  ({name, object, plus} = h);
319
- if (name === curFunc) {
320
- logif(" curFunc in lFuncList - match successful");
278
+ if (name === funcName) {
321
279
  return true;
322
280
  }
323
- if (plus && stack.isActive(name)) {
324
- logif(` func ${OL(name)} is active - match successful`);
281
+ if (plus && callStack.isActive(name)) {
325
282
  return true;
326
283
  }
327
284
  }
328
- logif(" - no match");
329
285
  return false;
330
286
  };
331
287
 
332
288
  // ---------------------------------------------------------------------------
333
- // --- type is one of: 'enter', 'return', 'string', 'object'
334
- export var getType = function(str, nObjects) {
289
+ // --- type is one of: 'enter', 'return', 'string'
290
+ export var getType = function(str, lObjects) {
335
291
  var lMatches;
336
292
  if (lMatches = str.match(/^\s*enter\s+([A-Za-z_][A-Za-z0-9_\.]*)/)) {
337
293
  // --- We are entering function curFunc
338
294
  return ['enter', lMatches[1]];
339
295
  } else if (lMatches = str.match(/^\s*return.+from\s+([A-Za-z_][A-Za-z0-9_\.]*)/)) {
340
296
  return ['return', lMatches[1]];
341
- } else if (nObjects > 0) {
342
- return ['objects', undef];
343
297
  } else {
344
298
  return ['string', undef];
345
299
  }
@@ -349,3 +303,23 @@ export var getType = function(str, nObjects) {
349
303
  reMethod = /^([A-Za-z_][A-Za-z0-9_]*)\.([A-Za-z_][A-Za-z0-9_]*)$/;
350
304
 
351
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