3 MochiKit.LoggingPane 1.4
5 See <http://mochikit.com/> for documentation, downloads, license, etc.
7 (c) 2005 Bob Ippolito. All rights Reserved.
11 if (typeof(dojo) != 'undefined') {
12 dojo.provide('MochiKit.LoggingPane');
13 dojo.require('MochiKit.Logging');
14 dojo.require('MochiKit.Base');
17 if (typeof(JSAN) != 'undefined') {
18 JSAN.use("MochiKit.Logging", []);
19 JSAN.use("MochiKit.Base", []);
23 if (typeof(MochiKit.Base) == 'undefined' || typeof(MochiKit.Logging) == 'undefined') {
27 throw "MochiKit.LoggingPane depends on MochiKit.Base and MochiKit.Logging!";
30 if (typeof(MochiKit.LoggingPane) == 'undefined') {
31 MochiKit.LoggingPane = {};
34 MochiKit.LoggingPane.NAME = "MochiKit.LoggingPane";
35 MochiKit.LoggingPane.VERSION = "1.4";
36 MochiKit.LoggingPane.__repr__ = function () {
37 return "[" + this.NAME + " " + this.VERSION + "]";
40 MochiKit.LoggingPane.toString = function () {
41 return this.__repr__();
44 /** @id MochiKit.LoggingPane.createLoggingPane */
45 MochiKit.LoggingPane.createLoggingPane = function (inline/* = false */) {
46 var m = MochiKit.LoggingPane;
48 if (m._loggingPane && m._loggingPane.inline != inline) {
49 m._loggingPane.closePane();
50 m._loggingPane = null;
52 if (!m._loggingPane || m._loggingPane.closed) {
53 m._loggingPane = new m.LoggingPane(inline, MochiKit.Logging.logger);
55 return m._loggingPane;
58 /** @id MochiKit.LoggingPane.LoggingPane */
59 MochiKit.LoggingPane.LoggingPane = function (inline/* = false */, logger/* = MochiKit.Logging.logger */) {
61 /* Use a div if inline, pop up a window if not */
62 /* Create the elements */
63 if (typeof(logger) == "undefined" || logger === null) {
64 logger = MochiKit.Logging.logger;
67 var update = MochiKit.Base.update;
68 var updatetree = MochiKit.Base.updatetree;
69 var bind = MochiKit.Base.bind;
70 var clone = MochiKit.Base.clone;
72 var uid = "_MochiKit_LoggingPane";
73 if (typeof(MochiKit.DOM) != "undefined") {
74 win = MochiKit.DOM.currentWindow();
77 // name the popup with the base URL for uniqueness
78 var url = win.location.href.split("?")[0].replace(/[#:\/.><&-]/g, "_");
79 var name = uid + "_" + url;
80 var nwin = win.open("", name, "dependent,resizable,height=200");
82 alert("Not able to open debugging window due to pop-up blocking.");
86 '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" '
87 + '"http://www.w3.org/TR/html4/loose.dtd">'
88 + '<html><head><title>[MochiKit.LoggingPane]</title></head>'
89 + '<body></body></html>'
91 nwin.document.close();
92 nwin.document.title += ' ' + win.document.title;
95 var doc = win.document;
98 // Connect to the debug pane if it already exists (i.e. in a window orphaned by the page being refreshed)
99 var debugPane = doc.getElementById(uid);
100 var existing_pane = !!debugPane;
101 if (debugPane && typeof(debugPane.loggingPane) != "undefined") {
102 debugPane.loggingPane.logger = this.logger;
103 debugPane.loggingPane.buildAndApplyFilter();
104 return debugPane.loggingPane;
108 // clear any existing contents
110 while ((child = debugPane.firstChild)) {
111 debugPane.removeChild(child);
114 debugPane = doc.createElement("div");
117 debugPane.loggingPane = this;
118 var levelFilterField = doc.createElement("input");
119 var infoFilterField = doc.createElement("input");
120 var filterButton = doc.createElement("button");
121 var loadButton = doc.createElement("button");
122 var clearButton = doc.createElement("button");
123 var closeButton = doc.createElement("button");
124 var logPaneArea = doc.createElement("div");
125 var logPane = doc.createElement("div");
127 /* Set up the functions */
128 var listenerId = uid + "_Listener";
129 this.colorTable = clone(this.colorTable);
131 var messageFilter = null;
133 /** @id MochiKit.LoggingPane.messageLevel */
134 var messageLevel = function (msg) {
135 var level = msg.level;
136 if (typeof(level) == "number") {
137 level = MochiKit.Logging.LogLevel[level];
142 /** @id MochiKit.LoggingPane.messageText */
143 var messageText = function (msg) {
144 return msg.info.join(" ");
147 /** @id MochiKit.LoggingPane.addMessageText */
148 var addMessageText = bind(function (msg) {
149 var level = messageLevel(msg);
150 var text = messageText(msg);
151 var c = this.colorTable[level];
152 var p = doc.createElement("span");
153 p.className = "MochiKit-LogMessage MochiKit-LogLevel-" + level;
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;
155 p.appendChild(doc.createTextNode(level + ": " + text));
156 logPane.appendChild(p);
157 logPane.appendChild(doc.createElement("br"));
158 if (logPaneArea.offsetHeight > logPaneArea.scrollHeight) {
159 logPaneArea.scrollTop = 0;
161 logPaneArea.scrollTop = logPaneArea.scrollHeight;
165 /** @id MochiKit.LoggingPane.addMessage */
166 var addMessage = function (msg) {
167 messages[messages.length] = msg;
171 /** @id MochiKit.LoggingPane.buildMessageFilter */
172 var buildMessageFilter = function () {
175 /* Catch any exceptions that might arise due to invalid regexes */
176 levelre = new RegExp(levelFilterField.value);
177 infore = new RegExp(infoFilterField.value);
179 /* If there was an error with the regexes, do no filtering */
180 logDebug("Error in filter regex: " + e.message);
184 return function (msg) {
186 levelre.test(messageLevel(msg)) &&
187 infore.test(messageText(msg))
192 /** @id MochiKit.LoggingPane.clearMessagePane */
193 var clearMessagePane = function () {
194 while (logPane.firstChild) {
195 logPane.removeChild(logPane.firstChild);
199 /** @id MochiKit.LoggingPane.clearMessages */
200 var clearMessages = function () {
205 /** @id MochiKit.LoggingPane.closePane */
206 var closePane = bind(function () {
211 if (MochiKit.LoggingPane._loggingPane == this) {
212 MochiKit.LoggingPane._loggingPane = null;
214 this.logger.removeListener(listenerId);
216 debugPane.loggingPane = null;
219 debugPane.parentNode.removeChild(debugPane);
225 /** @id MochiKit.LoggingPane.filterMessages */
226 var filterMessages = function () {
229 for (var i = 0; i < messages.length; i++) {
230 var msg = messages[i];
231 if (messageFilter === null || messageFilter(msg)) {
237 this.buildAndApplyFilter = function () {
238 messageFilter = buildMessageFilter();
242 this.logger.removeListener(listenerId);
243 this.logger.addListener(listenerId, messageFilter, addMessage);
247 /** @id MochiKit.LoggingPane.loadMessages */
248 var loadMessages = bind(function () {
249 messages = this.logger.getMessages();
253 /** @id MochiKit.LoggingPane.filterOnEnter */
254 var filterOnEnter = bind(function (event) {
255 event = event || window.event;
256 key = event.which || event.keyCode;
258 this.buildAndApplyFilter();
262 /* Create the debug pane */
263 var style = "display: block; z-index: 1000; left: 0px; bottom: 0px; position: fixed; width: 100%; background-color: white; font: " + this.logFont;
265 style += "; height: 10em; border-top: 2px solid black";
267 style += "; height: 100%;";
269 debugPane.style.cssText = style;
271 if (!existing_pane) {
272 doc.body.appendChild(debugPane);
275 /* Create the filter fields */
276 style = {"cssText": "width: 33%; display: inline; font: " + this.logFont};
278 updatetree(levelFilterField, {
279 "value": "FATAL|ERROR|WARNING|INFO|DEBUG",
280 "onkeypress": filterOnEnter,
283 debugPane.appendChild(levelFilterField);
285 updatetree(infoFilterField, {
287 "onkeypress": filterOnEnter,
290 debugPane.appendChild(infoFilterField);
292 /* Create the buttons */
293 style = "width: 8%; display:inline; font: " + this.logFont;
295 filterButton.appendChild(doc.createTextNode("Filter"));
296 filterButton.onclick = bind("buildAndApplyFilter", this);
297 filterButton.style.cssText = style;
298 debugPane.appendChild(filterButton);
300 loadButton.appendChild(doc.createTextNode("Load"));
301 loadButton.onclick = loadMessages;
302 loadButton.style.cssText = style;
303 debugPane.appendChild(loadButton);
305 clearButton.appendChild(doc.createTextNode("Clear"));
306 clearButton.onclick = clearMessages;
307 clearButton.style.cssText = style;
308 debugPane.appendChild(clearButton);
310 closeButton.appendChild(doc.createTextNode("Close"));
311 closeButton.onclick = closePane;
312 closeButton.style.cssText = style;
313 debugPane.appendChild(closeButton);
315 /* Create the logging pane */
316 logPaneArea.style.cssText = "overflow: auto; width: 100%";
317 logPane.style.cssText = "width: 100%; height: " + (inline ? "8em" : "100%");
319 logPaneArea.appendChild(logPane);
320 debugPane.appendChild(logPaneArea);
322 this.buildAndApplyFilter();
326 this.win = undefined;
330 this.inline = inline;
331 this.closePane = closePane;
337 MochiKit.LoggingPane.LoggingPane.prototype = {
338 "logFont": "8pt Verdana,sans-serif",
349 MochiKit.LoggingPane.EXPORT_OK = [
353 MochiKit.LoggingPane.EXPORT = [
357 MochiKit.LoggingPane.__new__ = function () {
359 ":common": this.EXPORT,
360 ":all": MochiKit.Base.concat(this.EXPORT, this.EXPORT_OK)
363 MochiKit.Base.nameFunctions(this);
365 MochiKit.LoggingPane._loggingPane = null;
369 MochiKit.LoggingPane.__new__();
371 MochiKit.Base._exportSymbols(this, MochiKit.LoggingPane);