aboutsummaryrefslogtreecommitdiff
path: root/lib/cologne/log_utilities.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/cologne/log_utilities.js')
-rw-r--r--lib/cologne/log_utilities.js155
1 files changed, 155 insertions, 0 deletions
diff --git a/lib/cologne/log_utilities.js b/lib/cologne/log_utilities.js
new file mode 100644
index 0000000..fc15a8a
--- /dev/null
+++ b/lib/cologne/log_utilities.js
@@ -0,0 +1,155 @@
+'use strict';
+
+let microtime = require('microtime');
+
+/**
+ * Container object for utilities used by loggers.
+ *
+ * @memberof Cologne
+ * @class LogUtilities
+ */
+let LogUtilities = {
+
+ /**
+ * Returns the current timestamp in miliseconds as a floating point that
+ * includes fractions (ie. microseconds)
+ *
+ * @function now
+ * @memberof Cologne.LogUtilities
+ * @return {Number} current time in miliseconds, including fractions.
+ */
+ now: function now() {
+ return microtime.nowDouble() * 1000;
+ },
+
+ /**
+ * Stringifies objects, avoiding circular references.
+ *
+ * @function stringify
+ * @memberof Cologne.LogUtilities
+ * @param {Object} object the object to stringify
+ * @return {String} the stringified object
+ */
+ stringify: function stringify(object) {
+ let cache;
+
+ cache = [];
+
+ return JSON.stringify(object, function (key, value) {
+ if (typeof value === 'object' && value !== null) {
+ if (cache.indexOf(value) !== -1) {
+ return this._circularString;
+ }
+
+ cache.push(value);
+ }
+
+ return value;
+ }.bind(this));
+ },
+
+ /**
+ * Given an ansi keyword, it will return the appropriate code.
+ *
+ * @function getAnsiCode
+ * @memberof Cologne.LogUtilities
+ * @param {String} ansiString the name of the desired code
+ * @return {String} the ansi code
+ */
+ getAnsiCode: function getAnsiCode(ansiString) {
+ switch(ansiString) {
+ case 'bold':
+ return '[1m';
+ case 'italics':
+ return '[3m';
+ case 'underline':
+ return '[4m';
+ case 'inverse':
+ return '[7m';
+ case 'strikethrough':
+ return '[9m';
+ case 'bold_off':
+ return '[22m';
+ case 'italics_off':
+ return '[23m';
+ case 'underline_off':
+ return '[24m';
+ case 'inverse_off':
+ return '[27m';
+ case 'strikethrough_off':
+ return '[29m';
+ case 'black':
+ return '[30m';
+ case 'red':
+ return '[31m';
+ case 'green':
+ return '[32m';
+ case 'yellow':
+ return '[33m';
+ case 'blue':
+ return '[34m';
+ case 'magenta':
+ return '[35m';
+ case 'cyan':
+ return '[36m';
+ case 'white':
+ return '[37m';
+ case 'default':
+ return '[39m';
+ case 'black_bg':
+ return '[40m';
+ case 'red_bg':
+ return '[41m';
+ case 'green_bg':
+ return '[42m';
+ case 'yellow_bg':
+ return '[43m';
+ case 'blue_bg':
+ return '[44m';
+ case 'magenta_bg':
+ return '[45m';
+ case 'cyan_bg':
+ return '[46m';
+ case 'white_bg':
+ return '[47m';
+ case 'default_bg':
+ return '[49m';
+ case 'reset': // for informative purpouses
+ default:
+ return '[0m';
+ }
+ },
+
+ /**
+ * Given a level, it will return the appropriate ansi keyword related
+ * to it.
+ *
+ * @function getLevelAnsi
+ * @memberof Cologne.LogUtilities
+ * @param {number} level the level of the log
+ * @return {String} the ansi keyword
+ */
+ getLevelAnsi: function getLevelAnsi(level) {
+ switch(level) {
+ case 0:
+ case 1:
+ case 2:
+ case 3:
+ return 'red';
+ case 4:
+ return 'yellow';
+ case 5:
+ case 6:
+ return 'blue';
+ case 7:
+ return 'green';
+ default:
+ return 'default';
+ }
+ }
+};
+
+// String used as default circular reference.
+LogUtilities._circularString = '[Circular]';
+
+module.exports = LogUtilities;