forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloud.logger.js
337 lines (276 loc) · 8.04 KB
/
cloud.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
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
// Version: 1.9.1.152
//
// Javascript logger utility
// Author
// Kelven Yang
// 2/25/2010
//
function Logger() {
this.bDockEnabled = true;
this.logWin = null;
this.logger = null;
this.header = null;
this.bEnabled = true;
this.level = 0;
this.bMoving = false;
this.offsetStart = {left: 0, top: 0};
this.ptStart = {x: 0, y: 0};
}
Logger.DEFAULT_WIN_HEIGHT = 500;
Logger.LEVEL_TRACE = 0;
Logger.LEVEL_DEBUG = 1;
Logger.LEVEL_INFO = 2;
Logger.LEVEL_WARN = 3;
Logger.LEVEL_ERROR = 4;
Logger.LEVEL_FATAL = 5;
Logger.LEVEL_SYS = 100;
Logger.prototype = {
open: function() {
if(this.logWin) {
this.logWin.show();
this.log(Logger.LEVEL_SYS, "Logger is open in browser: " + this.objectToString($.browser));
return;
}
var logger = this;
var logWinMarkup = [
'<div class="logwin">',
'<div class="logwin_title">',
'<div class="logwin_title_actionbox">',
'<a class="logwin_playbutton" href="#" cmd="1"></a>',
'<a class="logwin_stopbutton" href="#" cmd="2"></a>',
'<a class="logwin_clrbutton" href="#" cmd="3"></a>',
'<form action="#">',
'<select class="select" id="template_type">',
'<option value="0">TRACE</option>',
'<option value="1">DEBUG</option>',
'<option value="2">INFO</option>',
'<option value="3">WARN</option>',
'<option value="4">ERROR</option>',
'<option value="5">FATAL</option>',
'</select>',
'</form>',
'</div>',
'<div class="logwin_title_rgtactionbox">',
'<a class="logwin_minimizebutton" href="#" cmd="4"></a>',
'<a class="logwin_shrinkbutton" href="#" cmd="5"></a>',
'</div>',
'</div>',
'<div class="logwin_content"></div>',
'</div>'
].join('');
this.logWin = $(logWinMarkup).appendTo(document.body);
this.header = $('.logwin_title:first', this.logWin);
this.logger = $('.logwin_content:first', this.logWin);
$(".logwin_title", this.logWin).mousedown(function(e) {
if($(e.target).attr('cmd'))
return true;
if(!logger.bMoving) {
logger.bMoving = true;
logger.offsetStart = logger.logWin.offset();
logger.ptStart = {x: e.pageX, y: e.pageY};
$(document).bind("mousemove", function(e) {
if(logger.bMoving) {
logger.enableDocking(false);
var logWinNewLeft = logger.offsetStart.left + e.pageX - logger.ptStart.x;
var logWinNewTop = logger.offsetStart.top + e.pageY - logger.ptStart.y;
logger.logWin.css("left", logWinNewLeft + "px").css("top", logWinNewTop + "px");
}
return false;
});
$(document).bind("mouseup", function(e) {
if(logger.bMoving) {
logger.bMoving = false;
$(document).unbind("mousemove", arguments.callee.name);
$(document).unbind("mouseup", arguments.callee.name);
return false;
}
return true;
});
}
// prevent default handling
return false;
}).dblclick(function(e) {
logger.expand(!logger.isExpanded());
});
this.logWin.click(function(e) {
if($(e.target).attr('cmd')) {
switch($(e.target).attr('cmd')) {
case '1' :
logger.enable(true);
break;
case '2' :
logger.enable(false);
break;
case '3' :
logger.clear();
break;
case '4' :
logger.enableDocking(true);
logger.dockIn();
break;
case '5' :
logger.expand(!logger.isExpanded());
break;
default :
break;
}
}
});
$("#template_type", this.logWin).change(function(e) {
logger.setLevel(parseInt($(this).val()));
});
this.logWin.css("left", (($(document.body).width() - this.logWin.width()) / 2) + "px");
this.dockIn();
this.log(Logger.LEVEL_SYS, "Logger is open in browser: " + this.objectToString($.browser));
},
close: function() {
if(this.logWin)
this.logWin.hide();
},
isOpen: function() {
if(this.logWin)
return this.logWin.is(":visible");
return false;
},
dockIn: function() {
var logger = this;
var offset = this.logWin.offset();
var bottom = offset.top + this.logWin.height();
var delta = bottom - 2;
this.logWin.animate({top: (offset.top - delta) + "px"}, 200,
function() {
logger.logWin.unbind("mouseleave");
logger.logWin.bind("mouseenter", function(e) {
if(logger.bDockEnabled)
logger.dockOut();
});
}
);
},
dockOut: function() {
var logger = this;
this.logWin.animate({top: "0px"}, 200,
function() {
logger.logWin.unbind("mouseenter");
logger.logWin.bind("mouseleave", function(e) {
if(logger.bDockEnabled) {
var xPosInLogWin = e.pageX - logger.logWin.offset().left;
var yPosInLogWin = e.pageY - logger.logWin.offset().top;
if(xPosInLogWin < 0 || yPosInLogWin < 0 ||
xPosInLogWin > logger.logWin.width() || yPosInLogWin > logger.logWin.height()) {
logger.dockIn();
}
}
});
}
);
},
enableDocking: function(bEnable) {
this.bDockEnabled = bEnable;
},
log: function(level, message) {
// Note : LEVEL_SYS message will always be logged
if(this.logger && (level == Logger.LEVEL_SYS || this.bEnabled && level >= this.level)) {
var curTime = new Date();
var curTimeString = [
'', curTime.getMonth(),
'/', curTime.getDate(),
'/', curTime.getYear(),
' ',
curTime.getHours(),
':', curTime.getMinutes(),
":", curTime.getSeconds(),
".", curTime.getMilliseconds()].join('');
this.logger.append(this.getLevelDisplayString(level) + " - " + curTimeString + " - " + message + '<br>');
}
},
clear: function() {
if(this.logger) {
this.logger.empty();
this.log(Logger.LEVEL_SYS, "Logger is cleared");
}
},
setLevel: function(level) {
this.level = level;
this.log(Logger.LEVEL_SYS, "Set logger trace level to " + this.getLevelDisplayString(level));
},
enable: function(bEnabled) {
this.bEnabled = bEnabled;
if(bEnabled)
this.log(Logger.LEVEL_SYS, "Logger is enabled");
else
this.log(Logger.LEVEL_SYS, "Logger is disabled");
},
expand: function(bExpand) {
if(bExpand) {
this.logWin.height(Logger.DEFAULT_WIN_HEIGHT);
this.logger.height(Logger.DEFAULT_WIN_HEIGHT - this.header.height());
} else {
this.logWin.height(this.header.height());
this.logger.height(0);
}
},
isExpanded: function() {
return this.logWin.height() > this.header.height();
},
getLevelDisplayString: function(level) {
switch(level) {
case Logger.LEVEL_TRACE :
return "TRACE";
case Logger.LEVEL_DEBUG :
return "DEBUG";
case Logger.LEVEL_INFO :
return "INFO";
case Logger.LEVEL_WARN :
return "WARN";
case Logger.LEVEL_ERROR :
return "ERROR";
case Logger.LEVEL_FATAL :
return "FATAL";
case Logger.LEVEL_SYS :
return "SYSINFO";
}
return "LEVEL " + level;
},
// this is a util function which actually can be put elsewhere instead of in this class
objectToString : function(object) {
if(object) {
if(object instanceof Object) {
var sb = ['{' ];
$.each(object, function(name, val) {
sb.push('' + name + ': ');
if(val instanceof Object) {
sb.push(this.objectToString(val));
} else {
sb.push('' + val);
}
sb.push(',');
});
if(sb[sb.length - 1] == ',' )
sb.length = sb.length - 1;
sb.push('}');
return sb.join("");
} else {
return '' + object;
}
} else {
return 'label.na';
}
}
};