testing/mochitest/tests/MochiKit-1.4.2/MochiKit/Logging.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/mochitest/tests/MochiKit-1.4.2/MochiKit/Logging.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,315 @@
     1.4 +/***
     1.5 +
     1.6 +MochiKit.Logging 1.4.2
     1.7 +
     1.8 +See <http://mochikit.com/> for documentation, downloads, license, etc.
     1.9 +
    1.10 +(c) 2005 Bob Ippolito.  All rights Reserved.
    1.11 +
    1.12 +***/
    1.13 +
    1.14 +MochiKit.Base._deps('Logging', ['Base']);
    1.15 +
    1.16 +MochiKit.Logging.NAME = "MochiKit.Logging";
    1.17 +MochiKit.Logging.VERSION = "1.4.2";
    1.18 +MochiKit.Logging.__repr__ = function () {
    1.19 +    return "[" + this.NAME + " " + this.VERSION + "]";
    1.20 +};
    1.21 +
    1.22 +MochiKit.Logging.toString = function () {
    1.23 +    return this.__repr__();
    1.24 +};
    1.25 +
    1.26 +
    1.27 +MochiKit.Logging.EXPORT = [
    1.28 +    "LogLevel",
    1.29 +    "LogMessage",
    1.30 +    "Logger",
    1.31 +    "alertListener",
    1.32 +    "logger",
    1.33 +    "log",
    1.34 +    "logError",
    1.35 +    "logDebug",
    1.36 +    "logFatal",
    1.37 +    "logWarning"
    1.38 +];
    1.39 +
    1.40 +
    1.41 +MochiKit.Logging.EXPORT_OK = [
    1.42 +    "logLevelAtLeast",
    1.43 +    "isLogMessage",
    1.44 +    "compareLogMessage"
    1.45 +];
    1.46 +
    1.47 +
    1.48 +/** @id MochiKit.Logging.LogMessage */
    1.49 +MochiKit.Logging.LogMessage = function (num, level, info) {
    1.50 +    this.num = num;
    1.51 +    this.level = level;
    1.52 +    this.info = info;
    1.53 +    this.timestamp = new Date();
    1.54 +};
    1.55 +
    1.56 +MochiKit.Logging.LogMessage.prototype = {
    1.57 +     /** @id MochiKit.Logging.LogMessage.prototype.repr */
    1.58 +    repr: function () {
    1.59 +        var m = MochiKit.Base;
    1.60 +        return 'LogMessage(' +
    1.61 +            m.map(
    1.62 +                m.repr,
    1.63 +                [this.num, this.level, this.info]
    1.64 +            ).join(', ') + ')';
    1.65 +    },
    1.66 +    /** @id MochiKit.Logging.LogMessage.prototype.toString */
    1.67 +    toString: MochiKit.Base.forwardCall("repr")
    1.68 +};
    1.69 +
    1.70 +MochiKit.Base.update(MochiKit.Logging, {
    1.71 +    /** @id MochiKit.Logging.logLevelAtLeast */
    1.72 +    logLevelAtLeast: function (minLevel) {
    1.73 +        var self = MochiKit.Logging;
    1.74 +        if (typeof(minLevel) == 'string') {
    1.75 +            minLevel = self.LogLevel[minLevel];
    1.76 +        }
    1.77 +        return function (msg) {
    1.78 +            var msgLevel = msg.level;
    1.79 +            if (typeof(msgLevel) == 'string') {
    1.80 +                msgLevel = self.LogLevel[msgLevel];
    1.81 +            }
    1.82 +            return msgLevel >= minLevel;
    1.83 +        };
    1.84 +    },
    1.85 +
    1.86 +    /** @id MochiKit.Logging.isLogMessage */
    1.87 +    isLogMessage: function (/* ... */) {
    1.88 +        var LogMessage = MochiKit.Logging.LogMessage;
    1.89 +        for (var i = 0; i < arguments.length; i++) {
    1.90 +            if (!(arguments[i] instanceof LogMessage)) {
    1.91 +                return false;
    1.92 +            }
    1.93 +        }
    1.94 +        return true;
    1.95 +    },
    1.96 +
    1.97 +    /** @id MochiKit.Logging.compareLogMessage */
    1.98 +    compareLogMessage: function (a, b) {
    1.99 +        return MochiKit.Base.compare([a.level, a.info], [b.level, b.info]);
   1.100 +    },
   1.101 +
   1.102 +    /** @id MochiKit.Logging.alertListener */
   1.103 +    alertListener: function (msg) {
   1.104 +        alert(
   1.105 +            "num: " + msg.num +
   1.106 +            "\nlevel: " +  msg.level +
   1.107 +            "\ninfo: " + msg.info.join(" ")
   1.108 +        );
   1.109 +    }
   1.110 +
   1.111 +});
   1.112 +
   1.113 +/** @id MochiKit.Logging.Logger */
   1.114 +MochiKit.Logging.Logger = function (/* optional */maxSize) {
   1.115 +    this.counter = 0;
   1.116 +    if (typeof(maxSize) == 'undefined' || maxSize === null) {
   1.117 +        maxSize = -1;
   1.118 +    }
   1.119 +    this.maxSize = maxSize;
   1.120 +    this._messages = [];
   1.121 +    this.listeners = {};
   1.122 +    this.useNativeConsole = false;
   1.123 +};
   1.124 +
   1.125 +MochiKit.Logging.Logger.prototype = {
   1.126 +    /** @id MochiKit.Logging.Logger.prototype.clear */
   1.127 +    clear: function () {
   1.128 +        this._messages.splice(0, this._messages.length);
   1.129 +    },
   1.130 +
   1.131 +    /** @id MochiKit.Logging.Logger.prototype.logToConsole */
   1.132 +    logToConsole: function (msg) {
   1.133 +        if (typeof(window) != "undefined" && window.console
   1.134 +                && window.console.log) {
   1.135 +            // Safari and FireBug 0.4
   1.136 +            // Percent replacement is a workaround for cute Safari crashing bug
   1.137 +            window.console.log(msg.replace(/%/g, '\uFF05'));
   1.138 +        } else if (typeof(opera) != "undefined" && opera.postError) {
   1.139 +            // Opera
   1.140 +            opera.postError(msg);
   1.141 +        } else if (typeof(printfire) == "function") {
   1.142 +            // FireBug 0.3 and earlier
   1.143 +            printfire(msg);
   1.144 +        } else if (typeof(Debug) != "undefined" && Debug.writeln) {
   1.145 +            // IE Web Development Helper (?)
   1.146 +            // http://www.nikhilk.net/Entry.aspx?id=93
   1.147 +            Debug.writeln(msg);
   1.148 +        } else if (typeof(debug) != "undefined" && debug.trace) {
   1.149 +            // Atlas framework (?)
   1.150 +            // http://www.nikhilk.net/Entry.aspx?id=93
   1.151 +            debug.trace(msg);
   1.152 +        }
   1.153 +    },
   1.154 +
   1.155 +    /** @id MochiKit.Logging.Logger.prototype.dispatchListeners */
   1.156 +    dispatchListeners: function (msg) {
   1.157 +        for (var k in this.listeners) {
   1.158 +            var pair = this.listeners[k];
   1.159 +            if (pair.ident != k || (pair[0] && !pair[0](msg))) {
   1.160 +                continue;
   1.161 +            }
   1.162 +            pair[1](msg);
   1.163 +        }
   1.164 +    },
   1.165 +
   1.166 +    /** @id MochiKit.Logging.Logger.prototype.addListener */
   1.167 +    addListener: function (ident, filter, listener) {
   1.168 +        if (typeof(filter) == 'string') {
   1.169 +            filter = MochiKit.Logging.logLevelAtLeast(filter);
   1.170 +        }
   1.171 +        var entry = [filter, listener];
   1.172 +        entry.ident = ident;
   1.173 +        this.listeners[ident] = entry;
   1.174 +    },
   1.175 +
   1.176 +    /** @id MochiKit.Logging.Logger.prototype.removeListener */
   1.177 +    removeListener: function (ident) {
   1.178 +        delete this.listeners[ident];
   1.179 +    },
   1.180 +
   1.181 +    /** @id MochiKit.Logging.Logger.prototype.baseLog */
   1.182 +    baseLog: function (level, message/*, ...*/) {
   1.183 +        if (typeof(level) == "number") {
   1.184 +            if (level >= MochiKit.Logging.LogLevel.FATAL) {
   1.185 +                level = 'FATAL';
   1.186 +            } else if (level >= MochiKit.Logging.LogLevel.ERROR) {
   1.187 +                level = 'ERROR';
   1.188 +            } else if (level >= MochiKit.Logging.LogLevel.WARNING) {
   1.189 +                level = 'WARNING';
   1.190 +            } else if (level >= MochiKit.Logging.LogLevel.INFO) {
   1.191 +                level = 'INFO';
   1.192 +            } else {
   1.193 +                level = 'DEBUG';
   1.194 +            }
   1.195 +        }
   1.196 +        var msg = new MochiKit.Logging.LogMessage(
   1.197 +            this.counter,
   1.198 +            level,
   1.199 +            MochiKit.Base.extend(null, arguments, 1)
   1.200 +        );
   1.201 +        this._messages.push(msg);
   1.202 +        this.dispatchListeners(msg);
   1.203 +        if (this.useNativeConsole) {
   1.204 +            this.logToConsole(msg.level + ": " + msg.info.join(" "));
   1.205 +        }
   1.206 +        this.counter += 1;
   1.207 +        while (this.maxSize >= 0 && this._messages.length > this.maxSize) {
   1.208 +            this._messages.shift();
   1.209 +        }
   1.210 +    },
   1.211 +
   1.212 +    /** @id MochiKit.Logging.Logger.prototype.getMessages */
   1.213 +    getMessages: function (howMany) {
   1.214 +        var firstMsg = 0;
   1.215 +        if (!(typeof(howMany) == 'undefined' || howMany === null)) {
   1.216 +            firstMsg = Math.max(0, this._messages.length - howMany);
   1.217 +        }
   1.218 +        return this._messages.slice(firstMsg);
   1.219 +    },
   1.220 +
   1.221 +    /** @id MochiKit.Logging.Logger.prototype.getMessageText */
   1.222 +    getMessageText: function (howMany) {
   1.223 +        if (typeof(howMany) == 'undefined' || howMany === null) {
   1.224 +            howMany = 30;
   1.225 +        }
   1.226 +        var messages = this.getMessages(howMany);
   1.227 +        if (messages.length) {
   1.228 +            var lst = map(function (m) {
   1.229 +                return '\n  [' + m.num + '] ' + m.level + ': ' + m.info.join(' ');
   1.230 +            }, messages);
   1.231 +            lst.unshift('LAST ' + messages.length + ' MESSAGES:');
   1.232 +            return lst.join('');
   1.233 +        }
   1.234 +        return '';
   1.235 +    },
   1.236 +
   1.237 +    /** @id MochiKit.Logging.Logger.prototype.debuggingBookmarklet */
   1.238 +    debuggingBookmarklet: function (inline) {
   1.239 +        if (typeof(MochiKit.LoggingPane) == "undefined") {
   1.240 +            alert(this.getMessageText());
   1.241 +        } else {
   1.242 +            MochiKit.LoggingPane.createLoggingPane(inline || false);
   1.243 +        }
   1.244 +    }
   1.245 +};
   1.246 +
   1.247 +MochiKit.Logging.__new__ = function () {
   1.248 +    this.LogLevel = {
   1.249 +        ERROR: 40,
   1.250 +        FATAL: 50,
   1.251 +        WARNING: 30,
   1.252 +        INFO: 20,
   1.253 +        DEBUG: 10
   1.254 +    };
   1.255 +
   1.256 +    var m = MochiKit.Base;
   1.257 +    m.registerComparator("LogMessage",
   1.258 +        this.isLogMessage,
   1.259 +        this.compareLogMessage
   1.260 +    );
   1.261 +
   1.262 +    var partial = m.partial;
   1.263 +
   1.264 +    var Logger = this.Logger;
   1.265 +    var baseLog = Logger.prototype.baseLog;
   1.266 +    m.update(this.Logger.prototype, {
   1.267 +        debug: partial(baseLog, 'DEBUG'),
   1.268 +        log: partial(baseLog, 'INFO'),
   1.269 +        error: partial(baseLog, 'ERROR'),
   1.270 +        fatal: partial(baseLog, 'FATAL'),
   1.271 +        warning: partial(baseLog, 'WARNING')
   1.272 +    });
   1.273 +
   1.274 +    // indirectly find logger so it can be replaced
   1.275 +    var self = this;
   1.276 +    var connectLog = function (name) {
   1.277 +        return function () {
   1.278 +            self.logger[name].apply(self.logger, arguments);
   1.279 +        };
   1.280 +    };
   1.281 +
   1.282 +    /** @id MochiKit.Logging.log */
   1.283 +    this.log = connectLog('log');
   1.284 +    /** @id MochiKit.Logging.logError */
   1.285 +    this.logError = connectLog('error');
   1.286 +    /** @id MochiKit.Logging.logDebug */
   1.287 +    this.logDebug = connectLog('debug');
   1.288 +    /** @id MochiKit.Logging.logFatal */
   1.289 +    this.logFatal = connectLog('fatal');
   1.290 +    /** @id MochiKit.Logging.logWarning */
   1.291 +    this.logWarning = connectLog('warning');
   1.292 +    this.logger = new Logger();
   1.293 +    this.logger.useNativeConsole = true;
   1.294 +
   1.295 +    this.EXPORT_TAGS = {
   1.296 +        ":common": this.EXPORT,
   1.297 +        ":all": m.concat(this.EXPORT, this.EXPORT_OK)
   1.298 +    };
   1.299 +
   1.300 +    m.nameFunctions(this);
   1.301 +
   1.302 +};
   1.303 +
   1.304 +if (typeof(printfire) == "undefined" &&
   1.305 +        typeof(document) != "undefined" && document.createEvent &&
   1.306 +        typeof(dispatchEvent) != "undefined") {
   1.307 +    // FireBug really should be less lame about this global function
   1.308 +    printfire  = function () {
   1.309 +        printfire.args = arguments;
   1.310 +        var ev = document.createEvent("Events");
   1.311 +        ev.initEvent("printfire", false, true);
   1.312 +        dispatchEvent(ev);
   1.313 +    };
   1.314 +}
   1.315 +
   1.316 +MochiKit.Logging.__new__();
   1.317 +
   1.318 +MochiKit.Base._exportSymbols(this, MochiKit.Logging);

mercurial