Path: blob/trunk/third_party/closure/goog/debug/divconsole.js
2868 views
// Copyright 2006 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Simple logger that logs a Div Element.16*17*/1819goog.provide('goog.debug.DivConsole');2021goog.require('goog.debug.HtmlFormatter');22goog.require('goog.debug.LogManager');23goog.require('goog.dom.DomHelper');24goog.require('goog.dom.TagName');25goog.require('goog.dom.safe');26goog.require('goog.html.SafeHtml');27goog.require('goog.html.SafeStyleSheet');28goog.require('goog.string.Const');29goog.require('goog.style');303132/**33* A class for visualising logger calls in a div element.34* @param {Element} element The element to append to.35* @constructor36*/37goog.debug.DivConsole = function(element) {38this.publishHandler_ = goog.bind(this.addLogRecord, this);39this.formatter_ = new goog.debug.HtmlFormatter();40this.formatter_.showAbsoluteTime = false;41this.isCapturing_ = false;42this.element_ = element;43this.elementOwnerDocument_ =44this.element_.ownerDocument || this.element_.document;45this.domHelper_ = new goog.dom.DomHelper(this.elementOwnerDocument_);4647this.installStyles();48};495051/**52* Installs styles for the log messages and its div53*/54goog.debug.DivConsole.prototype.installStyles = function() {55goog.style.installSafeStyleSheet(56goog.html.SafeStyleSheet.fromConstant(goog.string.Const.from(57'.dbg-sev{color:#F00}' +58'.dbg-w{color:#C40}' +59'.dbg-sh{font-weight:bold;color:#000}' +60'.dbg-i{color:#444}' +61'.dbg-f{color:#999}' +62'.dbg-ev{color:#0A0}' +63'.dbg-m{color:#990}' +64'.logmsg{border-bottom:1px solid #CCC;padding:2px}' +65'.logsep{background-color: #8C8;}' +66'.logdiv{border:1px solid #CCC;background-color:#FCFCFC;' +67'font:medium monospace}')),68this.element_);69this.element_.className += ' logdiv';70};717273/**74* Sets whether we are currently capturing logger output.75* @param {boolean} capturing Whether to capture logger output.76*/77goog.debug.DivConsole.prototype.setCapturing = function(capturing) {78if (capturing == this.isCapturing_) {79return;80}8182// attach or detach handler from the root logger83var rootLogger = goog.debug.LogManager.getRoot();84if (capturing) {85rootLogger.addHandler(this.publishHandler_);86} else {87rootLogger.removeHandler(this.publishHandler_);88this.logBuffer = '';89}90this.isCapturing_ = capturing;91};929394/**95* Adds a log record.96* @param {goog.debug.LogRecord} logRecord The log entry.97*/98goog.debug.DivConsole.prototype.addLogRecord = function(logRecord) {99if (!logRecord) {100return;101}102var scroll = this.element_.scrollHeight - this.element_.scrollTop -103this.element_.clientHeight <=104100;105106var div = this.domHelper_.createElement(goog.dom.TagName.DIV);107div.className = 'logmsg';108goog.dom.safe.setInnerHtml(109div, this.formatter_.formatRecordAsHtml(logRecord));110this.element_.appendChild(div);111112if (scroll) {113this.element_.scrollTop = this.element_.scrollHeight;114}115};116117118/**119* Gets the formatter for outputting to the console. The default formatter120* is an instance of goog.debug.HtmlFormatter121* @return {!goog.debug.Formatter} The formatter in use.122*/123goog.debug.DivConsole.prototype.getFormatter = function() {124return this.formatter_;125};126127128/**129* Sets the formatter for outputting to the console.130* @param {goog.debug.HtmlFormatter} formatter The formatter to use.131*/132goog.debug.DivConsole.prototype.setFormatter = function(formatter) {133this.formatter_ = formatter;134};135136137/**138* Adds a separator to the debug window.139*/140goog.debug.DivConsole.prototype.addSeparator = function() {141var div = this.domHelper_.createElement(goog.dom.TagName.DIV);142div.className = 'logmsg logsep';143this.element_.appendChild(div);144};145146147/**148* Clears the console.149*/150goog.debug.DivConsole.prototype.clear = function() {151if (this.element_) {152goog.dom.safe.setInnerHtml(this.element_, goog.html.SafeHtml.EMPTY);153}154};155156157