forked from sfrancisx/log4js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.js
217 lines (187 loc) · 8.5 KB
/
console.js
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
Copyright (c) 2012, Yahoo! Inc. All rights reserved.
Redistribution and use of this software in source and binary forms,
with or without modification, are permitted provided that the following
conditions are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.
* Neither the name of Yahoo! Inc. nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission of Yahoo! Inc.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* An appender that displays log messages in a pop-up window.
* @module comms-log4js-appenders-console
*/
YUI.add('comms-log4js-appenders-console', function(Y)
{
Y.comms.log4js.register("console",
function(logger, show, name)
{
var w, doc, output, filterTimeout, timestamps, filter,
regex = /.*/,
firstIdx = 0,
/**
* This class implements an appender that will display log messages in a
* pop-up window. The appender can be created and attached without opening
* the window, if desired. The window can later be opened by calling show().
* @class consoleAppender
*/
a =
{
/**
* Used by the logger to notify this appender of a new log entry.
* @public
* @method notify
* @param {Object} entry A new log entry
*/
notify: function(entry)
{
if (output)
output.value += format(entry, timestamps.checked);
},
/**
* Open the pop-up window
* @public
* @method show
*/
show: function()
{
w = window.open('', name, 'width=650,height=700,resizable');
doc = w.document;
doc.body.innerHTML =
'<div id="toolbar" style="border-bottom:1px solid #aca899;background-color:#f1efe7;height:22px;font-size:90%">' +
'<button id="clear" style="float:right;margin-right:3px;font-size:95%;font-family:inherit">Clear</button>' +
'<input id="filter" type="text" title="Type to Filter" style="font-size:95%;float:right;margin-right:20px;font-family:tahoma,verdana"></input>' +
'<input id="timestamps" type="checkbox" style="font-size:95%"></input><label for="timestamps">Timestamps</label>' +
'</div>' +
'<div style="position:fixed;width:100%;top:22px;bottom:0;margin:0">'+
'<textarea id="output" readonly="readonly" style="resize:none;padding:0;width:100%;height:100%"></textarea>'+
'</div>';
doc.body.style.cssText="padding:0;margin:0;font-size:77%;font-family:tahoma,verdana";
output = doc.getElementById("output");
doc.getElementById("clear").onclick = clear;
timestamps = doc.getElementById("timestamps");
timestamps.onclick = refresh;
filter = doc.getElementById("filter");
filter.value = '.*';
filter.onkeyup = changeFilter;
refresh();
},
/**
* Redraw all log entries in the window.
* @public
* @method refresh
*/
refresh: function()
{
if (output)
{
var entry,
out = [ ],
i = 0,
entries = logger.getEntries(),
checked = timestamps.checked;
if (logger.header && regex.test(logger.header))
out.push(logger.header);
while (entry = entries[i++])
{
if (entry.id > firstIdx && regex.test(entry.msg))
out.push(format(entry, checked));
}
output.value = out.join("");
}
}
},
refresh = a.refresh;
logger.show = a.show;
show && a.show();
return a;
/**
* Clear the window. All this does is change the first index to display to be
* the next log entry to be received. It's not implemented, but un-clearing is
* possible.
* @private
* @method clear
*/
function clear()
{
var entries = logger.getEntries();
firstIdx = entries.length && entries[entries.length-1].id;
refresh();
}
/**
* The filter has been changed. Wait a short time and then redraw the window.
* The wait is to avoid redrawing on every keystroke when the user is typing
* a new filter.
* @private
* @method changeFilter
*/
function changeFilter()
{
if (filterTimeout)
clearTimeout(filterTimeout);
filterTimeout = setTimeout(updateFilter, 200);
}
/**
* The filter has changed and the short wait in changeFilter() has expired.
* Redraw the window using the new filter.
* @private
* @method updateFilter
*/
function updateFilter()
{
filterTimeout = 0;
// Do this in a try/catch because the user may type in an invalid regex
try
{
regex = new RegExp(filter.value, 'i');
}
catch (e) { }
refresh();
}
/**
* Format a single log entry for display.
* @private
* @method format
* @param entry {Object} The log entry to format
* @param ts {Boolean} Whether or not to include the timestamp (if present)
* @return {String} The log message, ready for display.
*/
function format(entry, ts)
{
var msg = "";
if (entry.id > firstIdx && regex.test(entry.msg))
{
ts = ts && entry.ts;
if (ts)
{
msg = ts.toTimeString().split(" ")[0];
ts = "" + ts.getMilliseconds();
while (ts.length < 3)
ts = "0" + ts;
msg = "[" + msg + "." + ts + "] ";
}
msg = entry.id + ". " + msg + entry.msg + "\n";
}
return msg;
}
});
}, '1.0.0', { requires: [ 'comms-log4js' ] });