]> git.r.bdr.sh - rbdr/cologne/blame - lib/utilities.js
Update version in lock
[rbdr/cologne] / lib / utilities.js
CommitLineData
58906d77
RBR
1'use strict';
2
ae85c067
RBR
3const internals = {
4 kCircularString: '[Circular]',
d1571ede
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 },
ae85c067
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
47internals.initialTimestamp = BigInt(Date.now()) * 1000000n;
48internals.initialHighResTime = internals.getHighResTime();
58906d77
RBR
49
50/**
51 * Container object for utilities used by loggers.
52 *
ae85c067 53 * @class Utilities
58906d77 54 */
ae85c067 55module.exports = {
58906d77
RBR
56
57 /**
ae85c067 58 * Returns the current timestamp in nanoseconds as a bigint.
58906d77
RBR
59 *
60 * @function now
ae85c067
RBR
61 * @memberof Utilities
62 * @return {bigint} current time in nanoseconds, including fractions.
58906d77 63 */
ae85c067
RBR
64 now() {
65
66 return internals.initialTimestamp + internals.getHighResTime() - internals.initialHighResTime;
58906d77
RBR
67 },
68
69 /**
70 * Stringifies objects, avoiding circular references.
71 *
72 * @function stringify
ae85c067 73 * @memberof Utilities
58906d77
RBR
74 * @param {Object} object the object to stringify
75 * @return {String} the stringified object
76 */
ae85c067 77 stringify(object) {
58906d77 78
ae85c067
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 }
58906d77 86
58906d77 87 if (typeof value === 'object' && value !== null) {
ae85c067
RBR
88 if (cache.has(value)) {
89 return internals.kCircularString;
58906d77
RBR
90 }
91
ae85c067 92 cache.add(value);
58906d77
RBR
93 }
94
95 return value;
ae85c067 96 });
58906d77
RBR
97 },
98
99 /**
100 * Given an ansi keyword, it will return the appropriate code.
101 *
102 * @function getAnsiCode
ae85c067 103 * @memberof Utilities
58906d77
RBR
104 * @param {String} ansiString the name of the desired code
105 * @return {String} the ansi code
106 */
ae85c067
RBR
107 getAnsiCode(ansiString) {
108
d1571ede 109 return internals.kAnsiCodes[ansiString] || internals.kAnsiCodes.reset;
58906d77
RBR
110 },
111
112 /**
113 * Given a level, it will return the appropriate ansi keyword related
114 * to it.
115 *
116 * @function getLevelAnsi
ae85c067 117 * @memberof Utilities
58906d77
RBR
118 * @param {number} level the level of the log
119 * @return {String} the ansi keyword
120 */
ae85c067
RBR
121 getLevelAnsi(level) {
122
123 switch (level) {
58906d77
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};