Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /*** |
michael@0 | 2 | |
michael@0 | 3 | MochiKit.LoggingPane 1.4 |
michael@0 | 4 | |
michael@0 | 5 | See <http://mochikit.com/> for documentation, downloads, license, etc. |
michael@0 | 6 | |
michael@0 | 7 | (c) 2005 Bob Ippolito. All rights Reserved. |
michael@0 | 8 | |
michael@0 | 9 | ***/ |
michael@0 | 10 | |
michael@0 | 11 | if (typeof(dojo) != 'undefined') { |
michael@0 | 12 | dojo.provide('MochiKit.LoggingPane'); |
michael@0 | 13 | dojo.require('MochiKit.Logging'); |
michael@0 | 14 | dojo.require('MochiKit.Base'); |
michael@0 | 15 | } |
michael@0 | 16 | |
michael@0 | 17 | if (typeof(JSAN) != 'undefined') { |
michael@0 | 18 | JSAN.use("MochiKit.Logging", []); |
michael@0 | 19 | JSAN.use("MochiKit.Base", []); |
michael@0 | 20 | } |
michael@0 | 21 | |
michael@0 | 22 | try { |
michael@0 | 23 | if (typeof(MochiKit.Base) == 'undefined' || typeof(MochiKit.Logging) == 'undefined') { |
michael@0 | 24 | throw ""; |
michael@0 | 25 | } |
michael@0 | 26 | } catch (e) { |
michael@0 | 27 | throw "MochiKit.LoggingPane depends on MochiKit.Base and MochiKit.Logging!"; |
michael@0 | 28 | } |
michael@0 | 29 | |
michael@0 | 30 | if (typeof(MochiKit.LoggingPane) == 'undefined') { |
michael@0 | 31 | MochiKit.LoggingPane = {}; |
michael@0 | 32 | } |
michael@0 | 33 | |
michael@0 | 34 | MochiKit.LoggingPane.NAME = "MochiKit.LoggingPane"; |
michael@0 | 35 | MochiKit.LoggingPane.VERSION = "1.4"; |
michael@0 | 36 | MochiKit.LoggingPane.__repr__ = function () { |
michael@0 | 37 | return "[" + this.NAME + " " + this.VERSION + "]"; |
michael@0 | 38 | }; |
michael@0 | 39 | |
michael@0 | 40 | MochiKit.LoggingPane.toString = function () { |
michael@0 | 41 | return this.__repr__(); |
michael@0 | 42 | }; |
michael@0 | 43 | |
michael@0 | 44 | /** @id MochiKit.LoggingPane.createLoggingPane */ |
michael@0 | 45 | MochiKit.LoggingPane.createLoggingPane = function (inline/* = false */) { |
michael@0 | 46 | var m = MochiKit.LoggingPane; |
michael@0 | 47 | inline = !(!inline); |
michael@0 | 48 | if (m._loggingPane && m._loggingPane.inline != inline) { |
michael@0 | 49 | m._loggingPane.closePane(); |
michael@0 | 50 | m._loggingPane = null; |
michael@0 | 51 | } |
michael@0 | 52 | if (!m._loggingPane || m._loggingPane.closed) { |
michael@0 | 53 | m._loggingPane = new m.LoggingPane(inline, MochiKit.Logging.logger); |
michael@0 | 54 | } |
michael@0 | 55 | return m._loggingPane; |
michael@0 | 56 | }; |
michael@0 | 57 | |
michael@0 | 58 | /** @id MochiKit.LoggingPane.LoggingPane */ |
michael@0 | 59 | MochiKit.LoggingPane.LoggingPane = function (inline/* = false */, logger/* = MochiKit.Logging.logger */) { |
michael@0 | 60 | |
michael@0 | 61 | /* Use a div if inline, pop up a window if not */ |
michael@0 | 62 | /* Create the elements */ |
michael@0 | 63 | if (typeof(logger) == "undefined" || logger === null) { |
michael@0 | 64 | logger = MochiKit.Logging.logger; |
michael@0 | 65 | } |
michael@0 | 66 | this.logger = logger; |
michael@0 | 67 | var update = MochiKit.Base.update; |
michael@0 | 68 | var updatetree = MochiKit.Base.updatetree; |
michael@0 | 69 | var bind = MochiKit.Base.bind; |
michael@0 | 70 | var clone = MochiKit.Base.clone; |
michael@0 | 71 | var win = window; |
michael@0 | 72 | var uid = "_MochiKit_LoggingPane"; |
michael@0 | 73 | if (typeof(MochiKit.DOM) != "undefined") { |
michael@0 | 74 | win = MochiKit.DOM.currentWindow(); |
michael@0 | 75 | } |
michael@0 | 76 | if (!inline) { |
michael@0 | 77 | // name the popup with the base URL for uniqueness |
michael@0 | 78 | var url = win.location.href.split("?")[0].replace(/[#:\/.><&-]/g, "_"); |
michael@0 | 79 | var name = uid + "_" + url; |
michael@0 | 80 | var nwin = win.open("", name, "dependent,resizable,height=200"); |
michael@0 | 81 | if (!nwin) { |
michael@0 | 82 | alert("Not able to open debugging window due to pop-up blocking."); |
michael@0 | 83 | return undefined; |
michael@0 | 84 | } |
michael@0 | 85 | nwin.document.write( |
michael@0 | 86 | '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" ' |
michael@0 | 87 | + '"http://www.w3.org/TR/html4/loose.dtd">' |
michael@0 | 88 | + '<html><head><title>[MochiKit.LoggingPane]</title></head>' |
michael@0 | 89 | + '<body></body></html>' |
michael@0 | 90 | ); |
michael@0 | 91 | nwin.document.close(); |
michael@0 | 92 | nwin.document.title += ' ' + win.document.title; |
michael@0 | 93 | win = nwin; |
michael@0 | 94 | } |
michael@0 | 95 | var doc = win.document; |
michael@0 | 96 | this.doc = doc; |
michael@0 | 97 | |
michael@0 | 98 | // Connect to the debug pane if it already exists (i.e. in a window orphaned by the page being refreshed) |
michael@0 | 99 | var debugPane = doc.getElementById(uid); |
michael@0 | 100 | var existing_pane = !!debugPane; |
michael@0 | 101 | if (debugPane && typeof(debugPane.loggingPane) != "undefined") { |
michael@0 | 102 | debugPane.loggingPane.logger = this.logger; |
michael@0 | 103 | debugPane.loggingPane.buildAndApplyFilter(); |
michael@0 | 104 | return debugPane.loggingPane; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | if (existing_pane) { |
michael@0 | 108 | // clear any existing contents |
michael@0 | 109 | var child; |
michael@0 | 110 | while ((child = debugPane.firstChild)) { |
michael@0 | 111 | debugPane.removeChild(child); |
michael@0 | 112 | } |
michael@0 | 113 | } else { |
michael@0 | 114 | debugPane = doc.createElement("div"); |
michael@0 | 115 | debugPane.id = uid; |
michael@0 | 116 | } |
michael@0 | 117 | debugPane.loggingPane = this; |
michael@0 | 118 | var levelFilterField = doc.createElement("input"); |
michael@0 | 119 | var infoFilterField = doc.createElement("input"); |
michael@0 | 120 | var filterButton = doc.createElement("button"); |
michael@0 | 121 | var loadButton = doc.createElement("button"); |
michael@0 | 122 | var clearButton = doc.createElement("button"); |
michael@0 | 123 | var closeButton = doc.createElement("button"); |
michael@0 | 124 | var logPaneArea = doc.createElement("div"); |
michael@0 | 125 | var logPane = doc.createElement("div"); |
michael@0 | 126 | |
michael@0 | 127 | /* Set up the functions */ |
michael@0 | 128 | var listenerId = uid + "_Listener"; |
michael@0 | 129 | this.colorTable = clone(this.colorTable); |
michael@0 | 130 | var messages = []; |
michael@0 | 131 | var messageFilter = null; |
michael@0 | 132 | |
michael@0 | 133 | /** @id MochiKit.LoggingPane.messageLevel */ |
michael@0 | 134 | var messageLevel = function (msg) { |
michael@0 | 135 | var level = msg.level; |
michael@0 | 136 | if (typeof(level) == "number") { |
michael@0 | 137 | level = MochiKit.Logging.LogLevel[level]; |
michael@0 | 138 | } |
michael@0 | 139 | return level; |
michael@0 | 140 | }; |
michael@0 | 141 | |
michael@0 | 142 | /** @id MochiKit.LoggingPane.messageText */ |
michael@0 | 143 | var messageText = function (msg) { |
michael@0 | 144 | return msg.info.join(" "); |
michael@0 | 145 | }; |
michael@0 | 146 | |
michael@0 | 147 | /** @id MochiKit.LoggingPane.addMessageText */ |
michael@0 | 148 | var addMessageText = bind(function (msg) { |
michael@0 | 149 | var level = messageLevel(msg); |
michael@0 | 150 | var text = messageText(msg); |
michael@0 | 151 | var c = this.colorTable[level]; |
michael@0 | 152 | var p = doc.createElement("span"); |
michael@0 | 153 | p.className = "MochiKit-LogMessage MochiKit-LogLevel-" + level; |
michael@0 | 154 | 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; |
michael@0 | 155 | p.appendChild(doc.createTextNode(level + ": " + text)); |
michael@0 | 156 | logPane.appendChild(p); |
michael@0 | 157 | logPane.appendChild(doc.createElement("br")); |
michael@0 | 158 | if (logPaneArea.offsetHeight > logPaneArea.scrollHeight) { |
michael@0 | 159 | logPaneArea.scrollTop = 0; |
michael@0 | 160 | } else { |
michael@0 | 161 | logPaneArea.scrollTop = logPaneArea.scrollHeight; |
michael@0 | 162 | } |
michael@0 | 163 | }, this); |
michael@0 | 164 | |
michael@0 | 165 | /** @id MochiKit.LoggingPane.addMessage */ |
michael@0 | 166 | var addMessage = function (msg) { |
michael@0 | 167 | messages[messages.length] = msg; |
michael@0 | 168 | addMessageText(msg); |
michael@0 | 169 | }; |
michael@0 | 170 | |
michael@0 | 171 | /** @id MochiKit.LoggingPane.buildMessageFilter */ |
michael@0 | 172 | var buildMessageFilter = function () { |
michael@0 | 173 | var levelre, infore; |
michael@0 | 174 | try { |
michael@0 | 175 | /* Catch any exceptions that might arise due to invalid regexes */ |
michael@0 | 176 | levelre = new RegExp(levelFilterField.value); |
michael@0 | 177 | infore = new RegExp(infoFilterField.value); |
michael@0 | 178 | } catch(e) { |
michael@0 | 179 | /* If there was an error with the regexes, do no filtering */ |
michael@0 | 180 | logDebug("Error in filter regex: " + e.message); |
michael@0 | 181 | return null; |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | return function (msg) { |
michael@0 | 185 | return ( |
michael@0 | 186 | levelre.test(messageLevel(msg)) && |
michael@0 | 187 | infore.test(messageText(msg)) |
michael@0 | 188 | ); |
michael@0 | 189 | }; |
michael@0 | 190 | }; |
michael@0 | 191 | |
michael@0 | 192 | /** @id MochiKit.LoggingPane.clearMessagePane */ |
michael@0 | 193 | var clearMessagePane = function () { |
michael@0 | 194 | while (logPane.firstChild) { |
michael@0 | 195 | logPane.removeChild(logPane.firstChild); |
michael@0 | 196 | } |
michael@0 | 197 | }; |
michael@0 | 198 | |
michael@0 | 199 | /** @id MochiKit.LoggingPane.clearMessages */ |
michael@0 | 200 | var clearMessages = function () { |
michael@0 | 201 | messages = []; |
michael@0 | 202 | clearMessagePane(); |
michael@0 | 203 | }; |
michael@0 | 204 | |
michael@0 | 205 | /** @id MochiKit.LoggingPane.closePane */ |
michael@0 | 206 | var closePane = bind(function () { |
michael@0 | 207 | if (this.closed) { |
michael@0 | 208 | return; |
michael@0 | 209 | } |
michael@0 | 210 | this.closed = true; |
michael@0 | 211 | if (MochiKit.LoggingPane._loggingPane == this) { |
michael@0 | 212 | MochiKit.LoggingPane._loggingPane = null; |
michael@0 | 213 | } |
michael@0 | 214 | this.logger.removeListener(listenerId); |
michael@0 | 215 | |
michael@0 | 216 | debugPane.loggingPane = null; |
michael@0 | 217 | |
michael@0 | 218 | if (inline) { |
michael@0 | 219 | debugPane.parentNode.removeChild(debugPane); |
michael@0 | 220 | } else { |
michael@0 | 221 | this.win.close(); |
michael@0 | 222 | } |
michael@0 | 223 | }, this); |
michael@0 | 224 | |
michael@0 | 225 | /** @id MochiKit.LoggingPane.filterMessages */ |
michael@0 | 226 | var filterMessages = function () { |
michael@0 | 227 | clearMessagePane(); |
michael@0 | 228 | |
michael@0 | 229 | for (var i = 0; i < messages.length; i++) { |
michael@0 | 230 | var msg = messages[i]; |
michael@0 | 231 | if (messageFilter === null || messageFilter(msg)) { |
michael@0 | 232 | addMessageText(msg); |
michael@0 | 233 | } |
michael@0 | 234 | } |
michael@0 | 235 | }; |
michael@0 | 236 | |
michael@0 | 237 | this.buildAndApplyFilter = function () { |
michael@0 | 238 | messageFilter = buildMessageFilter(); |
michael@0 | 239 | |
michael@0 | 240 | filterMessages(); |
michael@0 | 241 | |
michael@0 | 242 | this.logger.removeListener(listenerId); |
michael@0 | 243 | this.logger.addListener(listenerId, messageFilter, addMessage); |
michael@0 | 244 | }; |
michael@0 | 245 | |
michael@0 | 246 | |
michael@0 | 247 | /** @id MochiKit.LoggingPane.loadMessages */ |
michael@0 | 248 | var loadMessages = bind(function () { |
michael@0 | 249 | messages = this.logger.getMessages(); |
michael@0 | 250 | filterMessages(); |
michael@0 | 251 | }, this); |
michael@0 | 252 | |
michael@0 | 253 | /** @id MochiKit.LoggingPane.filterOnEnter */ |
michael@0 | 254 | var filterOnEnter = bind(function (event) { |
michael@0 | 255 | event = event || window.event; |
michael@0 | 256 | key = event.which || event.keyCode; |
michael@0 | 257 | if (key == 13) { |
michael@0 | 258 | this.buildAndApplyFilter(); |
michael@0 | 259 | } |
michael@0 | 260 | }, this); |
michael@0 | 261 | |
michael@0 | 262 | /* Create the debug pane */ |
michael@0 | 263 | var style = "display: block; z-index: 1000; left: 0px; bottom: 0px; position: fixed; width: 100%; background-color: white; font: " + this.logFont; |
michael@0 | 264 | if (inline) { |
michael@0 | 265 | style += "; height: 10em; border-top: 2px solid black"; |
michael@0 | 266 | } else { |
michael@0 | 267 | style += "; height: 100%;"; |
michael@0 | 268 | } |
michael@0 | 269 | debugPane.style.cssText = style; |
michael@0 | 270 | |
michael@0 | 271 | if (!existing_pane) { |
michael@0 | 272 | doc.body.appendChild(debugPane); |
michael@0 | 273 | } |
michael@0 | 274 | |
michael@0 | 275 | /* Create the filter fields */ |
michael@0 | 276 | style = {"cssText": "width: 33%; display: inline; font: " + this.logFont}; |
michael@0 | 277 | |
michael@0 | 278 | updatetree(levelFilterField, { |
michael@0 | 279 | "value": "FATAL|ERROR|WARNING|INFO|DEBUG", |
michael@0 | 280 | "onkeypress": filterOnEnter, |
michael@0 | 281 | "style": style |
michael@0 | 282 | }); |
michael@0 | 283 | debugPane.appendChild(levelFilterField); |
michael@0 | 284 | |
michael@0 | 285 | updatetree(infoFilterField, { |
michael@0 | 286 | "value": ".*", |
michael@0 | 287 | "onkeypress": filterOnEnter, |
michael@0 | 288 | "style": style |
michael@0 | 289 | }); |
michael@0 | 290 | debugPane.appendChild(infoFilterField); |
michael@0 | 291 | |
michael@0 | 292 | /* Create the buttons */ |
michael@0 | 293 | style = "width: 8%; display:inline; font: " + this.logFont; |
michael@0 | 294 | |
michael@0 | 295 | filterButton.appendChild(doc.createTextNode("Filter")); |
michael@0 | 296 | filterButton.onclick = bind("buildAndApplyFilter", this); |
michael@0 | 297 | filterButton.style.cssText = style; |
michael@0 | 298 | debugPane.appendChild(filterButton); |
michael@0 | 299 | |
michael@0 | 300 | loadButton.appendChild(doc.createTextNode("Load")); |
michael@0 | 301 | loadButton.onclick = loadMessages; |
michael@0 | 302 | loadButton.style.cssText = style; |
michael@0 | 303 | debugPane.appendChild(loadButton); |
michael@0 | 304 | |
michael@0 | 305 | clearButton.appendChild(doc.createTextNode("Clear")); |
michael@0 | 306 | clearButton.onclick = clearMessages; |
michael@0 | 307 | clearButton.style.cssText = style; |
michael@0 | 308 | debugPane.appendChild(clearButton); |
michael@0 | 309 | |
michael@0 | 310 | closeButton.appendChild(doc.createTextNode("Close")); |
michael@0 | 311 | closeButton.onclick = closePane; |
michael@0 | 312 | closeButton.style.cssText = style; |
michael@0 | 313 | debugPane.appendChild(closeButton); |
michael@0 | 314 | |
michael@0 | 315 | /* Create the logging pane */ |
michael@0 | 316 | logPaneArea.style.cssText = "overflow: auto; width: 100%"; |
michael@0 | 317 | logPane.style.cssText = "width: 100%; height: " + (inline ? "8em" : "100%"); |
michael@0 | 318 | |
michael@0 | 319 | logPaneArea.appendChild(logPane); |
michael@0 | 320 | debugPane.appendChild(logPaneArea); |
michael@0 | 321 | |
michael@0 | 322 | this.buildAndApplyFilter(); |
michael@0 | 323 | loadMessages(); |
michael@0 | 324 | |
michael@0 | 325 | if (inline) { |
michael@0 | 326 | this.win = undefined; |
michael@0 | 327 | } else { |
michael@0 | 328 | this.win = win; |
michael@0 | 329 | } |
michael@0 | 330 | this.inline = inline; |
michael@0 | 331 | this.closePane = closePane; |
michael@0 | 332 | this.closed = false; |
michael@0 | 333 | |
michael@0 | 334 | return this; |
michael@0 | 335 | }; |
michael@0 | 336 | |
michael@0 | 337 | MochiKit.LoggingPane.LoggingPane.prototype = { |
michael@0 | 338 | "logFont": "8pt Verdana,sans-serif", |
michael@0 | 339 | "colorTable": { |
michael@0 | 340 | "ERROR": "red", |
michael@0 | 341 | "FATAL": "darkred", |
michael@0 | 342 | "WARNING": "blue", |
michael@0 | 343 | "INFO": "black", |
michael@0 | 344 | "DEBUG": "green" |
michael@0 | 345 | } |
michael@0 | 346 | }; |
michael@0 | 347 | |
michael@0 | 348 | |
michael@0 | 349 | MochiKit.LoggingPane.EXPORT_OK = [ |
michael@0 | 350 | "LoggingPane" |
michael@0 | 351 | ]; |
michael@0 | 352 | |
michael@0 | 353 | MochiKit.LoggingPane.EXPORT = [ |
michael@0 | 354 | "createLoggingPane" |
michael@0 | 355 | ]; |
michael@0 | 356 | |
michael@0 | 357 | MochiKit.LoggingPane.__new__ = function () { |
michael@0 | 358 | this.EXPORT_TAGS = { |
michael@0 | 359 | ":common": this.EXPORT, |
michael@0 | 360 | ":all": MochiKit.Base.concat(this.EXPORT, this.EXPORT_OK) |
michael@0 | 361 | }; |
michael@0 | 362 | |
michael@0 | 363 | MochiKit.Base.nameFunctions(this); |
michael@0 | 364 | |
michael@0 | 365 | MochiKit.LoggingPane._loggingPane = null; |
michael@0 | 366 | |
michael@0 | 367 | }; |
michael@0 | 368 | |
michael@0 | 369 | MochiKit.LoggingPane.__new__(); |
michael@0 | 370 | |
michael@0 | 371 | MochiKit.Base._exportSymbols(this, MochiKit.LoggingPane); |