testing/mochitest/MochiKit/LoggingPane.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/mochitest/MochiKit/LoggingPane.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,371 @@
     1.4 +/***
     1.5 +
     1.6 +MochiKit.LoggingPane 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.LoggingPane');
    1.16 +    dojo.require('MochiKit.Logging');
    1.17 +    dojo.require('MochiKit.Base');
    1.18 +}
    1.19 +
    1.20 +if (typeof(JSAN) != 'undefined') {
    1.21 +    JSAN.use("MochiKit.Logging", []);
    1.22 +    JSAN.use("MochiKit.Base", []);
    1.23 +}
    1.24 +
    1.25 +try {
    1.26 +    if (typeof(MochiKit.Base) == 'undefined' || typeof(MochiKit.Logging) == 'undefined') {
    1.27 +        throw "";
    1.28 +    }
    1.29 +} catch (e) {
    1.30 +    throw "MochiKit.LoggingPane depends on MochiKit.Base and MochiKit.Logging!";
    1.31 +}
    1.32 +
    1.33 +if (typeof(MochiKit.LoggingPane) == 'undefined') {
    1.34 +    MochiKit.LoggingPane = {};
    1.35 +}
    1.36 +
    1.37 +MochiKit.LoggingPane.NAME = "MochiKit.LoggingPane";
    1.38 +MochiKit.LoggingPane.VERSION = "1.4";
    1.39 +MochiKit.LoggingPane.__repr__ = function () {
    1.40 +    return "[" + this.NAME + " " + this.VERSION + "]";
    1.41 +};
    1.42 +
    1.43 +MochiKit.LoggingPane.toString = function () {
    1.44 +    return this.__repr__();
    1.45 +};
    1.46 +
    1.47 +/** @id MochiKit.LoggingPane.createLoggingPane */
    1.48 +MochiKit.LoggingPane.createLoggingPane = function (inline/* = false */) {
    1.49 +    var m = MochiKit.LoggingPane;
    1.50 +    inline = !(!inline);
    1.51 +    if (m._loggingPane && m._loggingPane.inline != inline) {
    1.52 +        m._loggingPane.closePane();
    1.53 +        m._loggingPane = null;
    1.54 +    }
    1.55 +    if (!m._loggingPane || m._loggingPane.closed) {
    1.56 +        m._loggingPane = new m.LoggingPane(inline, MochiKit.Logging.logger);
    1.57 +    }
    1.58 +    return m._loggingPane;
    1.59 +};
    1.60 +
    1.61 +/** @id MochiKit.LoggingPane.LoggingPane */
    1.62 +MochiKit.LoggingPane.LoggingPane = function (inline/* = false */, logger/* = MochiKit.Logging.logger */) {
    1.63 +        
    1.64 +    /* Use a div if inline, pop up a window if not */
    1.65 +    /* Create the elements */
    1.66 +    if (typeof(logger) == "undefined" || logger === null) {
    1.67 +        logger = MochiKit.Logging.logger;
    1.68 +    }
    1.69 +    this.logger = logger;
    1.70 +    var update = MochiKit.Base.update;
    1.71 +    var updatetree = MochiKit.Base.updatetree;
    1.72 +    var bind = MochiKit.Base.bind;
    1.73 +    var clone = MochiKit.Base.clone;
    1.74 +    var win = window;
    1.75 +    var uid = "_MochiKit_LoggingPane";
    1.76 +    if (typeof(MochiKit.DOM) != "undefined") {
    1.77 +        win = MochiKit.DOM.currentWindow();
    1.78 +    }
    1.79 +    if (!inline) {
    1.80 +        // name the popup with the base URL for uniqueness
    1.81 +        var url = win.location.href.split("?")[0].replace(/[#:\/.><&-]/g, "_");
    1.82 +        var name = uid + "_" + url;
    1.83 +        var nwin = win.open("", name, "dependent,resizable,height=200");
    1.84 +        if (!nwin) {
    1.85 +            alert("Not able to open debugging window due to pop-up blocking.");
    1.86 +            return undefined;
    1.87 +        }
    1.88 +        nwin.document.write(
    1.89 +            '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" '
    1.90 +            + '"http://www.w3.org/TR/html4/loose.dtd">'
    1.91 +            + '<html><head><title>[MochiKit.LoggingPane]</title></head>'
    1.92 +            + '<body></body></html>'
    1.93 +        );
    1.94 +        nwin.document.close();
    1.95 +        nwin.document.title += ' ' + win.document.title;
    1.96 +        win = nwin;
    1.97 +    }
    1.98 +    var doc = win.document;
    1.99 +    this.doc = doc;
   1.100 +    
   1.101 +    // Connect to the debug pane if it already exists (i.e. in a window orphaned by the page being refreshed)
   1.102 +    var debugPane = doc.getElementById(uid);
   1.103 +    var existing_pane = !!debugPane;
   1.104 +    if (debugPane && typeof(debugPane.loggingPane) != "undefined") {
   1.105 +        debugPane.loggingPane.logger = this.logger;
   1.106 +        debugPane.loggingPane.buildAndApplyFilter();
   1.107 +        return debugPane.loggingPane;
   1.108 +    }
   1.109 +    
   1.110 +    if (existing_pane) {
   1.111 +        // clear any existing contents
   1.112 +        var child;
   1.113 +        while ((child = debugPane.firstChild)) {
   1.114 +            debugPane.removeChild(child);
   1.115 +        }
   1.116 +    } else {
   1.117 +        debugPane = doc.createElement("div");
   1.118 +        debugPane.id = uid;
   1.119 +    }
   1.120 +    debugPane.loggingPane = this;
   1.121 +    var levelFilterField = doc.createElement("input");
   1.122 +    var infoFilterField = doc.createElement("input");
   1.123 +    var filterButton = doc.createElement("button");
   1.124 +    var loadButton = doc.createElement("button");
   1.125 +    var clearButton = doc.createElement("button");
   1.126 +    var closeButton = doc.createElement("button");
   1.127 +    var logPaneArea = doc.createElement("div");
   1.128 +    var logPane = doc.createElement("div");
   1.129 +
   1.130 +    /* Set up the functions */
   1.131 +    var listenerId = uid + "_Listener";
   1.132 +    this.colorTable = clone(this.colorTable);
   1.133 +    var messages = [];
   1.134 +    var messageFilter = null;
   1.135 +
   1.136 +    /** @id MochiKit.LoggingPane.messageLevel */
   1.137 +    var messageLevel = function (msg) {
   1.138 +        var level = msg.level;
   1.139 +        if (typeof(level) == "number") {
   1.140 +            level = MochiKit.Logging.LogLevel[level];
   1.141 +        }
   1.142 +        return level;
   1.143 +    };
   1.144 +
   1.145 +    /** @id MochiKit.LoggingPane.messageText */
   1.146 +    var messageText = function (msg) {
   1.147 +        return msg.info.join(" ");
   1.148 +    };
   1.149 +
   1.150 +    /** @id MochiKit.LoggingPane.addMessageText */
   1.151 +    var addMessageText = bind(function (msg) {
   1.152 +        var level = messageLevel(msg);
   1.153 +        var text = messageText(msg);
   1.154 +        var c = this.colorTable[level];
   1.155 +        var p = doc.createElement("span");
   1.156 +        p.className = "MochiKit-LogMessage MochiKit-LogLevel-" + level;
   1.157 +        p.style.cssText = "margin: 0px; white-space: -moz-pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; white-space: pre-line; word-wrap: break-word; wrap-option: emergency; color: " + c;
   1.158 +        p.appendChild(doc.createTextNode(level + ": " + text));
   1.159 +        logPane.appendChild(p);
   1.160 +        logPane.appendChild(doc.createElement("br"));
   1.161 +        if (logPaneArea.offsetHeight > logPaneArea.scrollHeight) {
   1.162 +            logPaneArea.scrollTop = 0;
   1.163 +        } else {
   1.164 +            logPaneArea.scrollTop = logPaneArea.scrollHeight;
   1.165 +        }
   1.166 +    }, this);
   1.167 +
   1.168 +    /** @id MochiKit.LoggingPane.addMessage */
   1.169 +    var addMessage = function (msg) {
   1.170 +        messages[messages.length] = msg;
   1.171 +        addMessageText(msg);
   1.172 +    };
   1.173 +
   1.174 +    /** @id MochiKit.LoggingPane.buildMessageFilter */
   1.175 +    var buildMessageFilter = function () {
   1.176 +        var levelre, infore;
   1.177 +        try {
   1.178 +            /* Catch any exceptions that might arise due to invalid regexes */
   1.179 +            levelre = new RegExp(levelFilterField.value);
   1.180 +            infore = new RegExp(infoFilterField.value);
   1.181 +        } catch(e) {
   1.182 +            /* If there was an error with the regexes, do no filtering */
   1.183 +            logDebug("Error in filter regex: " + e.message);
   1.184 +            return null;
   1.185 +        }
   1.186 +
   1.187 +        return function (msg) {
   1.188 +            return (
   1.189 +                levelre.test(messageLevel(msg)) &&
   1.190 +                infore.test(messageText(msg))
   1.191 +            );
   1.192 +        };
   1.193 +    };
   1.194 +
   1.195 +    /** @id MochiKit.LoggingPane.clearMessagePane */
   1.196 +    var clearMessagePane = function () {
   1.197 +        while (logPane.firstChild) {
   1.198 +            logPane.removeChild(logPane.firstChild);
   1.199 +        }
   1.200 +    };
   1.201 +
   1.202 +    /** @id MochiKit.LoggingPane.clearMessages */
   1.203 +    var clearMessages = function () {
   1.204 +        messages = [];
   1.205 +        clearMessagePane();
   1.206 +    };
   1.207 +
   1.208 +    /** @id MochiKit.LoggingPane.closePane */
   1.209 +    var closePane = bind(function () {
   1.210 +        if (this.closed) {
   1.211 +            return;
   1.212 +        }
   1.213 +        this.closed = true;
   1.214 +        if (MochiKit.LoggingPane._loggingPane == this) {
   1.215 +            MochiKit.LoggingPane._loggingPane = null;
   1.216 +        }
   1.217 +        this.logger.removeListener(listenerId);
   1.218 +
   1.219 +        debugPane.loggingPane = null;
   1.220 +
   1.221 +        if (inline) {
   1.222 +            debugPane.parentNode.removeChild(debugPane);
   1.223 +        } else {
   1.224 +            this.win.close();
   1.225 +        }
   1.226 +    }, this);
   1.227 +
   1.228 +    /** @id MochiKit.LoggingPane.filterMessages */
   1.229 +    var filterMessages = function () {
   1.230 +        clearMessagePane();
   1.231 +
   1.232 +        for (var i = 0; i < messages.length; i++) {
   1.233 +            var msg = messages[i];
   1.234 +            if (messageFilter === null || messageFilter(msg)) {
   1.235 +                addMessageText(msg);
   1.236 +            }
   1.237 +        }
   1.238 +    };
   1.239 +
   1.240 +    this.buildAndApplyFilter = function () {
   1.241 +        messageFilter = buildMessageFilter();
   1.242 +
   1.243 +        filterMessages();
   1.244 +
   1.245 +        this.logger.removeListener(listenerId);
   1.246 +        this.logger.addListener(listenerId, messageFilter, addMessage);
   1.247 +    };
   1.248 +
   1.249 +
   1.250 +    /** @id MochiKit.LoggingPane.loadMessages */
   1.251 +    var loadMessages = bind(function () {
   1.252 +        messages = this.logger.getMessages();
   1.253 +        filterMessages();
   1.254 +    }, this);
   1.255 +
   1.256 +    /** @id MochiKit.LoggingPane.filterOnEnter */
   1.257 +    var filterOnEnter = bind(function (event) {
   1.258 +        event = event || window.event;
   1.259 +        key = event.which || event.keyCode;
   1.260 +        if (key == 13) {
   1.261 +            this.buildAndApplyFilter();
   1.262 +        }
   1.263 +    }, this);
   1.264 +
   1.265 +    /* Create the debug pane */
   1.266 +    var style = "display: block; z-index: 1000; left: 0px; bottom: 0px; position: fixed; width: 100%; background-color: white; font: " + this.logFont;
   1.267 +    if (inline) {
   1.268 +        style += "; height: 10em; border-top: 2px solid black";
   1.269 +    } else {
   1.270 +        style += "; height: 100%;";
   1.271 +    }
   1.272 +    debugPane.style.cssText = style;
   1.273 +
   1.274 +    if (!existing_pane) {
   1.275 +        doc.body.appendChild(debugPane);
   1.276 +    }
   1.277 +
   1.278 +    /* Create the filter fields */
   1.279 +    style = {"cssText": "width: 33%; display: inline; font: " + this.logFont};
   1.280 +
   1.281 +    updatetree(levelFilterField, {
   1.282 +        "value": "FATAL|ERROR|WARNING|INFO|DEBUG",
   1.283 +        "onkeypress": filterOnEnter,
   1.284 +        "style": style
   1.285 +    });
   1.286 +    debugPane.appendChild(levelFilterField);
   1.287 +
   1.288 +    updatetree(infoFilterField, {
   1.289 +        "value": ".*",
   1.290 +        "onkeypress": filterOnEnter,
   1.291 +        "style": style
   1.292 +    });
   1.293 +    debugPane.appendChild(infoFilterField);
   1.294 +
   1.295 +    /* Create the buttons */
   1.296 +    style = "width: 8%; display:inline; font: " + this.logFont;
   1.297 +
   1.298 +    filterButton.appendChild(doc.createTextNode("Filter"));
   1.299 +    filterButton.onclick = bind("buildAndApplyFilter", this);
   1.300 +    filterButton.style.cssText = style;
   1.301 +    debugPane.appendChild(filterButton);
   1.302 +
   1.303 +    loadButton.appendChild(doc.createTextNode("Load"));
   1.304 +    loadButton.onclick = loadMessages;
   1.305 +    loadButton.style.cssText = style;
   1.306 +    debugPane.appendChild(loadButton);
   1.307 +
   1.308 +    clearButton.appendChild(doc.createTextNode("Clear"));
   1.309 +    clearButton.onclick = clearMessages;
   1.310 +    clearButton.style.cssText = style;
   1.311 +    debugPane.appendChild(clearButton);
   1.312 +
   1.313 +    closeButton.appendChild(doc.createTextNode("Close"));
   1.314 +    closeButton.onclick = closePane;
   1.315 +    closeButton.style.cssText = style;
   1.316 +    debugPane.appendChild(closeButton);
   1.317 +
   1.318 +    /* Create the logging pane */
   1.319 +    logPaneArea.style.cssText = "overflow: auto; width: 100%";
   1.320 +    logPane.style.cssText = "width: 100%; height: " + (inline ? "8em" : "100%");
   1.321 +
   1.322 +    logPaneArea.appendChild(logPane);
   1.323 +    debugPane.appendChild(logPaneArea);
   1.324 +
   1.325 +    this.buildAndApplyFilter();
   1.326 +    loadMessages();
   1.327 +
   1.328 +    if (inline) {
   1.329 +        this.win = undefined;
   1.330 +    } else {
   1.331 +        this.win = win;
   1.332 +    }
   1.333 +    this.inline = inline;
   1.334 +    this.closePane = closePane;
   1.335 +    this.closed = false;
   1.336 +
   1.337 +    return this;
   1.338 +};
   1.339 +
   1.340 +MochiKit.LoggingPane.LoggingPane.prototype = {
   1.341 +    "logFont": "8pt Verdana,sans-serif",
   1.342 +    "colorTable": {
   1.343 +        "ERROR": "red",
   1.344 +        "FATAL": "darkred",
   1.345 +        "WARNING": "blue",
   1.346 +        "INFO": "black",
   1.347 +        "DEBUG": "green"
   1.348 +    }
   1.349 +};
   1.350 +
   1.351 +
   1.352 +MochiKit.LoggingPane.EXPORT_OK = [
   1.353 +    "LoggingPane"
   1.354 +];
   1.355 +
   1.356 +MochiKit.LoggingPane.EXPORT = [
   1.357 +    "createLoggingPane"
   1.358 +];
   1.359 +
   1.360 +MochiKit.LoggingPane.__new__ = function () {
   1.361 +    this.EXPORT_TAGS = {
   1.362 +        ":common": this.EXPORT,
   1.363 +        ":all": MochiKit.Base.concat(this.EXPORT, this.EXPORT_OK)
   1.364 +    };
   1.365 +    
   1.366 +    MochiKit.Base.nameFunctions(this);
   1.367 +
   1.368 +    MochiKit.LoggingPane._loggingPane = null;
   1.369 +  
   1.370 +};
   1.371 +
   1.372 +MochiKit.LoggingPane.__new__();
   1.373 +
   1.374 +MochiKit.Base._exportSymbols(this, MochiKit.LoggingPane);

mercurial