]> git.r.bdr.sh - rbdr/cologne/blob - lib/cologne/log_utilities.js
fc15a8aec0ef0eeb77348810e5687824f6f6dacd
[rbdr/cologne] / lib / cologne / log_utilities.js
1 'use strict';
2
3 let microtime = require('microtime');
4
5 /**
6 * Container object for utilities used by loggers.
7 *
8 * @memberof Cologne
9 * @class LogUtilities
10 */
11 let LogUtilities = {
12
13 /**
14 * Returns the current timestamp in miliseconds as a floating point that
15 * includes fractions (ie. microseconds)
16 *
17 * @function now
18 * @memberof Cologne.LogUtilities
19 * @return {Number} current time in miliseconds, including fractions.
20 */
21 now: function now() {
22 return microtime.nowDouble() * 1000;
23 },
24
25 /**
26 * Stringifies objects, avoiding circular references.
27 *
28 * @function stringify
29 * @memberof Cologne.LogUtilities
30 * @param {Object} object the object to stringify
31 * @return {String} the stringified object
32 */
33 stringify: function stringify(object) {
34 let cache;
35
36 cache = [];
37
38 return JSON.stringify(object, function (key, value) {
39 if (typeof value === 'object' && value !== null) {
40 if (cache.indexOf(value) !== -1) {
41 return this._circularString;
42 }
43
44 cache.push(value);
45 }
46
47 return value;
48 }.bind(this));
49 },
50
51 /**
52 * Given an ansi keyword, it will return the appropriate code.
53 *
54 * @function getAnsiCode
55 * @memberof Cologne.LogUtilities
56 * @param {String} ansiString the name of the desired code
57 * @return {String} the ansi code
58 */
59 getAnsiCode: function getAnsiCode(ansiString) {
60 switch(ansiString) {
61 case 'bold':
62 return '[1m';
63 case 'italics':
64 return '[3m';
65 case 'underline':
66 return '[4m';
67 case 'inverse':
68 return '[7m';
69 case 'strikethrough':
70 return '[9m';
71 case 'bold_off':
72 return '[22m';
73 case 'italics_off':
74 return '[23m';
75 case 'underline_off':
76 return '[24m';
77 case 'inverse_off':
78 return '[27m';
79 case 'strikethrough_off':
80 return '[29m';
81 case 'black':
82 return '[30m';
83 case 'red':
84 return '[31m';
85 case 'green':
86 return '[32m';
87 case 'yellow':
88 return '[33m';
89 case 'blue':
90 return '[34m';
91 case 'magenta':
92 return '[35m';
93 case 'cyan':
94 return '[36m';
95 case 'white':
96 return '[37m';
97 case 'default':
98 return '[39m';
99 case 'black_bg':
100 return '[40m';
101 case 'red_bg':
102 return '[41m';
103 case 'green_bg':
104 return '[42m';
105 case 'yellow_bg':
106 return '[43m';
107 case 'blue_bg':
108 return '[44m';
109 case 'magenta_bg':
110 return '[45m';
111 case 'cyan_bg':
112 return '[46m';
113 case 'white_bg':
114 return '[47m';
115 case 'default_bg':
116 return '[49m';
117 case 'reset': // for informative purpouses
118 default:
119 return '[0m';
120 }
121 },
122
123 /**
124 * Given a level, it will return the appropriate ansi keyword related
125 * to it.
126 *
127 * @function getLevelAnsi
128 * @memberof Cologne.LogUtilities
129 * @param {number} level the level of the log
130 * @return {String} the ansi keyword
131 */
132 getLevelAnsi: function getLevelAnsi(level) {
133 switch(level) {
134 case 0:
135 case 1:
136 case 2:
137 case 3:
138 return 'red';
139 case 4:
140 return 'yellow';
141 case 5:
142 case 6:
143 return 'blue';
144 case 7:
145 return 'green';
146 default:
147 return 'default';
148 }
149 }
150 };
151
152 // String used as default circular reference.
153 LogUtilities._circularString = '[Circular]';
154
155 module.exports = LogUtilities;