/testing/selenium-core/scripts/selenium-logging.js
JavaScript | 148 lines | 104 code | 19 blank | 25 comment | 15 complexity | cf7ad2c9c359888443ec4fdac470f4b0 MD5 | raw file
1/* 2 * Copyright 2004 ThoughtWorks, Inc 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17var Logger = function() { 18 this.logWindow = null; 19} 20Logger.prototype = { 21 22 logLevels: { 23 debug: 0, 24 info: 1, 25 warn: 2, 26 error: 3, 27 off: 999 28 }, 29 30 pendingMessages: new Array(), 31 32 threshold: "info", 33 34 setLogLevelThreshold: function(logLevel) { 35 this.threshold = logLevel; 36 var logWindow = this.getLogWindow() 37 if (logWindow && logWindow.setThresholdLevel) { 38 logWindow.setThresholdLevel(logLevel); 39 } 40 // NOTE: log messages will be discarded until the log window is 41 // fully loaded. 42 }, 43 44 getLogWindow: function() { 45 if (this.logWindow && this.logWindow.closed) { 46 this.logWindow = null; 47 } 48 return this.logWindow; 49 }, 50 51 openLogWindow: function() { 52 this.logWindow = window.open( 53 getDocumentBase(document) + "SeleniumLog.html?startingThreshold="+this.threshold, "SeleniumLog", 54 "width=600,height=1000,bottom=0,right=0,status,scrollbars,resizable" 55 ); 56 this.logWindow.moveTo(window.screenX + 1210, window.screenY + window.outerHeight - 1400); 57 if (browserVersion.appearsToBeBrokenInitialIE6) { 58 // I would really prefer for the message to immediately appear in the log window, the instant the user requests that the log window be 59 // visible. But when I initially coded it this way, thou message simply didn't appear unless I stepped through the code with a debugger. 60 // So obviously there is some timing issue here which I don't have the patience to figure out. 61 var pendingMessage = new LogMessage("warn", "You appear to be running an unpatched IE 6, which is not stable and can crash due to memory problems. We recommend you run Windows update to install a more stable version of IE."); 62 this.pendingMessages.push(pendingMessage); 63 } 64 return this.logWindow; 65 }, 66 67 show: function() { 68 if (! this.getLogWindow()) { 69 this.openLogWindow(); 70 } 71 setTimeout(function(){LOG.error("Log window displayed. Logging events will now be recorded to this window.");}, 500); 72 }, 73 74 logHook: function(logLevel, message) { 75 }, 76 77 log: function(logLevel, message) { 78 if (this.logLevels[logLevel] < this.logLevels[this.threshold]) { 79 return; 80 } 81 this.logHook(logLevel, message); 82 var logWindow = this.getLogWindow(); 83 if (logWindow) { 84 if (logWindow.append) { 85 if (logWindow.disabled) { 86 logWindow.callBack = fnBind(this.setLogLevelThreshold, this); 87 logWindow.enableButtons(); 88 } 89 if (this.pendingMessages.length > 0) { 90 logWindow.append("info("+(new Date().getTime())+"): Appending missed logging messages", "info"); 91 while (this.pendingMessages.length > 0) { 92 var msg = this.pendingMessages.shift(); 93 logWindow.append(msg.type + "("+msg.timestamp+"): " + msg.msg, msg.type); 94 } 95 logWindow.append("info("+(new Date().getTime())+"): Done appending missed logging messages", "info"); 96 } 97 logWindow.append(logLevel + "("+(new Date().getTime())+"): " + message, logLevel); 98 } 99 } else { 100 // TODO these logging messages are never flushed, which creates 101 // an enormous array of strings that never stops growing. 102 // there should at least be a way to clear the messages! 103 this.pendingMessages.push(new LogMessage(logLevel, message)); 104 } 105 }, 106 107 close: function(message) { 108 if (this.logWindow != null) { 109 try { 110 this.logWindow.close(); 111 } catch (e) { 112 // swallow exception 113 // the window is probably closed if we get an exception here 114 } 115 this.logWindow = null; 116 } 117 }, 118 119 debug: function(message) { 120 this.log("debug", message); 121 }, 122 123 info: function(message) { 124 this.log("info", message); 125 }, 126 127 warn: function(message) { 128 this.log("warn", message); 129 }, 130 131 error: function(message) { 132 this.log("error", message); 133 }, 134 135 exception: function(exception) { 136 this.error("Unexpected Exception: " + extractExceptionMessage(exception)); 137 this.error("Exception details: " + describe(exception, ', ')); 138 } 139 140}; 141 142var LOG = new Logger(); 143 144var LogMessage = function(type, msg) { 145 this.type = type; 146 this.msg = msg; 147 this.timestamp = (new Date().getTime()); 148}