Path: blob/trunk/third_party/closure/goog/debug/divconsole.js
4505 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Simple logger that logs a Div Element.8*/910goog.provide('goog.debug.DivConsole');1112goog.require('goog.debug.HtmlFormatter');13goog.require('goog.dom.DomHelper');14goog.require('goog.dom.TagName');15goog.require('goog.dom.safe');16goog.require('goog.html.SafeHtml');17goog.require('goog.html.SafeStyleSheet');18goog.require('goog.log');19goog.require('goog.string.Const');20goog.require('goog.style');21goog.requireType('goog.debug.Formatter');22goog.requireType('goog.log.LogRecord');232425/**26* A class for visualising logger calls in a div element.27* @param {Element} element The element to append to.28* @constructor29*/30goog.debug.DivConsole = function(element) {31'use strict';32this.publishHandler_ = goog.bind(this.addLogRecord, this);33this.formatter_ = new goog.debug.HtmlFormatter();34this.formatter_.showAbsoluteTime = false;35this.isCapturing_ = false;36this.element_ = element;37this.elementOwnerDocument_ =38this.element_.ownerDocument || this.element_.document;39this.domHelper_ = new goog.dom.DomHelper(this.elementOwnerDocument_);4041this.installStyles();42};434445/**46* Installs styles for the log messages and its div47*/48goog.debug.DivConsole.prototype.installStyles = function() {49'use strict';50goog.style.installSafeStyleSheet(51goog.html.SafeStyleSheet.fromConstant(goog.string.Const.from(52'.dbg-sev{color:#F00}' +53'.dbg-w{color:#C40}' +54'.dbg-sh{font-weight:bold;color:#000}' +55'.dbg-i{color:#444}' +56'.dbg-f{color:#999}' +57'.dbg-ev{color:#0A0}' +58'.dbg-m{color:#990}' +59'.logmsg{border-bottom:1px solid #CCC;padding:2px}' +60'.logsep{background-color: #8C8;}' +61'.logdiv{border:1px solid #CCC;background-color:#FCFCFC;' +62'font:medium monospace}')),63this.element_);64this.element_.className += ' logdiv';65};666768/**69* Sets whether we are currently capturing logger output.70* @param {boolean} capturing Whether to capture logger output.71*/72goog.debug.DivConsole.prototype.setCapturing = function(capturing) {73'use strict';74if (capturing == this.isCapturing_) {75return;76}7778// attach or detach handler from the root logger79var rootLogger = goog.log.getRootLogger();80if (capturing) {81goog.log.addHandler(rootLogger, this.publishHandler_);82} else {83goog.log.removeHandler(rootLogger, this.publishHandler_);84}85this.isCapturing_ = capturing;86};878889/**90* Adds a log record.91* @param {?goog.log.LogRecord} logRecord The log entry.92*/93goog.debug.DivConsole.prototype.addLogRecord = function(logRecord) {94'use strict';95if (!logRecord) {96return;97}98var scroll = this.element_.scrollHeight - this.element_.scrollTop -99this.element_.clientHeight <=100100;101102var div = this.domHelper_.createElement(goog.dom.TagName.DIV);103div.className = 'logmsg';104goog.dom.safe.setInnerHtml(105div, this.formatter_.formatRecordAsHtml(logRecord));106this.element_.appendChild(div);107108if (scroll) {109this.element_.scrollTop = this.element_.scrollHeight;110}111};112113114/**115* Gets the formatter for outputting to the console. The default formatter116* is an instance of goog.debug.HtmlFormatter117* @return {!goog.debug.Formatter} The formatter in use.118*/119goog.debug.DivConsole.prototype.getFormatter = function() {120'use strict';121return this.formatter_;122};123124125/**126* Sets the formatter for outputting to the console.127* @param {goog.debug.HtmlFormatter} formatter The formatter to use.128*/129goog.debug.DivConsole.prototype.setFormatter = function(formatter) {130'use strict';131this.formatter_ = formatter;132};133134135/**136* Adds a separator to the debug window.137*/138goog.debug.DivConsole.prototype.addSeparator = function() {139'use strict';140var div = this.domHelper_.createElement(goog.dom.TagName.DIV);141div.className = 'logmsg logsep';142this.element_.appendChild(div);143};144145146/**147* Clears the console.148*/149goog.debug.DivConsole.prototype.clear = function() {150'use strict';151if (this.element_) {152goog.dom.safe.setInnerHtml(this.element_, goog.html.SafeHtml.EMPTY);153}154};155156157