logger.js 4.56 KB
Newer Older
1 2 3 4 5
(function(define) {
    'use strict';
    define(['underscore', 'logger'], function(_, Logger) {
        var loggers = [],
            NotesLogger, now, destroyLogger;
6

7 8 9 10 11 12 13 14 15
        now = function() {
            if (performance && performance.now) {
                return performance.now();
            } else if (Date.now) {
                return Date.now();
            } else {
                return (new Date()).getTime();
            }
        };
16 17 18 19 20

    /**
     * Removes a reference on the logger from `loggers`.
     * @param  {Object} logger An instance of Logger.
     */
21 22 23
        destroyLogger = function(logger) {
            var index = loggers.length,
                removedLogger;
24

25 26 27 28 29 30 31
            while (index--) {
                if (loggers[index].id === logger.id) {
                    removedLogger = loggers.splice(index, 1)[0];
                    removedLogger.historyStorage = [];
                    removedLogger.timeStorage = {};
                    break;
                }
32
            }
33
        };
34 35 36 37 38 39 40

    /**
     * NotesLogger constructor.
     * @constructor
     * @param {String} id Id of the logger.
     * @param {Boolean|Number} mode Outputs messages to the Web Console if true.
     */
41 42 43 44
        NotesLogger = function(id, mode) {
            this.id = id;
            this.historyStorage = [];
            this.timeStorage = {};
45 46
        // 0 - silent;
        // 1 - show logs;
47 48
            this.logLevel = mode;
        };
49 50 51 52 53 54 55

    /**
     * Outputs a message with appropriate type to the Web Console and
     * store it in the history.
     * @param  {String} logType The type of the log message.
     * @param  {Arguments} args Information that will be stored.
     */
56 57 58 59 60
        NotesLogger.prototype._log = function(logType, args) {
            if (!this.logLevel) {
                return false;
            }
            this.updateHistory.apply(this, arguments);
61
        // Adds ID at the first place
62 63 64 65 66 67 68
            Array.prototype.unshift.call(args, this.id);
            if (console && console[logType]) {
                if (console[logType].apply) {
                    console[logType].apply(console, args);
                } else { // Do this for IE
                    console[logType](args.join(' '));
                }
69
            }
70
        };
71 72 73 74

    /**
     * Outputs a message to the Web Console and store it in the history.
     */
75 76 77
        NotesLogger.prototype.log = function() {
            this._log('log', arguments);
        };
78 79 80 81

    /**
     * Outputs an error message to the Web Console and store it in the history.
     */
82 83 84
        NotesLogger.prototype.error = function() {
            this._log('error', arguments);
        };
85 86 87 88

    /**
     * Adds information to the history.
     */
89 90 91
        NotesLogger.prototype.updateHistory = function() {
            this.historyStorage.push(arguments);
        };
92 93 94 95 96

    /**
     * Returns the history for the logger.
     * @return {Array}
     */
97 98 99
        NotesLogger.prototype.getHistory = function() {
            return this.historyStorage;
        };
100 101 102 103 104

    /**
     * Starts a timer you can use to track how long an operation takes.
     * @param {String} label Timer name.
     */
105 106 107
        NotesLogger.prototype.time = function(label) {
            this.timeStorage[label] = now();
        };
108 109 110 111 112

    /**
     * Stops a timer that was previously started by calling NotesLogger.prototype.time().
     * @param {String} label Timer name.
     */
113 114 115 116
        NotesLogger.prototype.timeEnd = function(label) {
            if (!this.timeStorage[label]) {
                return null;
            }
117

118 119 120
            this._log('log', [label, now() - this.timeStorage[label], 'ms']);
            delete this.timeStorage[label];
        };
121

122 123 124
        NotesLogger.prototype.destroy = function() {
            destroyLogger(this);
        };
125 126 127 128 129 130 131

    /**
     * Emits the event.
     * @param  {String}  eventName The name of the event.
     * @param  {*}       data      Information about the event.
     * @param  {Number}  timeout   Optional timeout for the ajax request in ms.
     */
132 133 134 135 136 137 138 139
        NotesLogger.prototype.emit = function(eventName, data, timeout) {
            var args = [eventName, data];
            this.log(eventName, data);
            if (timeout) {
                args.push(null, {'timeout': timeout});
            }
            return Logger.log.apply(Logger, args);
        };
140

141 142 143 144 145 146 147 148 149
        return {
            getLogger: function(id, mode) {
                var logger = new NotesLogger(id, mode);
                loggers.push(logger);
                return logger;
            },
            destroyLogger: destroyLogger
        };
    });
150
}).call(this, define || RequireJS.define);