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