-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.js
474 lines (434 loc) · 19.1 KB
/
Logger.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
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
///////////////////////////////////////////////////////////////////////////////
// Logger.js
// =========
// singleton log class (module pattern)
//
// This class creates a drawable tab at the bottom of web browser. You can slide
// up/down by clicking the tab. You can toggle on/off programmatically by
// calling Logger.toggle(), or Logger.show() and Logger.hide() respectively.
// It can be completely hidden by calling Logger.hide(). To get it back, call
// Logger.show(). It can be also enabled/disabled by Logger.enable()/disable().
// When it is disabled, a message with log() won't be written to logger.
// When you need to purge all previous messages, use Logger.clear().
//
// Use log() utility function to print a message to the log window.
// e.g.: log("Hello") : print Hello
// log(123) : print 123
// log() : print a blank line
// log("hello","css-color") css-color is optional on all examples
//
// History:
// PiccinniGius 1.17 Added foreground color to log
// 1.17: Print array elements for "Array" object.
// Print "object" for Object, and "function" for Function type.
// 1.16: Removed vendor-specific border radius CSS.
// Added left text-align on the logger container.
// 1.15: Fixed height issue for box-sizing CSS.
// 1.14: Added toggle() to draw logger window up/down.
// Added open()/close() to slide up/down.
// Changed show()/hide() for visibility.
// Changed tab size smaller.
// 1.13: Added the class name along with version.
// 1.12: Removed width and added margin-left for msgDiv.
// 1.11: Added clear() function.
// 1.10: Added enable()/disable() functions.
// 1.09: Added z-index attribute on the logger container.
// 1.08: Use requestAnimationFrame() for animations.
// 1.07: Wrap a message if it is longer than the window width.
// 1.06: Print "undefined" or "null" if msg is undefined or null value.
// 1.05: Added time stamp for log() (with no param).
// 1.04: Modified handling undefined type of msg.
// 1.03: Fixed the error when msg is undefined.
// 1.02: Added sliding animation easy in/out using cosine.
// 1.01: Changed "display:none" to visibility:hidden for logDiv.
// Supported IE v8 without transparent background.
// 1.00: First public release.
//
// AUTHOR: Song Ho Ahn ([email protected])
// CREATED: 2011-02-15
// UPDATED: 2018-01-19
//
// Copyright 2011. Song Ho Ahn
// Forked: https://github.com/piccinnigius/ on 2018-11-06
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// utility function to print message with timestamp to log
// e.g.: log("Hello") : print Hello
// log(123) : print 123
// log() : print a blank line
function log(msg,mode)
{
if(arguments.length == 0)
Logger.print("","std"); // print a blank line
else if(arguments.length == 1)
Logger.print(msg, "std");
else
Logger.print(msg, mode)
};
///////////////////////////////////////////////////////////////////////////////
var Logger = (function()
{
"use strict";
///////////////////////////////////////////////////////////////////////////
// private members
///////////////////////////////////////////////////////////////////////////
var version = "PiccinniGius 1.17";
var containerDiv = null;
var tabDiv = null;
var logDiv = null;
var visible = true; // flag for visibility
var opened = false; // flag for toggle on/off
var enabled = true; // does not accept log messages any more if it is false
var logHeight = 215; // 204 + 2*padding + border-top
var tabHeight = 20;
// for animation
var animTime = 0;
var animDuration = 200; // ms
var animFrameTime= 16; // ms
///////////////////////////////////////////////////////////////////////////
// get time and date as string with a trailing space
var getTime = function()
{
var now = new Date();
var hour = "0" + now.getHours();
hour = hour.substring(hour.length-2);
var minute = "0" + now.getMinutes();
minute = minute.substring(minute.length-2);
var second = "0" + now.getSeconds();
second = second.substring(second.length-2);
return hour + ":" + minute + ":" + second;
};
var getDate = function()
{
var now = new Date();
var year = "" + now.getFullYear();
var month = "0" + (now.getMonth()+1);
month = month.substring(month.length-2);
var date = "0" + now.getDate();
date = date.substring(date.length-2);
return year + "-" + month + "-" + date;
};
///////////////////////////////////////////////////////////////////////////
// return available requestAnimationFrame(), otherwise, fallback to setTimeOut
var getRequestAnimationFrameFunction = function()
{
var requestAnimationFrame = window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.webkitRequestAnimationFrame;
if(requestAnimationFrame)
return function(callback){ return requestAnimationFrame(callback); };
else
return function(callback){ return setTimeout(callback, 16); };
};
///////////////////////////////////////////////////////////////////////////
// public members
///////////////////////////////////////////////////////////////////////////
var self =
{
///////////////////////////////////////////////////////////////////////
// create a div for log and attach it to document
init: function()
{
// avoid redundant call
if(containerDiv)
return true;
// check if DOM is ready
if(!document || !document.createElement || !document.body || !document.body.appendChild)
return false;
// constants
var CONTAINER_DIV = "loggerContainer";
var TAB_DIV = "loggerTab";
var LOG_DIV = "logger";
var Z_INDEX = 9999;
// create logger DOM element
containerDiv = document.getElementById(CONTAINER_DIV);
if(!containerDiv)
{
// container
containerDiv = document.createElement("div");
containerDiv.id = CONTAINER_DIV;
containerDiv.setAttribute("style", "width:100%; " +
"margin:0; " +
"padding:0; " +
"text-align:left; " +
"box-sizing:border-box; " +
"position:fixed; " +
"left:0; " +
"z-index:" + Z_INDEX + "; " +
"bottom:" + (-logHeight) + "px; "); /* hide it initially */
// tab
tabDiv = document.createElement("div");
tabDiv.id = TAB_DIV;
tabDiv.appendChild(document.createTextNode("LOG"));
tabDiv.setAttribute("style", "width:40px; " +
"box-sizing:border-box; " +
"overflow:hidden; " +
"font:bold 10px verdana,helvetica,sans-serif; " +
"line-height:" + (tabHeight-1) + "px; " + /* subtract top-border */
"color:#fff; " +
"position:absolute; " +
"left:20px; " +
"top:" + -tabHeight + "px; " +
"margin:0; padding:0; " +
"text-align:center; " +
"border:1px solid #aaa; " +
"border-bottom:none; " +
/*"background:#333; " + */
"background:rgba(0,0,0, 0.8); " +
"border-top-right-radius:8px; " +
"border-top-left-radius:8px; ");
// add mouse event handlers
tabDiv.onmouseover = function()
{
this.style.cursor = "pointer";
this.style.textShadow = "0 0 1px #fff, 0 0 2px #0f0, 0 0 6px #0f0";
};
tabDiv.onmouseout = function()
{
this.style.cursor = "auto";
this.style.textShadow = "none";
};
tabDiv.onclick = function()
{
Logger.toggle();
this.style.textShadow = "none";
};
// log message
logDiv = document.createElement("div");
logDiv.id = LOG_DIV;
logDiv.setAttribute("style", "font:12px monospace; " +
"height: " + logHeight + "px; " +
"box-sizing:border-box; " +
"color:#fff; " +
"overflow-x:hidden; " +
"overflow-y:scroll; " +
"visibility:hidden; " +
"position:relative; " +
"bottom:0px; " +
"margin:0px; " +
"padding:5px; " +
"resize:vertical; " +
/*"background:#333; " + */
"background:rgba(0, 0, 0, 0.85); " +
"border-top:1px solid #aaa; ");
// style for log message
var span = document.createElement("span"); // for coloring text
span.style.color = "#afa";
span.style.fontWeight = "bold";
// the first message in log
var msg = "===== Log Started at " +
getDate() + ", " + getTime() + ", " +
"(Logger version " + version + ") " +
"=====";
span.appendChild(document.createTextNode(msg));
logDiv.appendChild(span);
logDiv.appendChild(document.createElement("br")); // blank line
logDiv.appendChild(document.createElement("br")); // blank line
// add divs to document
containerDiv.appendChild(tabDiv);
containerDiv.appendChild(logDiv);
document.body.appendChild(containerDiv);
}
return true;
},
///////////////////////////////////////////////////////////////////////
// print log message to logDiv
print: function(msg, mode)
{
// ignore message if it is disabled
if(!enabled)
return;
// check if this object is initialized
if(!containerDiv)
{
var ready = this.init();
if(!ready)
return;
}
var msgDefined = true;
// convert non-string type to string
if(typeof msg == "undefined") // print "undefined" if param is not defined
{
msg = "undefined";
msgDefined = false;
}
else if(typeof msg == "function") // print "function" if param is function ptr
{
msg = "function";
msgDefined = false;
}
else if(msg === null) // print "null" if param has null value
{
msg = "null";
msgDefined = false;
}
else
{
if(msg instanceof Array) // print array elements if param is array object
{
msg = this.arrayToString(msg);
}
else if(msg instanceof Object) // print "object" if param is object type
{
msg = "object";
msgDefined = false;
}
else
{
msg += ""; // for other types
}
}
var lines = msg.split(/\r\n|\r|\n/);
for(var i = 0, c = lines.length; i < c; ++i)
{
// format time and put the text node to inline element
var timeDiv = document.createElement("div"); // color for time
timeDiv.setAttribute("style", "color:#999;" +
"float:left;");
var timeNode = document.createTextNode(getTime() + "\u00a0");
timeDiv.appendChild(timeNode);
// create message span
var msgDiv = document.createElement("div");
msgDiv.setAttribute("style", "word-wrap:break-word;" + // wrap msg
"margin-left:6.0em;"); // margin-left = 9 * ?
if(mode != "std")
msgDiv.style.color = mode;
// if(!msgDefined)
// msgDiv.style.color = "#afa"; // override color if msg is not defined
// put message into a text node
var line = lines[i].replace(/ /g, "\u00a0");
var msgNode = document.createTextNode(line);
msgDiv.appendChild(msgNode);
// new line div with clearing css float property
var newLineDiv = document.createElement("div");
newLineDiv.setAttribute("style", "clear:both;");
logDiv.appendChild(timeDiv); // add time
logDiv.appendChild(msgDiv); // add message
logDiv.appendChild(newLineDiv); // add message
logDiv.scrollTop = logDiv.scrollHeight; // scroll to last line
}
},
///////////////////////////////////////////////////////////////////////
// slide log container up and down
toggle: function()
{
if(opened) // if opened, close the window
this.close();
else // if closed, open the window
this.open();
},
open: function()
{
if(!this.init()) return;
if(!visible) return;
if(opened) return;
logDiv.style.visibility = "visible";
animTime = Date.now();
var requestAnimationFrame = getRequestAnimationFrameFunction();
requestAnimationFrame(slideUp);
function slideUp()
{
var duration = Date.now() - animTime;
if(duration >= animDuration)
{
containerDiv.style.bottom = 0;
opened = true;
return;
}
var y = Math.round(-logHeight * (1 - 0.5 * (1 - Math.cos(Math.PI * duration / animDuration))));
containerDiv.style.bottom = "" + y + "px";
requestAnimationFrame(slideUp);
}
},
close: function()
{
if(!this.init()) return;
if(!visible) return;
if(!opened) return;
animTime = Date.now();
var requestAnimationFrame = getRequestAnimationFrameFunction();
requestAnimationFrame(slideDown);
function slideDown()
{
var duration = Date.now() - animTime;
if(duration >= animDuration)
{
containerDiv.style.bottom = "" + -logHeight + "px";
logDiv.style.visibility = "hidden";
opened = false;
return;
}
var y = Math.round(-logHeight * 0.5 * (1 - Math.cos(Math.PI * duration / animDuration)));
containerDiv.style.bottom = "" + y + "px";
requestAnimationFrame(slideDown);
}
},
///////////////////////////////////////////////////////////////////////
// show/hide the logger window and tab
show: function()
{
if(!this.init())
return;
containerDiv.style.display = "block";
visible = true;
},
hide: function()
{
if(!this.init())
return;
containerDiv.style.display = "none";
visible = false;
},
///////////////////////////////////////////////////////////////////////
// when Logger is enabled (default), log() method will write its message
// to the console ("logDiv")
enable: function()
{
if(!this.init())
return;
enabled = true;
tabDiv.style.color = "#fff";
logDiv.style.color = "#fff";
},
///////////////////////////////////////////////////////////////////////
// when it is diabled, subsequent log() calls will be ignored and
// the message won't be written on "logDiv".
// "LOG" tab and log text are grayed out to indicate it is disabled.
disable: function()
{
if(!this.init())
return;
enabled = false;
tabDiv.style.color = "#444";
logDiv.style.color = "#444";
},
///////////////////////////////////////////////////////////////////////
// clear all messages from logDiv
clear: function()
{
if(!this.init())
return;
logDiv.innerHTML = "";
},
///////////////////////////////////////////////////////////////////////
// utility funtions
arrayToString: function(array)
{
var str = "[";
for(var i = 0, c = array.length; i < c; ++i)
{
if(array[i] instanceof Array)
str += this.arrayToString(array[i]);
else
str += array[i];
if(i < c - 1)
str += ", ";
}
str += "]";
return str;
}
};
return self;
})();