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