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