@jdeighan/coffee-utils 7.0.49 → 7.0.52

Sign up to get free protection for your applications and to get access to all the features.
package/src/log_utils.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // Generated by CoffeeScript 2.7.0
2
2
  // log_utils.coffee
3
- var escReplacer, fixStr, loaded, maxOneLine, putBlock, putstr;
3
+ var doDebugLog, escReplacer, fixForTerminal, fourSpaces, loaded, maxOneLine, putBlock, putstr;
4
4
 
5
5
  import yaml from 'js-yaml';
6
6
 
@@ -15,7 +15,8 @@ import {
15
15
  escapeStr,
16
16
  sep_eq,
17
17
  sep_dash,
18
- pass
18
+ pass,
19
+ OL
19
20
  } from '@jdeighan/coffee-utils';
20
21
 
21
22
  import {
@@ -25,14 +26,27 @@ import {
25
26
  import {
26
27
  tabify,
27
28
  untabify,
28
- indentation
29
+ indentation,
30
+ indented
29
31
  } from '@jdeighan/coffee-utils/indent';
30
32
 
31
33
  // --- This logger only ever gets passed a single string argument
32
34
  putstr = undef;
33
35
 
36
+ doDebugLog = false;
37
+
34
38
  export var stringify = undef;
35
39
 
40
+ fourSpaces = ' ';
41
+
42
+ // ---------------------------------------------------------------------------
43
+ export var debugLog = function(flag = true) {
44
+ doDebugLog = flag;
45
+ if (doDebugLog) {
46
+ LOG(`doDebugLog = ${flag}`);
47
+ }
48
+ };
49
+
36
50
  // ---------------------------------------------------------------------------
37
51
  // This is useful for debugging
38
52
  export var LOG = function(...lArgs) {
@@ -41,9 +55,9 @@ export var LOG = function(...lArgs) {
41
55
  if (lArgs.length > 1) {
42
56
  // --- There's both a label and an item
43
57
  if (item === undef) {
44
- console.log(`${label}: UNDEFINED`);
58
+ console.log(`${label} = undef`);
45
59
  } else if (item === null) {
46
- console.log(`${label}: NULL`);
60
+ console.log(`${label} = null`);
47
61
  } else {
48
62
  console.log(sep_dash);
49
63
  console.log(`${label}:`);
@@ -134,19 +148,6 @@ export var orderedStringify = function(obj, escape = false) {
134
148
  // ---------------------------------------------------------------------------
135
149
  maxOneLine = 32;
136
150
 
137
- // ---------------------------------------------------------------------------
138
- fixStr = function(str) {
139
- if (!str) {
140
- return '';
141
- }
142
- // --- If putstr is console.log, we'll convert TAB char to 3 spaces
143
- if (putstr === console.log) {
144
- return untabify(str);
145
- } else {
146
- return str;
147
- }
148
- };
149
-
150
151
  // ---------------------------------------------------------------------------
151
152
  export var log = function(str, hOptions = {}) {
152
153
  var prefix;
@@ -155,7 +156,10 @@ export var log = function(str, hOptions = {}) {
155
156
  assert(isFunction(putstr), "putstr not properly set");
156
157
  assert(isString(str), "log(): not a string");
157
158
  assert(isHash(hOptions), "log(): arg 2 not a hash");
158
- prefix = fixStr(hOptions.prefix);
159
+ prefix = fixForTerminal(hOptions.prefix);
160
+ if (doDebugLog) {
161
+ LOG(`CALL log(${OL(str)}), prefix = ${OL(prefix)}`);
162
+ }
159
163
  putstr(`${prefix}${str}`);
160
164
  return true; // to allow use in boolean expressions
161
165
  };
@@ -165,13 +169,17 @@ export var log = function(str, hOptions = {}) {
165
169
  export var logItem = function(label, item, hOptions = {}) {
166
170
  var i, labelStr, len, prefix, ref, str;
167
171
  // --- valid options:
168
- // prefix - not used
169
- // itemPrefix - always used
172
+ // prefix
170
173
  assert(isFunction(putstr), "putstr not properly set");
171
174
  assert(!label || isString(label), "label a non-string");
172
175
  assert(isHash(hOptions), "arg 3 not a hash");
173
- label = fixStr(label);
174
- prefix = fixStr(hOptions.itemPrefix || hOptions.prefix);
176
+ label = fixForTerminal(label);
177
+ prefix = fixForTerminal(hOptions.prefix);
178
+ assert(prefix.indexOf("\t") === -1, "prefix has TAB");
179
+ if (doDebugLog) {
180
+ LOG(`CALL logItem(${OL(label)}, ${OL(item)})`);
181
+ LOG(`prefix = ${OL(prefix)}`);
182
+ }
175
183
  labelStr = label ? `${label} = ` : "";
176
184
  if (item === undef) {
177
185
  putstr(`${prefix}${labelStr}undef`);
@@ -184,12 +192,11 @@ export var logItem = function(label, item, hOptions = {}) {
184
192
  if (label) {
185
193
  putstr(`${prefix}${label}:`);
186
194
  }
187
- putBlock(item, prefix);
195
+ putBlock(item, prefix + fourSpaces);
188
196
  }
189
197
  } else if (isNumber(item)) {
190
198
  putstr(`${prefix}${labelStr}${item}`);
191
199
  } else {
192
- putstr(`${prefix}${sep_dash}`);
193
200
  if (label) {
194
201
  putstr(`${prefix}${label}:`);
195
202
  }
@@ -197,13 +204,27 @@ export var logItem = function(label, item, hOptions = {}) {
197
204
  // escape special chars
198
205
  for (i = 0, len = ref.length; i < len; i++) {
199
206
  str = ref[i];
200
- putstr(`${prefix}${indentation(1)}${fixStr(str)}`);
207
+ putstr(`${prefix + fourSpaces}${fixForTerminal(str)}`);
201
208
  }
202
- putstr(`${prefix}${sep_dash}`);
203
209
  }
204
210
  return true;
205
211
  };
206
212
 
213
+ // ---------------------------------------------------------------------------
214
+ export var shortEnough = function(label, value) {
215
+ return value === undef;
216
+ };
217
+
218
+ // ---------------------------------------------------------------------------
219
+ // --- needed because Windows Terminal handles TAB chars badly
220
+ fixForTerminal = function(str) {
221
+ if (!str) {
222
+ return '';
223
+ }
224
+ // --- convert TAB char to 4 spaces
225
+ return str.replace(/\t/g, fourSpaces);
226
+ };
227
+
207
228
  // ---------------------------------------------------------------------------
208
229
  putBlock = function(item, prefix = '') {
209
230
  var i, len, line, ref;