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
|
'use strict';
const internals = {
kCircularString: '[Circular]',
kAnsiCodes: {
bold: '[1m',
italics: '[3m',
underline: '[4m',
inverse: '[7m',
strikethrough: '[9m',
bold_off: '[22m',
italics_off: '[23m',
underline_off: '[24m',
inverse_off: '[27m',
strikethrough_off: '[29m',
black: '[30m',
red: '[31m',
green: '[32m',
yellow: '[33m',
blue: '[34m',
magenta: '[35m',
cyan: '[36m',
white: '[37m',
default: '[39m',
black_bg: '[40m',
red_bg: '[41m',
green_bg: '[42m',
yellow_bg: '[43m',
blue_bg: '[44m',
magenta_bg: '[45m',
cyan_bg: '[46m',
white_bg: '[47m',
default_bg: '[49m',
reset: '[0m'
},
// Get High Res time depending on platform. Currently only supporting node.
getHighResTime() {
return process.hrtime.bigint();
}
};
// Initialise relative time.
internals.initialTimestamp = BigInt(Date.now()) * 1000000n;
internals.initialHighResTime = internals.getHighResTime();
/**
* Container object for utilities used by loggers.
*
* @class Utilities
*/
module.exports = {
/**
* Returns the current timestamp in nanoseconds as a bigint.
*
* @function now
* @memberof Utilities
* @return {bigint} current time in nanoseconds, including fractions.
*/
now() {
return internals.initialTimestamp + internals.getHighResTime() - internals.initialHighResTime;
},
/**
* Stringifies objects, avoiding circular references.
*
* @function stringify
* @memberof Utilities
* @param {Object} object the object to stringify
* @return {String} the stringified object
*/
stringify(object) {
const cache = new Set();
return JSON.stringify(object, (key, value) => {
if (typeof value === 'bigint') {
return String(value) + 'n';
}
if (typeof value === 'object' && value !== null) {
if (cache.has(value)) {
return internals.kCircularString;
}
cache.add(value);
}
return value;
});
},
/**
* Given an ansi keyword, it will return the appropriate code.
*
* @function getAnsiCode
* @memberof Utilities
* @param {String} ansiString the name of the desired code
* @return {String} the ansi code
*/
getAnsiCode(ansiString) {
return internals.kAnsiCodes[ansiString] || internals.kAnsiCodes.reset;
},
/**
* Given a level, it will return the appropriate ansi keyword related
* to it.
*
* @function getLevelAnsi
* @memberof Utilities
* @param {number} level the level of the log
* @return {String} the ansi keyword
*/
getLevelAnsi(level) {
switch (level) {
case 0:
case 1:
case 2:
case 3:
return 'red';
case 4:
return 'yellow';
case 5:
case 6:
return 'blue';
case 7:
return 'green';
default:
return 'default';
}
}
};
|