analogger 1.8.3 → 1.8.4
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.
- package/CHANGELOG.md +3 -1
- package/package.json +1 -1
- package/src/cjs/ana-logger.cjs +807 -0
- package/src/cjs/bump.js +1 -0
- package/src/cjs/constants.cjs +31 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
## [1.8.
|
|
1
|
+
## [1.8.4](https://github.com/thimpat/analogger/compare/v1.8.3...v1.8.4) (2022-02-28)
|
|
2
2
|
|
|
3
|
+
## [1.8.3](https://github.com/thimpat/analogger/compare/v1.8.2...v1.8.3) (2022-02-28)
|
|
4
|
+
|
|
3
5
|
## [1.8.2](https://github.com/thimpat/analogger/compare/v1.8.1...v1.8.2) (2022-02-28)
|
|
4
6
|
|
|
5
7
|
## [1.8.1](https://github.com/thimpat/analogger/compare/v1.8.0...v1.8.1) (2022-02-28)
|
package/package.json
CHANGED
|
@@ -0,0 +1,807 @@
|
|
|
1
|
+
/** to-esm-browser: remove **/
|
|
2
|
+
const path = require("path");
|
|
3
|
+
const fs = require("fs");
|
|
4
|
+
const os = require("os");
|
|
5
|
+
/** to-esm-browser: end-remove **/
|
|
6
|
+
|
|
7
|
+
const toAnsi = require("to-ansi");
|
|
8
|
+
const rgbHex = require("rgb-hex-cjs");
|
|
9
|
+
const {COLOR_TABLE, SYSTEM} = require("./constants.cjs");
|
|
10
|
+
|
|
11
|
+
const EOL =`
|
|
12
|
+
`;
|
|
13
|
+
|
|
14
|
+
class AnaLogger
|
|
15
|
+
{
|
|
16
|
+
system = "";
|
|
17
|
+
|
|
18
|
+
logIndex = 0;
|
|
19
|
+
logCounter = 0;
|
|
20
|
+
contexts = [];
|
|
21
|
+
targets = {};
|
|
22
|
+
|
|
23
|
+
activeTarget = null;
|
|
24
|
+
|
|
25
|
+
indexColor = 0;
|
|
26
|
+
|
|
27
|
+
format = "";
|
|
28
|
+
|
|
29
|
+
keepLog = false;
|
|
30
|
+
logHistory = [];
|
|
31
|
+
|
|
32
|
+
$containers = null;
|
|
33
|
+
|
|
34
|
+
options = {
|
|
35
|
+
hideHookMessage: false
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
static ALIGN = {
|
|
39
|
+
LEFT : "LEFT",
|
|
40
|
+
RIGHT: "RIGHT"
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
static ENVIRONMENT_TYPE = {
|
|
44
|
+
BROWSER: "BROWSER",
|
|
45
|
+
NODE : "NODE",
|
|
46
|
+
OTHER : "OTHER"
|
|
47
|
+
};
|
|
48
|
+
originalFormatFunction;
|
|
49
|
+
|
|
50
|
+
constructor()
|
|
51
|
+
{
|
|
52
|
+
if (AnaLogger.Instance)
|
|
53
|
+
{
|
|
54
|
+
return AnaLogger.Instance;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
AnaLogger.Instance = this;
|
|
58
|
+
|
|
59
|
+
this.system = (typeof process === "object") ? SYSTEM.NODE : SYSTEM.BROWSER;
|
|
60
|
+
this.format = this.onBuildLog.bind(this);
|
|
61
|
+
this.originalFormatFunction = this.format;
|
|
62
|
+
|
|
63
|
+
this.errorTargetHandler = this.onError.bind(this);
|
|
64
|
+
this.errorUserTargetHandler = this.onErrorForUserTarget.bind(this);
|
|
65
|
+
|
|
66
|
+
this.setOptions(this.options);
|
|
67
|
+
|
|
68
|
+
this.realConsoleLog = console.log;
|
|
69
|
+
this.realConsoleInfo = console.info;
|
|
70
|
+
this.realConsoleWarn = console.warn;
|
|
71
|
+
this.realConsoleError = console.error;
|
|
72
|
+
|
|
73
|
+
this.ALIGN = AnaLogger.ALIGN;
|
|
74
|
+
this.ENVIRONMENT_TYPE = AnaLogger.ENVIRONMENT_TYPE;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
keepLogHistory()
|
|
78
|
+
{
|
|
79
|
+
this.keepLog = true;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
releaseLogHistory()
|
|
83
|
+
{
|
|
84
|
+
this.keepLog = false;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
resetLogHistory()
|
|
88
|
+
{
|
|
89
|
+
this.logHistory = [];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
getLogHistory(join = true, symbol = EOL)
|
|
93
|
+
{
|
|
94
|
+
const history = JSON.parse(JSON.stringify(this.logHistory.slice(0)));
|
|
95
|
+
if (!join)
|
|
96
|
+
{
|
|
97
|
+
return history;
|
|
98
|
+
}
|
|
99
|
+
return history.join(symbol);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Tell whether we are in a Node environment
|
|
104
|
+
* @returns {boolean}
|
|
105
|
+
*/
|
|
106
|
+
isNode()
|
|
107
|
+
{
|
|
108
|
+
return this.system === SYSTEM.NODE;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Tell whether the logger runs from a browser
|
|
113
|
+
* @returns {boolean}
|
|
114
|
+
*/
|
|
115
|
+
isBrowser()
|
|
116
|
+
{
|
|
117
|
+
return !this.isNode();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
resetLogger()
|
|
121
|
+
{
|
|
122
|
+
this.options = {
|
|
123
|
+
contextLenMax : 10,
|
|
124
|
+
idLenMax : 5,
|
|
125
|
+
lidLenMax : 5,
|
|
126
|
+
symbolLenMax : 2,
|
|
127
|
+
messageLenMax : undefined,
|
|
128
|
+
hideLog : undefined,
|
|
129
|
+
hideError : undefined,
|
|
130
|
+
hideHookMessage : undefined,
|
|
131
|
+
hidePassingTests : undefined,
|
|
132
|
+
logToDom : undefined,
|
|
133
|
+
logToFile : undefined,
|
|
134
|
+
oneConsolePerContext: undefined,
|
|
135
|
+
silent : undefined
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
resetOptions()
|
|
140
|
+
{
|
|
141
|
+
this.options.contextLenMax = 10;
|
|
142
|
+
this.options.idLenMax = 5;
|
|
143
|
+
this.options.lidLenMax = 5;
|
|
144
|
+
this.options.messageLenMax = undefined;
|
|
145
|
+
this.options.symbolLenMax = 60;
|
|
146
|
+
this.options.hideHookMessage = false;
|
|
147
|
+
this.options.hidePassingTests = false;
|
|
148
|
+
this.options.hideLog = false;
|
|
149
|
+
this.options.hideError = false;
|
|
150
|
+
this.options.oneConsolePerContext = true;
|
|
151
|
+
this.options.logToDom = undefined;
|
|
152
|
+
this.options.logToDomlogToFile = undefined;
|
|
153
|
+
this.options.silent = false;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
setOptions({
|
|
157
|
+
contextLenMax = 10,
|
|
158
|
+
idLenMax = 5,
|
|
159
|
+
lidLenMax = 5,
|
|
160
|
+
symbolLenMax = 2,
|
|
161
|
+
messageLenMax = undefined,
|
|
162
|
+
hideLog = undefined,
|
|
163
|
+
hideError = undefined,
|
|
164
|
+
hideHookMessage = undefined,
|
|
165
|
+
hidePassingTests = undefined,
|
|
166
|
+
logToDom = undefined,
|
|
167
|
+
logToFile = undefined,
|
|
168
|
+
oneConsolePerContext = undefined,
|
|
169
|
+
silent = undefined
|
|
170
|
+
} = null)
|
|
171
|
+
{
|
|
172
|
+
this.options.contextLenMax = contextLenMax;
|
|
173
|
+
this.options.idLenMax = idLenMax;
|
|
174
|
+
this.options.lidLenMax = lidLenMax;
|
|
175
|
+
this.options.messageLenMax = messageLenMax;
|
|
176
|
+
this.options.symbolLenMax = symbolLenMax;
|
|
177
|
+
|
|
178
|
+
if (hidePassingTests !== undefined)
|
|
179
|
+
{
|
|
180
|
+
this.options.hidePassingTests = !!hidePassingTests;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (hideHookMessage !== undefined)
|
|
184
|
+
{
|
|
185
|
+
this.options.hideHookMessage = !!hideHookMessage;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (hideLog !== undefined)
|
|
189
|
+
{
|
|
190
|
+
this.options.hideLog = !!hideLog;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (hideError !== undefined)
|
|
194
|
+
{
|
|
195
|
+
this.options.hideError = !!hideError;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
if (oneConsolePerContext !== undefined)
|
|
199
|
+
{
|
|
200
|
+
this.options.oneConsolePerContext = !!oneConsolePerContext;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (logToDom !== undefined)
|
|
204
|
+
{
|
|
205
|
+
this.options.logToDom = logToDom || "#analogger";
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (logToFile !== undefined)
|
|
209
|
+
{
|
|
210
|
+
if (!this.isBrowser())
|
|
211
|
+
{
|
|
212
|
+
this.options.logToFile = logToFile || "./analogger.log";
|
|
213
|
+
|
|
214
|
+
/** to-esm-browser: remove **/
|
|
215
|
+
// these require won't get compiled by to-esm
|
|
216
|
+
this.options.logToFilePath = path.resolve(this.options.logToFile);
|
|
217
|
+
this.logFile = fs.createWriteStream(this.options.logToFilePath, {flags : "a"});
|
|
218
|
+
this.EOL = os.EOL;
|
|
219
|
+
/** to-esm-browser: end-remove **/
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** to-esm-browser: add
|
|
223
|
+
this.realConsoleLog("LogToFile is not supported in this environment. ")
|
|
224
|
+
* **/
|
|
225
|
+
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (silent !== undefined)
|
|
229
|
+
{
|
|
230
|
+
this.options.silent = !!silent;
|
|
231
|
+
this.options.hideLog = this.options.silent;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
getOptions()
|
|
237
|
+
{
|
|
238
|
+
return this.options;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
truncateMessage(input = "", {fit = 0, align = AnaLogger.ALIGN.LEFT} = {})
|
|
242
|
+
{
|
|
243
|
+
input = "" + input;
|
|
244
|
+
if (fit && input.length >= fit + 2)
|
|
245
|
+
{
|
|
246
|
+
input = input.substring(0, fit - 3) + "...";
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
input = align === AnaLogger.ALIGN.LEFT ? input.padEnd(fit, " ") : input.padStart(fit, " ");
|
|
250
|
+
return input;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Format inputs
|
|
255
|
+
* @see Override {@link setLogFormat}
|
|
256
|
+
* @param contextName
|
|
257
|
+
* @param id
|
|
258
|
+
* @param message
|
|
259
|
+
* @param lid
|
|
260
|
+
* @param symbol
|
|
261
|
+
* @returns {string}
|
|
262
|
+
*/
|
|
263
|
+
onBuildLog({contextName, message = "", lid = "", symbol = ""} = {})
|
|
264
|
+
{
|
|
265
|
+
// Time
|
|
266
|
+
const date = new Date();
|
|
267
|
+
let time = ("0" + date.getHours()).slice(-2) + ":" + ("0" + date.getMinutes()).slice(-2) + ":" + ("0" + date.getSeconds()).slice(-2);
|
|
268
|
+
|
|
269
|
+
// Display content in columns
|
|
270
|
+
time = this.truncateMessage(time, {fit: 7});
|
|
271
|
+
contextName = this.truncateMessage(contextName, {fit: this.options.contextLenMax, align: AnaLogger.ALIGN.RIGHT});
|
|
272
|
+
// id = this.truncateMessage(id, {fit: this.options.idLenMax})
|
|
273
|
+
lid = this.truncateMessage(lid, {fit: this.options.lidLenMax});
|
|
274
|
+
|
|
275
|
+
if (this.options.messageLenMax !== undefined)
|
|
276
|
+
{
|
|
277
|
+
message = this.truncateMessage(message, {fit: this.options.messageLenMax});
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
symbol = this.truncateMessage(symbol, {fit: this.options.symbolLenMax});
|
|
281
|
+
|
|
282
|
+
return `[${time}] ${contextName}: (${lid}) ${symbol} ${message}`;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
onErrorForUserTarget(context, ...args)
|
|
286
|
+
{
|
|
287
|
+
this.errorUserTargetHandler(context, ...args);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
onError(context, ...args)
|
|
291
|
+
{
|
|
292
|
+
if (context.target === this.targets.USER)
|
|
293
|
+
{
|
|
294
|
+
this.onErrorForUserTarget(context, ...args);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Forward input to real console log
|
|
300
|
+
* @param args
|
|
301
|
+
*/
|
|
302
|
+
onDisplayLog(...args)
|
|
303
|
+
{
|
|
304
|
+
this.log(...args);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Forward input to real console log
|
|
309
|
+
* @param args
|
|
310
|
+
*/
|
|
311
|
+
onDisplayError(...args)
|
|
312
|
+
{
|
|
313
|
+
this.error(...args);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Set log template
|
|
318
|
+
* @param format
|
|
319
|
+
*/
|
|
320
|
+
setLogFormat(format)
|
|
321
|
+
{
|
|
322
|
+
if (typeof format !== "function")
|
|
323
|
+
{
|
|
324
|
+
console.error("Invalid parameter for setFormat. It is expecting a function or method.");
|
|
325
|
+
return false;
|
|
326
|
+
}
|
|
327
|
+
this.format = format.bind(this);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
resetLogFormatter()
|
|
331
|
+
{
|
|
332
|
+
this.format = this.originalFormatFunction;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
setErrorHandler(handler)
|
|
336
|
+
{
|
|
337
|
+
this.errorTargetHandler = handler.bind(this);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
setErrorHandlerForUserTarget(handler)
|
|
341
|
+
{
|
|
342
|
+
this.errorUserTargetHandler = handler.bind(this);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// ------------------------------------------------
|
|
346
|
+
// Color
|
|
347
|
+
// ------------------------------------------------
|
|
348
|
+
|
|
349
|
+
// ------------------------------------------------
|
|
350
|
+
// Log Contexts
|
|
351
|
+
// ------------------------------------------------
|
|
352
|
+
isContextValid(context)
|
|
353
|
+
{
|
|
354
|
+
if (
|
|
355
|
+
!(typeof context === "object" &&
|
|
356
|
+
!Array.isArray(context) &&
|
|
357
|
+
context !== null)
|
|
358
|
+
)
|
|
359
|
+
{
|
|
360
|
+
return false;
|
|
361
|
+
}
|
|
362
|
+
return (context.hasOwnProperty("contextName") && context.hasOwnProperty("target"));
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
generateDefaultContext()
|
|
366
|
+
{
|
|
367
|
+
const defaultContext = {
|
|
368
|
+
name : "DEFAULT",
|
|
369
|
+
contextName: "DEFAULT",
|
|
370
|
+
target : "ALL",
|
|
371
|
+
symbol : "⚡"
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
defaultContext.id = this.logIndex++;
|
|
375
|
+
defaultContext.color = COLOR_TABLE[1];
|
|
376
|
+
return defaultContext;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
generateNewContext()
|
|
380
|
+
{
|
|
381
|
+
const newContext = this.generateDefaultContext();
|
|
382
|
+
newContext.color = COLOR_TABLE[(this.indexColor++) % (COLOR_TABLE.length - 3) + 2];
|
|
383
|
+
newContext.symbol = "";
|
|
384
|
+
return newContext;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
generateErrorContext()
|
|
388
|
+
{
|
|
389
|
+
const errorContext = this.generateDefaultContext();
|
|
390
|
+
errorContext.color = COLOR_TABLE[0];
|
|
391
|
+
errorContext.symbol = "v";
|
|
392
|
+
errorContext.error = true;
|
|
393
|
+
return errorContext;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
#allegeProperties(entry)
|
|
397
|
+
{
|
|
398
|
+
let converted = entry;
|
|
399
|
+
|
|
400
|
+
const defaultContext = this.generateNewContext();
|
|
401
|
+
|
|
402
|
+
converted = Object.assign({}, defaultContext, converted);
|
|
403
|
+
|
|
404
|
+
if (converted.color.toLowerCase().indexOf("rgb") > -1)
|
|
405
|
+
{
|
|
406
|
+
converted.color = "#" + rgbHex(converted.color);
|
|
407
|
+
}
|
|
408
|
+
else if (converted.color.indexOf("#") === -1)
|
|
409
|
+
{
|
|
410
|
+
const colorConvert = require("color-convert-cjs");
|
|
411
|
+
if (colorConvert)
|
|
412
|
+
{
|
|
413
|
+
converted.color = "#" + colorConvert.keyword.hex(converted.color);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
return converted;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Load the context names that should be available to the environment.
|
|
422
|
+
* They are defined by the user.
|
|
423
|
+
* @see Context definitions {@link ./example/cjs/contexts-def.cjs}
|
|
424
|
+
* @param contextTable
|
|
425
|
+
*/
|
|
426
|
+
setContexts(contextTable)
|
|
427
|
+
{
|
|
428
|
+
const arr = Object.keys(contextTable);
|
|
429
|
+
contextTable["DEFAULT"] = this.contexts["DEFAULT"] = this.generateDefaultContext();
|
|
430
|
+
contextTable["ERROR"] = this.contexts["ERROR"] = this.generateErrorContext();
|
|
431
|
+
arr.forEach((key) =>
|
|
432
|
+
{
|
|
433
|
+
const contextPassed = contextTable[key] || {};
|
|
434
|
+
contextPassed.contextName = key;
|
|
435
|
+
contextPassed.name = key;
|
|
436
|
+
this.contexts[key] = this.#allegeProperties(contextPassed);
|
|
437
|
+
contextTable[key] = this.contexts[key];
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
setTargets(targetTable = {})
|
|
442
|
+
{
|
|
443
|
+
this.targets = Object.assign({}, targetTable, {ALL: "ALL", USER: "USER"});
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
setActiveTarget(target)
|
|
447
|
+
{
|
|
448
|
+
this.activeTarget = target;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
isTargetAllowed(target)
|
|
452
|
+
{
|
|
453
|
+
if (!target || !this.activeTarget)
|
|
454
|
+
{
|
|
455
|
+
return true;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
if (target === this.targets.ALL)
|
|
459
|
+
{
|
|
460
|
+
return true;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
return this.activeTarget === target;
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
// ------------------------------------------------
|
|
468
|
+
// Logging methods
|
|
469
|
+
// ------------------------------------------------
|
|
470
|
+
setColumns($line, context, text)
|
|
471
|
+
{
|
|
472
|
+
let index = 0;
|
|
473
|
+
for (let columnName in context)
|
|
474
|
+
{
|
|
475
|
+
if ("name" === columnName)
|
|
476
|
+
{
|
|
477
|
+
continue;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
const colContent = context[columnName];
|
|
481
|
+
const $col = document.createElement("span");
|
|
482
|
+
$col.classList.add("analogger-col", `analogger-col-${columnName}`, `analogger-col-${index}`);
|
|
483
|
+
++index;
|
|
484
|
+
$col.textContent = colContent;
|
|
485
|
+
$line.append($col);
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
const $col = document.createElement("span");
|
|
489
|
+
$col.classList.add("analogger-col", "analogger-col-text", `analogger-col-${index}`);
|
|
490
|
+
$col.textContent = text;
|
|
491
|
+
$line.append($col);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
writeLogToDom(context, text)
|
|
495
|
+
{
|
|
496
|
+
this.$containers = this.$containers || document.querySelectorAll(this.options.logToDom);
|
|
497
|
+
|
|
498
|
+
for (let i = 0; i < this.$containers.length; ++i)
|
|
499
|
+
{
|
|
500
|
+
const $container = this.$containers[i];
|
|
501
|
+
|
|
502
|
+
let $view = $container.querySelector(".analogger-view");
|
|
503
|
+
if (!$view)
|
|
504
|
+
{
|
|
505
|
+
$view = document.createElement("div");
|
|
506
|
+
$view.classList.add("analogger-view");
|
|
507
|
+
$container.append($view);
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
const $line = document.createElement("div");
|
|
511
|
+
$line.classList.add("to-esm-line");
|
|
512
|
+
$line.style.color = context.color;
|
|
513
|
+
$line.setAttribute("data-log-counter", this.logCounter);
|
|
514
|
+
$line.setAttribute("data-log-index", this.logIndex);
|
|
515
|
+
|
|
516
|
+
this.setColumns($line ,context, text);
|
|
517
|
+
|
|
518
|
+
$view.append($line);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
writeLogToFile(text)
|
|
523
|
+
{
|
|
524
|
+
this.logFile.write(text + this.EOL);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* Display log following template
|
|
529
|
+
* @param context
|
|
530
|
+
*/
|
|
531
|
+
processOutput(context = {})
|
|
532
|
+
{
|
|
533
|
+
try
|
|
534
|
+
{
|
|
535
|
+
if (!this.isTargetAllowed(context.target))
|
|
536
|
+
{
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
let args = Array.prototype.slice.call(arguments);
|
|
541
|
+
args.shift();
|
|
542
|
+
|
|
543
|
+
const message = args.join(" | ");
|
|
544
|
+
|
|
545
|
+
let output = "";
|
|
546
|
+
const text = this.format({...context, message});
|
|
547
|
+
|
|
548
|
+
++this.logCounter;
|
|
549
|
+
|
|
550
|
+
if (this.isBrowser())
|
|
551
|
+
{
|
|
552
|
+
context.environnment = AnaLogger.ENVIRONMENT_TYPE.BROWSER;
|
|
553
|
+
if (this.options.logToDom)
|
|
554
|
+
{
|
|
555
|
+
this.writeLogToDom(context, text);
|
|
556
|
+
}
|
|
557
|
+
output = `%c${text}`;
|
|
558
|
+
}
|
|
559
|
+
else
|
|
560
|
+
{
|
|
561
|
+
context.environnment = AnaLogger.ENVIRONMENT_TYPE.NODE;
|
|
562
|
+
output = toAnsi.getTextFromHex(text, {fg: context.color});
|
|
563
|
+
|
|
564
|
+
if (this.options.logToFile)
|
|
565
|
+
{
|
|
566
|
+
this.writeLogToFile(text);
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
if (this.keepLog)
|
|
571
|
+
{
|
|
572
|
+
this.logHistory.push(output);
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
if (this.options.hideLog)
|
|
576
|
+
{
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
if (this.isBrowser())
|
|
581
|
+
{
|
|
582
|
+
this.realConsoleLog(output, `color: ${context.color}`);
|
|
583
|
+
}
|
|
584
|
+
else
|
|
585
|
+
{
|
|
586
|
+
this.realConsoleLog(output);
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
this.errorTargetHandler(context, args);
|
|
590
|
+
}
|
|
591
|
+
catch (e)
|
|
592
|
+
{
|
|
593
|
+
/* istanbul ignore next */
|
|
594
|
+
console.error("AnaLogger:", e.message);
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/**
|
|
599
|
+
* Check that a parameter (should be the first) uses the expected format.
|
|
600
|
+
* @param options
|
|
601
|
+
* @returns {boolean}
|
|
602
|
+
*/
|
|
603
|
+
isExtendedOptionsPassed(options)
|
|
604
|
+
{
|
|
605
|
+
if (typeof options !== "object")
|
|
606
|
+
{
|
|
607
|
+
return false;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
return options.hasOwnProperty("context") ||
|
|
611
|
+
options.hasOwnProperty("target") ||
|
|
612
|
+
options.hasOwnProperty("color") ||
|
|
613
|
+
options.hasOwnProperty("lid");
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
convertToContext(options, defaultContext)
|
|
617
|
+
{
|
|
618
|
+
defaultContext = defaultContext || this.generateDefaultContext();
|
|
619
|
+
options = options || defaultContext;
|
|
620
|
+
let context = options;
|
|
621
|
+
if (options.context && typeof options.context === "object")
|
|
622
|
+
{
|
|
623
|
+
const moreOptions = Object.assign({}, options);
|
|
624
|
+
delete moreOptions.context;
|
|
625
|
+
context = Object.assign({}, options.context, moreOptions);
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
context = Object.assign({}, defaultContext, context);
|
|
629
|
+
delete context.context;
|
|
630
|
+
|
|
631
|
+
return context;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* console.log with options set on the first parameter to dictate console log behaviours
|
|
636
|
+
* @param options
|
|
637
|
+
* @param args
|
|
638
|
+
*/
|
|
639
|
+
log(options, ...args)
|
|
640
|
+
{
|
|
641
|
+
if (!this.isExtendedOptionsPassed(options))
|
|
642
|
+
{
|
|
643
|
+
const defaultContext = this.generateDefaultContext();
|
|
644
|
+
this.processOutput.apply(this, [defaultContext, options, ...args]);
|
|
645
|
+
return;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
let context = this.convertToContext(options);
|
|
649
|
+
|
|
650
|
+
this.processOutput.apply(this, [context, ...args]);
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
error(options, ...args)
|
|
654
|
+
{
|
|
655
|
+
if (this.options.hideError)
|
|
656
|
+
{
|
|
657
|
+
return;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
if (!this.isExtendedOptionsPassed(options))
|
|
661
|
+
{
|
|
662
|
+
const defaultContext = this.generateErrorContext();
|
|
663
|
+
this.processOutput.apply(this, [defaultContext, options, ...args]);
|
|
664
|
+
return;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
const errorContext = this.generateErrorContext();
|
|
668
|
+
let context = this.convertToContext(options, errorContext);
|
|
669
|
+
|
|
670
|
+
let args0 = Array.prototype.slice.call(arguments, 1);
|
|
671
|
+
this.log(context, ...args0);
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
overrideError()
|
|
675
|
+
{
|
|
676
|
+
if (!this.options.hideHookMessage)
|
|
677
|
+
{
|
|
678
|
+
this.realConsoleLog("AnaLogger: Hook placed on console.error");
|
|
679
|
+
}
|
|
680
|
+
console.error = this.onDisplayError.bind(this);
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
overrideConsole({log = true, info = true, warn = true, error = false} = {})
|
|
684
|
+
{
|
|
685
|
+
if (!this.options.hideHookMessage)
|
|
686
|
+
{
|
|
687
|
+
this.realConsoleLog("AnaLogger: Hook placed on console.log");
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
if (log)
|
|
691
|
+
{
|
|
692
|
+
console.log = this.onDisplayLog.bind(this);
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
if (info)
|
|
696
|
+
{
|
|
697
|
+
console.info = this.onDisplayLog.bind(this);
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
if (warn)
|
|
701
|
+
{
|
|
702
|
+
console.warn = this.onDisplayLog.bind(this);
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
if (error)
|
|
706
|
+
{
|
|
707
|
+
this.overrideError();
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
removeOverrideError()
|
|
712
|
+
{
|
|
713
|
+
console.warn = this.realConsoleError;
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
removeOverride({log = true, info = true, warn = true, error = false} = {})
|
|
717
|
+
{
|
|
718
|
+
if (log)
|
|
719
|
+
{
|
|
720
|
+
console.log = this.realConsoleLog;
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
if (info)
|
|
724
|
+
{
|
|
725
|
+
console.info = this.realConsoleInfo;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
if (warn)
|
|
729
|
+
{
|
|
730
|
+
console.warn = this.realConsoleWarn;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
if (error)
|
|
734
|
+
{
|
|
735
|
+
this.removeOverrideError();
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
info(...args)
|
|
741
|
+
{
|
|
742
|
+
return this.log(...args);
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
warn(...args)
|
|
746
|
+
{
|
|
747
|
+
return this.log(...args);
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
alert(...args)
|
|
751
|
+
{
|
|
752
|
+
if (this.isNode())
|
|
753
|
+
{
|
|
754
|
+
return this.log(...args);
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
const message = args.join(" | ");
|
|
758
|
+
|
|
759
|
+
alert(message);
|
|
760
|
+
}
|
|
761
|
+
|
|
762
|
+
assert(condition, expected = true, ...args)
|
|
763
|
+
{
|
|
764
|
+
let result;
|
|
765
|
+
|
|
766
|
+
try
|
|
767
|
+
{
|
|
768
|
+
if (typeof condition === "function")
|
|
769
|
+
{
|
|
770
|
+
result = condition(...args);
|
|
771
|
+
if (result !== expected)
|
|
772
|
+
{
|
|
773
|
+
this.error("Asset failed");
|
|
774
|
+
return false;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
if (!this.options.hidePassingTests)
|
|
778
|
+
{
|
|
779
|
+
this.log("SUCCESS: Assert passed");
|
|
780
|
+
}
|
|
781
|
+
return true;
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
if (condition !== expected)
|
|
785
|
+
{
|
|
786
|
+
this.error("Assert failed");
|
|
787
|
+
return false;
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
if (!this.options.hidePassingTests)
|
|
791
|
+
{
|
|
792
|
+
this.log("SUCCESS: Assert passed");
|
|
793
|
+
}
|
|
794
|
+
return true;
|
|
795
|
+
}
|
|
796
|
+
catch (e)
|
|
797
|
+
{
|
|
798
|
+
this.error("Unexpected error in assert");
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
return false;
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
module.exports = new AnaLogger();
|
|
807
|
+
module.exports.anaLogger = new AnaLogger();
|
package/src/cjs/bump.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
// Bump version to fix failing build
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const constants = {
|
|
2
|
+
COLOR_TABLE: [
|
|
3
|
+
"#d2466e", // Error context color
|
|
4
|
+
"#FFA07A", // Default context color
|
|
5
|
+
"#FF7F50",
|
|
6
|
+
"#FF6347",
|
|
7
|
+
"#FFE4B5",
|
|
8
|
+
"#ADFF2F",
|
|
9
|
+
"#808000",
|
|
10
|
+
"#40E0D0",
|
|
11
|
+
"#1E90FF",
|
|
12
|
+
"#EE82EE",
|
|
13
|
+
"#708090",
|
|
14
|
+
"#DEB887",
|
|
15
|
+
"#FE642E",
|
|
16
|
+
"#210B61",
|
|
17
|
+
"#088A4B",
|
|
18
|
+
"#5E610B",
|
|
19
|
+
"#FA8258",
|
|
20
|
+
"#088A68",
|
|
21
|
+
"#B40431",
|
|
22
|
+
],
|
|
23
|
+
SYSTEM: {
|
|
24
|
+
BROWSER: "BROWSER",
|
|
25
|
+
NODE: "NODE"
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
module.exports.COLOR_TABLE = constants.COLOR_TABLE;
|
|
31
|
+
module.exports.SYSTEM = constants.SYSTEM;
|