-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtraceconsole.js
More file actions
624 lines (536 loc) · 24.5 KB
/
traceconsole.js
File metadata and controls
624 lines (536 loc) · 24.5 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/*
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.
*/
/**
* A custom log4js appender specifically for tracer output.
* @module comms-log4js-appenders-traceconsole
*/
YUI.add('comms-log4js-appenders-traceconsole', function(Y)
{
Y.comms.log4js.register("traceconsole",
function(logger, show, name)
{
var w, doc, output, timer, verbose, showStack, tedious, status, paused, lastFrame, root, frames,
// fnCount = { }, // See the comment below about fnCount
/**
* A custom log4js appender for displaying tracer output. This appender
* is incompatible with 'normal' logging. Normal appender expect
* entry.msg to be a string. This appender expects it to be an object
* describing a stack frame.
* @class traceconsole
*/
a =
{
/**
* The logger's interface to this appender. This method provides
* notification of a new log entry.
* @method notify
* @param entry {object|string} The log entry. If entry.msg is
* an object, it describes a stack
* frame. If it's a string, it's an
* annotation in the trace log.
*/
notify: function(entry)
{
if (paused)
return;
var frame = entry.msg;
frame.idx = frames.push(frame) - 1;
if (typeof entry.msg != "object")
{
root.children[root.children.length-1].anno = entry.msg;
lastFrame.anno = entry.msg;
return;
}
// This counts the number of times a particular function gets traced. If tracing
// makes the app unbearably slow, uncomment these lines (& the declaration above),
// let the tracer run a while, and then break here. Open a console window and
// execute something like:
// for (name in fnCount) { if (fnCount[name] > 500) console.log(name + ": " + fnCount[name]); }
// to see the most commonly traced methods. Add those methods to the weaver's
// blacklist.
//if (!frame.stack)
// fnCount[frame.n || frame.name] = (fnCount[frame.n || frame.name] || 0) + 1;
while (lastFrame.depth > frame.depth - 1)
lastFrame = lastFrame.parent;
lastFrame.children = lastFrame.children || [ ];
lastFrame.children.push(frame);
frame.parent = lastFrame;
lastFrame = frame;
if (frame.depth == 1)
{
if (timer)
clearTimeout(timer);
timer = setTimeout(expand, 100);
}
if ((frames.length - 1) % 500 == 0)
{
console.log("updating status to " + (frames.length -1));
status.innerHTML = (frames.length - 1) + " methods traced.";
}
},
/**
* Show the tracer's output window.
* @method show
*/
show: function()
{
w = window.open('', name, 'width=650,height=700,resizable');
doc = w.document;
doc.open();
doc.write(
'<!DOCTYPE html>' +
'<html>' +
'<head>' +
'<title>Yahoo! Mail Tracer</title>' +
'<style>' +
'.fixed > DIV { position: fixed }' +
'#toolbar { border-bottom:1px solid #aca899;background-color:#f1efe7;height:22px;font-size:90%;left:0;right:0 }' +
'.check { margin-right:3px;font-size:95%;font-family:inherit }' +
'.output { position:fixed;width:100%;top:22px;bottom:22px;margin:0;overflow:auto }' +
'#frame_0 { overflow:auto;display:inline-block }' +
'.footer { position:fixed;width:100%;bottom:0px;height:22px;border-top:1px solid #aca899;background-color:#f1efe7;}' +
'#tip { position:fixed;bottom:0px;height:19px;left:3px }' +
'#status { float:right;margin:3px }' +
'.hidden > DIV { display: none }' +
'.expandable > .expander { background: url("http://mail.yimg.com/ok/u/assets/sprite/default/16x16/launch-ltr.png") no-repeat scroll 0 -360px transparent; }' +
'.expander { width: 14px;height:11px;display:inline-block; } ' +
'.expandable.expanded > .expander { background-position: 0 -390px; }' +
'.expandable > .expandee { margin-left:5px;display:none }' +
'.expandable.expanded > .expandee { display:block }' +
'.toolbutton { font-size:90%; margin:2px; float:right; }' +
'</style>' +
'</head>' +
'<body class="fixed" style="padding:0;margin:0;font-size:77%;font-family:tahoma,verdana">' +
'<div id="toolbar">' +
'<input id="showStack" type="checkbox" class="check"></input><label for="showStack">Include Stack</label>' +
'<input id="verbose" type="checkbox" class="check"></input><label for="verbose">Verbose</label>' +
'<input id="tedious" type="checkbox" class="check"></input><label for="tedious">Tedious</label>' +
'<label for="pause" style="float:right;margin:3px">Pause</label><input id="pause" type="checkbox" style="float:right" class="check"></input>' +
'<button id="clear" class="toolbutton">Clear</button>' +
'</div>' +
'<div class="output">'+
'<div id="frame_0"></div>' +
'</div>' +
'<div class="footer">'+
'<div id="tip"></div>' +
'<div id="status"></div>'+
'</div>' +
'</body>' +
'</html>');
doc.close();
output = doc.getElementById("frame_0");
status = doc.getElementById("status");
output.onclick = expandHandler;
output.ondblclick = expandAllHandler;
doc.getElementById("showStack").onclick = refresh;
doc.getElementById("verbose").onclick = update;
doc.getElementById("tedious").onclick = update;
doc.getElementById("pause").onclick = pause;
doc.getElementById("clear").onclick = clearAndRefresh;
},
/**
* Redraw the output. This method is public, but should
* only be used externally by the logger when the
* traceconsole is first created. Internally, it's used when
* the 'show stack' option changes.
* @method refresh
*/
refresh: function()
{
showStack = doc.getElementById("showStack").checked;
verbose = doc.getElementById("verbose").checked;
tedious = doc.getElementById("tedious").checked;
if (output)
{
output.innerHTML = '';
for (var i = 1; i < frames.length; i++)
{
if (frames[i])
frames[i].created = 0;
}
expand();
}
}
},
refresh = a.refresh;
clear();
logger.show = a.show;
show && a.show();
return a;
/**
* Pause tracing.
* @method pause
* @private
*/
function pause()
{
paused = doc.getElementById("pause").checked;
Y.comms.aop && (Y.comms.aop.paused = paused);
}
/**
* Reset the trace console's data
* @method clear
* @private
*/
function clear()
{
root = lastFrame = { depth: 0, idx: 0, created: 1 };
frames = [ root ];
}
/**
* Clear the trace console
* @method clearAndRefresh
* @private
*/
function clearAndRefresh()
{
clear();
refresh();
}
/**
* Update the output. Used when the verbose/tedious state changes.
* @method update
* @private
*/
function update()
{
verbose = doc.getElementById("verbose").checked;
tedious = doc.getElementById("tedious").checked;
var frame, el, inner, index,
i = 1;
for (; i < frames.length; i++)
{
if (frame = frames[i])
{
el = doc.getElementById("frame_" + i);
if (el)
{
inner = el.innerHTML;
index = inner.indexOf('<div id=');
if (index == -1)
inner = '';
else
inner = inner.substr(index);
el.innerHTML = '<div class="expander"></div>' + formatFrame(frame) + inner;
}
}
}
}
/**
* Event handler to expand a single item in the call stack
* @method expandHandler
* @private
* @param evt
*/
function expandHandler(evt)
{
if (!showStack)
return;
evt = evt || w.event;
var id, frame, el,
target = evt.target || evt.srcElement;
setTimeout(doExpansion, 200);
/**
* Find the frame to be expanded and call expand() on it.
* @method doExpansion
* @private
*/
function doExpansion()
{
while (target && !target.id)
target = target.parentNode;
if (target)
{
id = target.id.split("_")[1];
frame = frames[id];
el = doc.getElementById("frame_" + frame.idx);
expand(0, frame);
}
}
}
/**
* Event handler to expand all children of an item in the call stack.
* @method expandAllHandler
* @private
*/
function expandAllHandler(evt)
{
if (!showStack)
return;
evt = evt || w.event;
var id,
target = evt.target || evt.srcElement;
while (!target.id)
target = target.parentNode;
id = target.id.split("_")[1];
expandAll(frames[id]);
}
/**
* Expand an entry in the call stack, or simply display an entry if not in stack mode. DOM elements are
* created on-demand, so expansion might require creating all the DOM elements.
* @method expand
* @private
* @param parentFrame {object} The stack frame to expand
* @param expandOnly {boolean} Truthy to expand a node, falsey to toggle a node's expansion state
*/
function expand(dummy, parentFrame, expandOnly)
{
var child, el, ex,
i = 0,
innerHTML = '';
parentFrame = parentFrame || root;
if (!parentFrame.children)
return;
el = doc.getElementById("frame_" + parentFrame.idx);
if (showStack)
{
if (hasClass(el, "expanded"))
{
if (!expandOnly)
removeClass(el, "expanded");
}
else
addClass(el, "expanded");
timer = 0;
while (child = parentFrame.children[i++])
{
if (!child.created)
{
child.created = 1;
ex = '';
if (child.children && child.children.length)
ex = ' expandable';
innerHTML += '<div id="frame_' + child.idx + '" class="expandee' + ex + '"><div class="expander"></div>' + formatFrame(child) + '</div>';
}
}
}
else
{
for (; i < frames.length; i++)
{
child = frames[i];
if (child && !child.created && !child.stack)
{
child.created = 1;
innerHTML += '<div id="frame_' + child.idx + '">' + formatFrame(child) + '</div>';
}
}
}
if (innerHTML)
el.innerHTML += innerHTML;
status.innerHTML = (frames.length - 1) + " methods traced.";
}
/**
* Expand all children of a stack frame.
* @method expandAll
* @private
* @param frame {object} The frame to expand.
*/
function expandAll(frame)
{
var child,
i = 0;
if (frame.children)
{
expand(0, frame, 1);
while (child = frame.children[i++])
expandAll(child);
}
}
/**
* Get the HTML for a stack frame
* @method formatFrame
* @private
* @param frame {object} The frame to be formatted
* @return {string} The HTML
*/
function formatFrame(frame)
{
var arg, objText, value, m, mName, type,
i = 0,
args = frame.args,
argText = [ ],
name = "<strong>" + frame.name + "</strong>";
if (frame.stack)
name = "<em>" + frame.name + "</em>";
name = frame.idx + " " + name;
while (args && (i < args.length))
{
arg = args[i++];
type = typeof arg;
switch (type)
{
case 'number':
case 'boolean':
argText.push(arg);
break;
case 'string':
if (!tedious && arg.length > 50)
arg = arg.substr(0,47) + "...";
argText.push("'" + arg + "'");
break;
case 'function':
argText.push("function " + getFunctionName(arg) + "()");
break;
case 'object':
try
{
if ((tedious || verbose) && arg)
{
objText = [ ];
value = '';
for (mName in arg)
{
m = arg[mName];
switch (typeof m)
{
case 'number':
case 'boolean':
value = m;
break;
case 'string':
if (!tedious && m.length > 50)
m = m.substr(0,47) + "...";
value = "'" + m + "'";
break;
default:
if (m)
value = "{" + typeof m + "}";
else
value = "" + m;
}
objText.push(mName + ":" + value);
}
objText = objText.join(", ");
if (!tedious && (objText.length > 50))
objText = objText.substr(0, 47) + "...";
argText.push("{" + objText + "}");
break;
}
}
catch (e) { }
arg && argText.push("{" + type + "}");
arg || argText.push("" + arg);
}
}
argText = "(" + argText.join(", ").replace(/</g, "<") + ")";
name = name + argText;
if (frame.anno)
name = name + " ***** " + frame.anno + " *****";
return name;
}
/**
* Try to get a function's name from the Function object.
* @method getFunctionName
* @private
* @param fn {function} The function whose name should be returned.
* @return {string} The name of the function, or "{?}" if it can't be found.
*/
function getFunctionName(fn)
{
if (fn.name)
return fn.name;
var name, fnStr, match;
try
{
fnStr = fn.toString();
match = fnStr.match(/function\s+([^\s(]+)/);
if (match && match.length > 1)
name = match[1];
}
catch (e) { }
name = name || '{?}';
name = name.replace(/^(\s+)?(.*\S)(\s+)?$/g, '$2');
return name;
}
/**
* Add one or more classes to a DOM element
* @method addClass
* @private
* @param el {HTMLElement} The DOM element
* @param class {string} The class to be added. Repeatable.
*/
function addClass(el /*, class, ...*/)
{
var classes,
a = arguments,
i = a.length,
doc = document;
if (typeof el == "string")
el = doc.getElementById(el);
classes = el.className.split(" ");
while (--i)
{
if (!hasClass(el, arguments[i]))
classes.push(arguments[i]);
}
el.className = classes.join(" ");
}
/**
* Remove a class from a DOM element
* @method removeClass
* @private
* @param el {HTMLElement} The DOM element
* @param className The class to remove
*/
function removeClass(el, className)
{
if (typeof el == "string")
el = doc.getElementById(el);
var i = 0,
classes = el.className.split(" ");
while (i < classes.length)
{
if (classes[i] == className)
classes.splice(i, 1);
else
i++;
}
el.className = classes.join(" ");
}
/**
* Check to see if an element has a given class
* @method hasClass
* @private
* @param el {HTMLElement} The DOM element to check
* @param className {string} The class to check for
* @return {boolean} Truthy if the class is present, falsey otherwise.
*/
function hasClass(el, className)
{
if (!el.className)
return;
var i = 0,
classes = el.className.split(" ");
while (i < classes.length)
if (classes[i++] == className)
return 1;
}
});
}, '1.0.0', { requires: [ 'comms-log4js' ] });