divconsole.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2006 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Simple logger that logs a Div Element.
  16. *
  17. */
  18. goog.provide('goog.debug.DivConsole');
  19. goog.require('goog.debug.HtmlFormatter');
  20. goog.require('goog.debug.LogManager');
  21. goog.require('goog.dom.DomHelper');
  22. goog.require('goog.dom.TagName');
  23. goog.require('goog.dom.safe');
  24. goog.require('goog.html.SafeHtml');
  25. goog.require('goog.html.SafeStyleSheet');
  26. goog.require('goog.string.Const');
  27. goog.require('goog.style');
  28. /**
  29. * A class for visualising logger calls in a div element.
  30. * @param {Element} element The element to append to.
  31. * @constructor
  32. */
  33. goog.debug.DivConsole = function(element) {
  34. this.publishHandler_ = goog.bind(this.addLogRecord, this);
  35. this.formatter_ = new goog.debug.HtmlFormatter();
  36. this.formatter_.showAbsoluteTime = false;
  37. this.isCapturing_ = false;
  38. this.element_ = element;
  39. this.elementOwnerDocument_ =
  40. this.element_.ownerDocument || this.element_.document;
  41. this.domHelper_ = new goog.dom.DomHelper(this.elementOwnerDocument_);
  42. this.installStyles();
  43. };
  44. /**
  45. * Installs styles for the log messages and its div
  46. */
  47. goog.debug.DivConsole.prototype.installStyles = function() {
  48. goog.style.installSafeStyleSheet(
  49. goog.html.SafeStyleSheet.fromConstant(goog.string.Const.from(
  50. '.dbg-sev{color:#F00}' +
  51. '.dbg-w{color:#C40}' +
  52. '.dbg-sh{font-weight:bold;color:#000}' +
  53. '.dbg-i{color:#444}' +
  54. '.dbg-f{color:#999}' +
  55. '.dbg-ev{color:#0A0}' +
  56. '.dbg-m{color:#990}' +
  57. '.logmsg{border-bottom:1px solid #CCC;padding:2px}' +
  58. '.logsep{background-color: #8C8;}' +
  59. '.logdiv{border:1px solid #CCC;background-color:#FCFCFC;' +
  60. 'font:medium monospace}')),
  61. this.element_);
  62. this.element_.className += ' logdiv';
  63. };
  64. /**
  65. * Sets whether we are currently capturing logger output.
  66. * @param {boolean} capturing Whether to capture logger output.
  67. */
  68. goog.debug.DivConsole.prototype.setCapturing = function(capturing) {
  69. if (capturing == this.isCapturing_) {
  70. return;
  71. }
  72. // attach or detach handler from the root logger
  73. var rootLogger = goog.debug.LogManager.getRoot();
  74. if (capturing) {
  75. rootLogger.addHandler(this.publishHandler_);
  76. } else {
  77. rootLogger.removeHandler(this.publishHandler_);
  78. this.logBuffer = '';
  79. }
  80. this.isCapturing_ = capturing;
  81. };
  82. /**
  83. * Adds a log record.
  84. * @param {goog.debug.LogRecord} logRecord The log entry.
  85. */
  86. goog.debug.DivConsole.prototype.addLogRecord = function(logRecord) {
  87. if (!logRecord) {
  88. return;
  89. }
  90. var scroll = this.element_.scrollHeight - this.element_.scrollTop -
  91. this.element_.clientHeight <=
  92. 100;
  93. var div = this.domHelper_.createElement(goog.dom.TagName.DIV);
  94. div.className = 'logmsg';
  95. goog.dom.safe.setInnerHtml(
  96. div, this.formatter_.formatRecordAsHtml(logRecord));
  97. this.element_.appendChild(div);
  98. if (scroll) {
  99. this.element_.scrollTop = this.element_.scrollHeight;
  100. }
  101. };
  102. /**
  103. * Gets the formatter for outputting to the console. The default formatter
  104. * is an instance of goog.debug.HtmlFormatter
  105. * @return {!goog.debug.Formatter} The formatter in use.
  106. */
  107. goog.debug.DivConsole.prototype.getFormatter = function() {
  108. return this.formatter_;
  109. };
  110. /**
  111. * Sets the formatter for outputting to the console.
  112. * @param {goog.debug.HtmlFormatter} formatter The formatter to use.
  113. */
  114. goog.debug.DivConsole.prototype.setFormatter = function(formatter) {
  115. this.formatter_ = formatter;
  116. };
  117. /**
  118. * Adds a separator to the debug window.
  119. */
  120. goog.debug.DivConsole.prototype.addSeparator = function() {
  121. var div = this.domHelper_.createElement(goog.dom.TagName.DIV);
  122. div.className = 'logmsg logsep';
  123. this.element_.appendChild(div);
  124. };
  125. /**
  126. * Clears the console.
  127. */
  128. goog.debug.DivConsole.prototype.clear = function() {
  129. if (this.element_) {
  130. goog.dom.safe.setInnerHtml(this.element_, goog.html.SafeHtml.EMPTY);
  131. }
  132. };