]>
Commit | Line | Data |
---|---|---|
58906d77 RBR |
1 | 'use strict'; |
2 | ||
ae85c067 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(); | |
58906d77 RBR |
18 | |
19 | /** | |
20 | * Container object for utilities used by loggers. | |
21 | * | |
ae85c067 | 22 | * @class Utilities |
58906d77 | 23 | */ |
ae85c067 | 24 | module.exports = { |
58906d77 RBR |
25 | |
26 | /** | |
ae85c067 | 27 | * Returns the current timestamp in nanoseconds as a bigint. |
58906d77 RBR |
28 | * |
29 | * @function now | |
ae85c067 RBR |
30 | * @memberof Utilities |
31 | * @return {bigint} current time in nanoseconds, including fractions. | |
58906d77 | 32 | */ |
ae85c067 RBR |
33 | now() { |
34 | ||
35 | return internals.initialTimestamp + internals.getHighResTime() - internals.initialHighResTime; | |
58906d77 RBR |
36 | }, |
37 | ||
38 | /** | |
39 | * Stringifies objects, avoiding circular references. | |
40 | * | |
41 | * @function stringify | |
ae85c067 | 42 | * @memberof Utilities |
58906d77 RBR |
43 | * @param {Object} object the object to stringify |
44 | * @return {String} the stringified object | |
45 | */ | |
ae85c067 | 46 | stringify(object) { |
58906d77 | 47 | |
ae85c067 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 | } | |
58906d77 | 55 | |
58906d77 | 56 | if (typeof value === 'object' && value !== null) { |
ae85c067 RBR |
57 | if (cache.has(value)) { |
58 | return internals.kCircularString; | |
58906d77 RBR |
59 | } |
60 | ||
ae85c067 | 61 | cache.add(value); |
58906d77 RBR |
62 | } |
63 | ||
64 | return value; | |
ae85c067 | 65 | }); |
58906d77 RBR |
66 | }, |
67 | ||
68 | /** | |
69 | * Given an ansi keyword, it will return the appropriate code. | |
70 | * | |
71 | * @function getAnsiCode | |
ae85c067 | 72 | * @memberof Utilities |
58906d77 RBR |
73 | * @param {String} ansiString the name of the desired code |
74 | * @return {String} the ansi code | |
75 | */ | |
ae85c067 RBR |
76 | getAnsiCode(ansiString) { |
77 | ||
78 | switch (ansiString) { | |
58906d77 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 | |
ae85c067 | 146 | * @memberof Utilities |
58906d77 RBR |
147 | * @param {number} level the level of the log |
148 | * @return {String} the ansi keyword | |
149 | */ | |
ae85c067 RBR |
150 | getLevelAnsi(level) { |
151 | ||
152 | switch (level) { | |
58906d77 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 | }; |