]>
Commit | Line | Data |
---|---|---|
bfd26c00 RBR |
1 | 'use strict'; |
2 | ||
b3847a2e RBR |
3 | const internals = { |
4 | kCircularString: '[Circular]', | |
1db54b9b RBR |
5 | kAnsiCodes: { |
6 | bold: '[1m', | |
7 | italics: '[3m', | |
8 | underline: '[4m', | |
9 | inverse: '[7m', | |
10 | strikethrough: '[9m', | |
11 | bold_off: '[22m', | |
12 | italics_off: '[23m', | |
13 | underline_off: '[24m', | |
14 | inverse_off: '[27m', | |
15 | strikethrough_off: '[29m', | |
16 | black: '[30m', | |
17 | red: '[31m', | |
18 | green: '[32m', | |
19 | yellow: '[33m', | |
20 | blue: '[34m', | |
21 | magenta: '[35m', | |
22 | cyan: '[36m', | |
23 | white: '[37m', | |
24 | default: '[39m', | |
25 | black_bg: '[40m', | |
26 | red_bg: '[41m', | |
27 | green_bg: '[42m', | |
28 | yellow_bg: '[43m', | |
29 | blue_bg: '[44m', | |
30 | magenta_bg: '[45m', | |
31 | cyan_bg: '[46m', | |
32 | white_bg: '[47m', | |
33 | default_bg: '[49m', | |
34 | reset: '[0m' | |
35 | }, | |
b3847a2e RBR |
36 | |
37 | // Get High Res time depending on platform. Currently only supporting node. | |
38 | ||
39 | getHighResTime() { | |
40 | ||
41 | return process.hrtime.bigint(); | |
42 | } | |
43 | }; | |
44 | ||
45 | // Initialise relative time. | |
46 | ||
47 | internals.initialTimestamp = BigInt(Date.now()) * 1000000n; | |
48 | internals.initialHighResTime = internals.getHighResTime(); | |
bfd26c00 RBR |
49 | |
50 | /** | |
51 | * Container object for utilities used by loggers. | |
52 | * | |
b3847a2e | 53 | * @class Utilities |
bfd26c00 | 54 | */ |
b3847a2e | 55 | module.exports = { |
bfd26c00 RBR |
56 | |
57 | /** | |
b3847a2e | 58 | * Returns the current timestamp in nanoseconds as a bigint. |
bfd26c00 RBR |
59 | * |
60 | * @function now | |
b3847a2e RBR |
61 | * @memberof Utilities |
62 | * @return {bigint} current time in nanoseconds, including fractions. | |
bfd26c00 | 63 | */ |
b3847a2e RBR |
64 | now() { |
65 | ||
66 | return internals.initialTimestamp + internals.getHighResTime() - internals.initialHighResTime; | |
bfd26c00 RBR |
67 | }, |
68 | ||
69 | /** | |
70 | * Stringifies objects, avoiding circular references. | |
71 | * | |
72 | * @function stringify | |
b3847a2e | 73 | * @memberof Utilities |
bfd26c00 RBR |
74 | * @param {Object} object the object to stringify |
75 | * @return {String} the stringified object | |
76 | */ | |
b3847a2e | 77 | stringify(object) { |
bfd26c00 | 78 | |
b3847a2e RBR |
79 | const cache = new Set(); |
80 | ||
81 | return JSON.stringify(object, (key, value) => { | |
82 | ||
83 | if (typeof value === 'bigint') { | |
84 | return String(value) + 'n'; | |
85 | } | |
bfd26c00 | 86 | |
bfd26c00 | 87 | if (typeof value === 'object' && value !== null) { |
b3847a2e RBR |
88 | if (cache.has(value)) { |
89 | return internals.kCircularString; | |
bfd26c00 RBR |
90 | } |
91 | ||
b3847a2e | 92 | cache.add(value); |
bfd26c00 RBR |
93 | } |
94 | ||
95 | return value; | |
b3847a2e | 96 | }); |
bfd26c00 RBR |
97 | }, |
98 | ||
99 | /** | |
100 | * Given an ansi keyword, it will return the appropriate code. | |
101 | * | |
102 | * @function getAnsiCode | |
b3847a2e | 103 | * @memberof Utilities |
bfd26c00 RBR |
104 | * @param {String} ansiString the name of the desired code |
105 | * @return {String} the ansi code | |
106 | */ | |
b3847a2e RBR |
107 | getAnsiCode(ansiString) { |
108 | ||
1db54b9b | 109 | return internals.kAnsiCodes[ansiString] || internals.kAnsiCodes.reset; |
bfd26c00 RBR |
110 | }, |
111 | ||
112 | /** | |
113 | * Given a level, it will return the appropriate ansi keyword related | |
114 | * to it. | |
115 | * | |
116 | * @function getLevelAnsi | |
b3847a2e | 117 | * @memberof Utilities |
bfd26c00 RBR |
118 | * @param {number} level the level of the log |
119 | * @return {String} the ansi keyword | |
120 | */ | |
b3847a2e RBR |
121 | getLevelAnsi(level) { |
122 | ||
123 | switch (level) { | |
bfd26c00 RBR |
124 | case 0: |
125 | case 1: | |
126 | case 2: | |
127 | case 3: | |
128 | return 'red'; | |
129 | case 4: | |
130 | return 'yellow'; | |
131 | case 5: | |
132 | case 6: | |
133 | return 'blue'; | |
134 | case 7: | |
135 | return 'green'; | |
136 | default: | |
137 | return 'default'; | |
138 | } | |
139 | } | |
140 | }; |