-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
475 lines (409 loc) · 11.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/**
* semlog
* A semantic logger that colors and formats messages automatically according to the content
*
* @author Simon Heimler
*/
'use strict';
//////////////////////////////////////////
// Requirements //
//////////////////////////////////////////
var chalk = require('chalk');
var prettyjson = require('prettyjson');
//////////////////////////////////////////
// Variables //
//////////////////////////////////////////
if (!global.githubFannonSemlog) {
global.githubFannonSemlog = {
history: [],
config: {
silent: false,
colorize: true,
printYaml: false,
logDateTime: true,
printTime: true,
printDateTime: false,
printDebug: true,
printVerbose: true,
historySize: 2048 // 0 for none
},
statistics: {
debug: 0,
warning: 0,
error: 0,
total: 0
}
};
}
/** Publicly export the chalk color library */
exports.chalk = chalk;
//////////////////////////////////////////
// LOGGING FUNCTIONS //
//////////////////////////////////////////
/**
* Custom Logging function
*
* Writes Logs to console, stringifies objects first
*
* @param {string|object} msg Message String or Object
* @param {boolean} [silent] Dot not print message to the console, but stores it to the log history.
*/
exports.log = function(obj, silent) {
if (obj && obj instanceof Error) {
exports.error(obj, silent);
} else if (obj && typeof obj === 'object') {
exports.debug(obj, silent);
} else {
exports.message(obj, silent);
}
};
exports.message = function(msg, silent) {
global.githubFannonSemlog.statistics.total += 1;
if (msg && msg.indexOf && (msg.indexOf('[V]') >= 0 || msg.indexOf('[D]') >= 0)) {
global.githubFannonSemlog.statistics.debug += 1;
} else if (msg && msg.indexOf && msg.indexOf('[W]') >= 0) {
global.githubFannonSemlog.statistics.warning += 1;
} else if (msg && msg.indexOf && msg.indexOf('[E]') >= 0) {
global.githubFannonSemlog.statistics.error += 1;
}
var config = global.githubFannonSemlog.config;
silent = silent || config.silent;
if (typeof msg !== 'string') {
try {
msg = '' + JSON.stringify(msg);
} catch (e) {
msg = '[E] [SEMLOG] Could not stringify given parameter';
}
}
exports.addToHistory(msg);
if (!silent) {
if (config.colorize) {
msg = exports.colorize(msg);
}
if ((config.printTime || config.printDateTime) && msg.trim && msg.trim().length > 0) {
if (config.printDateTime) {
msg = chalk.gray('[' + exports.humanDate() + '] ') + msg;
} else {
msg = chalk.gray('[' + exports.humanTime() + '] ') + msg;
}
}
if (!config.printVerbose && msg.indexOf('[V]') >= 0) {
// Supressing output of verbose message
} else if (!config.printDebug && msg.indexOf('[D]') >= 0) {
// Supressing output of debug message
} else {
console.log(msg);
}
}
};
/**
* Prints out debugging information for the current model object
*
* @param {object} obj Object
*/
exports.debug = function(obj, silent) {
global.githubFannonSemlog.statistics.total += 1;
var config = global.githubFannonSemlog.config;
silent = silent || config.silent;
exports.addToHistory(obj);
if (!silent) {
if (global.githubFannonSemlog.config.printYaml) {
// Print YAML
var options = {
keysColor: 'white',
dashColor: 'white',
stringColor: 'yellow',
numberColor: 'blue'
};
if (!global.githubFannonSemlog.config.colorize) {
options.noColor = true;
}
console.log(chalk.gray('---\n') + prettyjson.render(obj, options));
} else {
// Print indented JSON
var msg = JSON.stringify(obj, false, 4);
console.log(chalk.gray(msg));
}
}
};
/**
* Prints errors
*
* @param {object} obj Object
*/
exports.error = function(obj, silent) {
global.githubFannonSemlog.statistics.total += 1;
global.githubFannonSemlog.statistics.error += 1;
var config = global.githubFannonSemlog.config;
silent = silent || config.silent;
exports.addToHistory(obj);
if (!silent) {
console.error(chalk.red('[E] ' + obj.message));
console.log(chalk.gray(JSON.stringify(obj, null, 4)));
if (obj.stack) {
console.log(chalk.gray(obj.stack));
}
}
};
exports.addToHistory = function(obj) {
var config = global.githubFannonSemlog.config;
var msg = '';
try {
msg = JSON.stringify(obj, null, 4);
// Check that the message history size doesn't get to big
if (config.historySize && global.githubFannonSemlog.history.length >= config.historySize) {
global.githubFannonSemlog.history.shift();
}
if (config.logDateTime) {
msg = '[' + exports.humanDate() + '] ' + msg;
}
} catch (e) {
msg = '[W] Internal semlog error: Could not push circular/invalid object into the history';
}
global.githubFannonSemlog.history.push(msg);
};
/**
* Colors the messages by searching for specific indicator strings
* TODO: Allow to add to the colorMap
*
* @param {string} msg
* @returns {string}
*/
exports.colorize = function(msg) {
var colorMap = {
'[E]': 'red', // ERROR
'[W]': 'yellow', // WARNING
'[?]': 'yellow', // MISSING
'[S]': 'green', // SUCCESS
'[i]': 'blue', // INFO
'[+]': 'green', // ADDED
'[-]': 'red', // REMOVED
'[C]': 'cyan', // CHANGED
'[U]': 'grey', // UNCHANGED
'[=]': 'grey', // EQUAL
'[/]': 'grey', // SKIPPED
'[V]': 'magenta', // VERBOSE
'[D]': 'magenta', // DEBUG
'[T]': 'magenta', // TO-DO
'[TODO]': 'magenta' // TO-DO
};
for (var searchString in colorMap) {
var color = colorMap[searchString];
if (msg && msg.indexOf && msg.indexOf(searchString) > -1) {
return chalk[color](msg);
}
}
return msg;
};
//////////////////////////////////////////
// LOGGER FUNCTIONS //
//////////////////////////////////////////
/**
* Gets the current config
*/
exports.getConfig = function() {
return global.githubFannonSemlog.config;
};
/**
* Gets the current config
*/
exports.getStatistics = function() {
return global.githubFannonSemlog.statistics;
};
/**
* Updates the config.
* Only those parameters that have been given will be updated
*
* @param {object} config
*/
exports.updateConfig = function(config) {
for (var key in config) {
var value = config[key];
global.githubFannonSemlog.config[key] = value;
}
return global.githubFannonSemlog.config;
};
/**
* Clears (empties) the log object
*/
exports.clearLogHistory = function() {
global.githubFannonSemlog.history = [];
global.githubFannonSemlog.statistics = {
debug: 0,
warning: 0,
error: 0,
total: 0
};
};
/**
* Returns the global.moboLogObject
*
* @returns {Array}
*/
exports.getLogHistory = function() {
return global.githubFannonSemlog.history;
};
//////////////////////////////////////////
// HELPER UTILITIES //
//////////////////////////////////////////
/**
* Pad a number with n digits
*
* @param {number} number number to pad
* @param {number} digits number of total digits
* @returns {string}
*/
exports.pad = function(number, digits) {
return new Array(Math.max(digits - String(number).length + 1, 0)).join(0) + number;
};
/**
* Adds dots as thousand separators to numbers
*
* http://stackoverflow.com/a/2901298
*
* @param number
* @returns {string}
*/
exports.prettyNumber = function(number) {
return number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, '.');
};
/**
* Replace all (/.../g) leading slash (^\/) or (|) trailing slash (\/$) with an empty string.
*
* @see http://stackoverflow.com/a/3840645
*
* @param {String} url URL / Path to cleanup
* @returns {String}
*/
exports.cleanUrl = function(url) {
url = url.trim();
return url.replace(/^\/|\/$/g, '');
};
/**
* Strips trailing slashes from URL
*
* @see http://stackoverflow.com/a/6680858/776425
*
* @param {String} url URL to cleanup
* @returns {String}
*/
exports.stripTrailingSlash = function(url) {
if (url.substr(-1) === '/') {
url = url.substr(0, url.length - 1);
}
return url;
};
/**
* Returns the byte length of an utf8 string or an object (when parsed to JSON)
*
* @see http://stackoverflow.com/a/23329386
*/
exports.byteSize = function(obj) {
var str = '';
if (typeof obj === 'object') {
str = JSON.stringify(obj);
} else {
str = obj.toString();
}
var s = str.length;
for (var i = str.length - 1; i >= 0; i--) {
var code = str.charCodeAt(i);
if (code > 0x7f && code <= 0x7ff) {
s++;
} else if (code > 0x7ff && code <= 0xffff) {
s += 2;
}
if (code >= 0xDC00 && code <= 0xDFFF) {
i--;
} //trail surrogate
}
return s;
};
/**
*
* @param bytes
* @param [si]
* @returns {string}
*
* @see http://stackoverflow.com/a/14919494
*/
exports.prettyBytes = function(bytes, si) {
var thresh = si ? 1000 : 1024;
if (bytes < thresh) {
return bytes + ' B';
}
var units = si ? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] : ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
var u = -1;
do {
bytes /= thresh;
++u;
} while (bytes >= thresh);
return bytes.toFixed(1) + ' ' + units[u];
};
/**
* Returns an array with date / time information
* Starts with year at index 0 up to index 6 for milliseconds
*
* @param {Date=} date Optional date object. If falsy, will take current time.
* @returns {Array}
*/
exports.getDateArray = function(date) {
date = date || new Date();
return [
date.getFullYear(),
exports.pad(date.getMonth() + 1, 2),
exports.pad(date.getDate(), 2),
exports.pad(date.getHours(), 2),
exports.pad(date.getMinutes(), 2),
exports.pad(date.getSeconds(), 2),
exports.pad(date.getMilliseconds(), 2)
];
};
/**
* Returns nicely formatted date-time
* @example 2015-02-10 16:01:12
*
* @param {object} [date]
* @returns {string}
*/
exports.humanDate = function(date) {
date = date || new Date();
var d = exports.getDateArray(date);
return d[0] + '-' + d[1] + '-' + d[2] + ' ' + d[3] + ':' + d[4] + ':' + d[5];
};
/**
* Returns a formatted date-time, optimized for machines
* @example 2015-02-10_16-00-08
*
* @param {object} [date]
* @returns {string}
*/
exports.roboDate = function(date) {
date = date || new Date();
var d = exports.getDateArray(date);
return d[0] + '-' + d[1] + '-' + d[2] + '_' + d[3] + '-' + d[4] + '-' + d[5];
};
/**
* Returns nicely formatted date-time
* @example 16:01:12
*
* @param {object} [date]
* @returns {string}
*/
exports.humanTime = function(date) {
date = date || new Date();
var d = exports.getDateArray(date);
return d[3] + ':' + d[4] + ':' + d[5];
};
/**
* Returns a formatted date-time, optimized for machines
* @example 2015-02-10_16-00-08
*
* @param {object} [date]
* @returns {string}
*/
exports.roboTime = function(date) {
date = date || new Date();
var d = exports.getDateArray(date);
return d[3] + '-' + d[4] + '-' + d[5];
};