testing/mochitest/MochiKit/Logging.js

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

mercurial