PageRenderTime 53ms CodeModel.GetById 22ms RepoModel.GetById 17ms app.codeStats 0ms

/testing/selenium-core/scripts/selenium-logging.js

http://datanucleus-appengine.googlecode.com/
JavaScript | 148 lines | 104 code | 19 blank | 25 comment | 15 complexity | cf7ad2c9c359888443ec4fdac470f4b0 MD5 | raw file
Possible License(s): Apache-2.0
  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. var Logger = function() {
  17. this.logWindow = null;
  18. }
  19. Logger.prototype = {
  20. logLevels: {
  21. debug: 0,
  22. info: 1,
  23. warn: 2,
  24. error: 3,
  25. off: 999
  26. },
  27. pendingMessages: new Array(),
  28. threshold: "info",
  29. setLogLevelThreshold: function(logLevel) {
  30. this.threshold = logLevel;
  31. var logWindow = this.getLogWindow()
  32. if (logWindow && logWindow.setThresholdLevel) {
  33. logWindow.setThresholdLevel(logLevel);
  34. }
  35. // NOTE: log messages will be discarded until the log window is
  36. // fully loaded.
  37. },
  38. getLogWindow: function() {
  39. if (this.logWindow && this.logWindow.closed) {
  40. this.logWindow = null;
  41. }
  42. return this.logWindow;
  43. },
  44. openLogWindow: function() {
  45. this.logWindow = window.open(
  46. getDocumentBase(document) + "SeleniumLog.html?startingThreshold="+this.threshold, "SeleniumLog",
  47. "width=600,height=1000,bottom=0,right=0,status,scrollbars,resizable"
  48. );
  49. this.logWindow.moveTo(window.screenX + 1210, window.screenY + window.outerHeight - 1400);
  50. if (browserVersion.appearsToBeBrokenInitialIE6) {
  51. // I would really prefer for the message to immediately appear in the log window, the instant the user requests that the log window be
  52. // visible. But when I initially coded it this way, thou message simply didn't appear unless I stepped through the code with a debugger.
  53. // So obviously there is some timing issue here which I don't have the patience to figure out.
  54. 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.");
  55. this.pendingMessages.push(pendingMessage);
  56. }
  57. return this.logWindow;
  58. },
  59. show: function() {
  60. if (! this.getLogWindow()) {
  61. this.openLogWindow();
  62. }
  63. setTimeout(function(){LOG.error("Log window displayed. Logging events will now be recorded to this window.");}, 500);
  64. },
  65. logHook: function(logLevel, message) {
  66. },
  67. log: function(logLevel, message) {
  68. if (this.logLevels[logLevel] < this.logLevels[this.threshold]) {
  69. return;
  70. }
  71. this.logHook(logLevel, message);
  72. var logWindow = this.getLogWindow();
  73. if (logWindow) {
  74. if (logWindow.append) {
  75. if (logWindow.disabled) {
  76. logWindow.callBack = fnBind(this.setLogLevelThreshold, this);
  77. logWindow.enableButtons();
  78. }
  79. if (this.pendingMessages.length > 0) {
  80. logWindow.append("info("+(new Date().getTime())+"): Appending missed logging messages", "info");
  81. while (this.pendingMessages.length > 0) {
  82. var msg = this.pendingMessages.shift();
  83. logWindow.append(msg.type + "("+msg.timestamp+"): " + msg.msg, msg.type);
  84. }
  85. logWindow.append("info("+(new Date().getTime())+"): Done appending missed logging messages", "info");
  86. }
  87. logWindow.append(logLevel + "("+(new Date().getTime())+"): " + message, logLevel);
  88. }
  89. } else {
  90. // TODO these logging messages are never flushed, which creates
  91. // an enormous array of strings that never stops growing.
  92. // there should at least be a way to clear the messages!
  93. this.pendingMessages.push(new LogMessage(logLevel, message));
  94. }
  95. },
  96. close: function(message) {
  97. if (this.logWindow != null) {
  98. try {
  99. this.logWindow.close();
  100. } catch (e) {
  101. // swallow exception
  102. // the window is probably closed if we get an exception here
  103. }
  104. this.logWindow = null;
  105. }
  106. },
  107. debug: function(message) {
  108. this.log("debug", message);
  109. },
  110. info: function(message) {
  111. this.log("info", message);
  112. },
  113. warn: function(message) {
  114. this.log("warn", message);
  115. },
  116. error: function(message) {
  117. this.log("error", message);
  118. },
  119. exception: function(exception) {
  120. this.error("Unexpected Exception: " + extractExceptionMessage(exception));
  121. this.error("Exception details: " + describe(exception, ', '));
  122. }
  123. };
  124. var LOG = new Logger();
  125. var LogMessage = function(type, msg) {
  126. this.type = type;
  127. this.msg = msg;
  128. this.timestamp = (new Date().getTime());
  129. }