Debian Codegraph @

closure-library/closure/goog/debug/divconsole.js

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