1318 |
alexandre_ |
1 |
/*
|
|
|
2 |
Copyright (c) 2004-2006, The Dojo Foundation
|
|
|
3 |
All Rights Reserved.
|
|
|
4 |
|
|
|
5 |
Licensed under the Academic Free License version 2.1 or above OR the
|
|
|
6 |
modified BSD license. For more information on Dojo licensing, see:
|
|
|
7 |
|
|
|
8 |
http://dojotoolkit.org/community/licensing.shtml
|
|
|
9 |
*/
|
|
|
10 |
|
1422 |
alexandre_ |
11 |
|
|
|
12 |
|
1318 |
alexandre_ |
13 |
dojo.provide("dojo.logging.Logger");
|
|
|
14 |
dojo.provide("dojo.logging.LogFilter");
|
|
|
15 |
dojo.provide("dojo.logging.Record");
|
|
|
16 |
dojo.provide("dojo.log");
|
|
|
17 |
dojo.require("dojo.lang.common");
|
|
|
18 |
dojo.require("dojo.lang.declare");
|
|
|
19 |
dojo.logging.Record = function (logLevel, message) {
|
|
|
20 |
this.level = logLevel;
|
|
|
21 |
this.message = "";
|
|
|
22 |
this.msgArgs = [];
|
|
|
23 |
this.time = new Date();
|
|
|
24 |
if (dojo.lang.isArray(message)) {
|
|
|
25 |
if (message.length > 0 && dojo.lang.isString(message[0])) {
|
|
|
26 |
this.message = message.shift();
|
|
|
27 |
}
|
|
|
28 |
this.msgArgs = message;
|
|
|
29 |
} else {
|
|
|
30 |
this.message = message;
|
|
|
31 |
}
|
|
|
32 |
};
|
|
|
33 |
dojo.logging.LogFilter = function (loggerChain) {
|
|
|
34 |
this.passChain = loggerChain || "";
|
|
|
35 |
this.filter = function (record) {
|
|
|
36 |
return true;
|
|
|
37 |
};
|
|
|
38 |
};
|
|
|
39 |
dojo.logging.Logger = function () {
|
|
|
40 |
this.cutOffLevel = 0;
|
|
|
41 |
this.propagate = true;
|
|
|
42 |
this.parent = null;
|
|
|
43 |
this.data = [];
|
|
|
44 |
this.filters = [];
|
|
|
45 |
this.handlers = [];
|
|
|
46 |
};
|
|
|
47 |
dojo.extend(dojo.logging.Logger, {_argsToArr:function (args) {
|
|
|
48 |
var ret = [];
|
|
|
49 |
for (var x = 0; x < args.length; x++) {
|
|
|
50 |
ret.push(args[x]);
|
|
|
51 |
}
|
|
|
52 |
return ret;
|
|
|
53 |
}, setLevel:function (lvl) {
|
|
|
54 |
this.cutOffLevel = parseInt(lvl);
|
|
|
55 |
}, isEnabledFor:function (lvl) {
|
|
|
56 |
return parseInt(lvl) >= this.cutOffLevel;
|
|
|
57 |
}, getEffectiveLevel:function () {
|
|
|
58 |
if ((this.cutOffLevel == 0) && (this.parent)) {
|
|
|
59 |
return this.parent.getEffectiveLevel();
|
|
|
60 |
}
|
|
|
61 |
return this.cutOffLevel;
|
|
|
62 |
}, addFilter:function (flt) {
|
|
|
63 |
this.filters.push(flt);
|
|
|
64 |
return this.filters.length - 1;
|
|
|
65 |
}, removeFilterByIndex:function (fltIndex) {
|
|
|
66 |
if (this.filters[fltIndex]) {
|
|
|
67 |
delete this.filters[fltIndex];
|
|
|
68 |
return true;
|
|
|
69 |
}
|
|
|
70 |
return false;
|
|
|
71 |
}, removeFilter:function (fltRef) {
|
|
|
72 |
for (var x = 0; x < this.filters.length; x++) {
|
|
|
73 |
if (this.filters[x] === fltRef) {
|
|
|
74 |
delete this.filters[x];
|
|
|
75 |
return true;
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
return false;
|
|
|
79 |
}, removeAllFilters:function () {
|
|
|
80 |
this.filters = [];
|
|
|
81 |
}, filter:function (rec) {
|
|
|
82 |
for (var x = 0; x < this.filters.length; x++) {
|
|
|
83 |
if ((this.filters[x]["filter"]) && (!this.filters[x].filter(rec)) || (rec.level < this.cutOffLevel)) {
|
|
|
84 |
return false;
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
return true;
|
|
|
88 |
}, addHandler:function (hdlr) {
|
|
|
89 |
this.handlers.push(hdlr);
|
|
|
90 |
return this.handlers.length - 1;
|
|
|
91 |
}, handle:function (rec) {
|
|
|
92 |
if ((!this.filter(rec)) || (rec.level < this.cutOffLevel)) {
|
|
|
93 |
return false;
|
|
|
94 |
}
|
|
|
95 |
for (var x = 0; x < this.handlers.length; x++) {
|
|
|
96 |
if (this.handlers[x]["handle"]) {
|
|
|
97 |
this.handlers[x].handle(rec);
|
|
|
98 |
}
|
|
|
99 |
}
|
|
|
100 |
return true;
|
|
|
101 |
}, log:function (lvl, msg) {
|
|
|
102 |
if ((this.propagate) && (this.parent) && (this.parent.rec.level >= this.cutOffLevel)) {
|
|
|
103 |
this.parent.log(lvl, msg);
|
|
|
104 |
return false;
|
|
|
105 |
}
|
|
|
106 |
this.handle(new dojo.logging.Record(lvl, msg));
|
|
|
107 |
return true;
|
|
|
108 |
}, debug:function (msg) {
|
|
|
109 |
return this.logType("DEBUG", this._argsToArr(arguments));
|
|
|
110 |
}, info:function (msg) {
|
|
|
111 |
return this.logType("INFO", this._argsToArr(arguments));
|
|
|
112 |
}, warning:function (msg) {
|
|
|
113 |
return this.logType("WARNING", this._argsToArr(arguments));
|
|
|
114 |
}, error:function (msg) {
|
|
|
115 |
return this.logType("ERROR", this._argsToArr(arguments));
|
|
|
116 |
}, critical:function (msg) {
|
|
|
117 |
return this.logType("CRITICAL", this._argsToArr(arguments));
|
|
|
118 |
}, exception:function (msg, e, squelch) {
|
|
|
119 |
if (e) {
|
|
|
120 |
var eparts = [e.name, (e.description || e.message)];
|
|
|
121 |
if (e.fileName) {
|
|
|
122 |
eparts.push(e.fileName);
|
|
|
123 |
eparts.push("line " + e.lineNumber);
|
|
|
124 |
}
|
|
|
125 |
msg += " " + eparts.join(" : ");
|
|
|
126 |
}
|
|
|
127 |
this.logType("ERROR", msg);
|
|
|
128 |
if (!squelch) {
|
|
|
129 |
throw e;
|
|
|
130 |
}
|
|
|
131 |
}, logType:function (type, args) {
|
|
|
132 |
return this.log.apply(this, [dojo.logging.log.getLevel(type), args]);
|
|
|
133 |
}, warn:function () {
|
|
|
134 |
this.warning.apply(this, arguments);
|
|
|
135 |
}, err:function () {
|
|
|
136 |
this.error.apply(this, arguments);
|
|
|
137 |
}, crit:function () {
|
|
|
138 |
this.critical.apply(this, arguments);
|
|
|
139 |
}});
|
|
|
140 |
dojo.logging.LogHandler = function (level) {
|
|
|
141 |
this.cutOffLevel = (level) ? level : 0;
|
|
|
142 |
this.formatter = null;
|
|
|
143 |
this.data = [];
|
|
|
144 |
this.filters = [];
|
|
|
145 |
};
|
|
|
146 |
dojo.lang.extend(dojo.logging.LogHandler, {setFormatter:function (formatter) {
|
|
|
147 |
dojo.unimplemented("setFormatter");
|
|
|
148 |
}, flush:function () {
|
|
|
149 |
}, close:function () {
|
|
|
150 |
}, handleError:function () {
|
|
|
151 |
dojo.deprecated("dojo.logging.LogHandler.handleError", "use handle()", "0.6");
|
|
|
152 |
}, handle:function (record) {
|
|
|
153 |
if ((this.filter(record)) && (record.level >= this.cutOffLevel)) {
|
|
|
154 |
this.emit(record);
|
|
|
155 |
}
|
|
|
156 |
}, emit:function (record) {
|
|
|
157 |
dojo.unimplemented("emit");
|
|
|
158 |
}});
|
|
|
159 |
void (function () {
|
|
|
160 |
var names = ["setLevel", "addFilter", "removeFilterByIndex", "removeFilter", "removeAllFilters", "filter"];
|
|
|
161 |
var tgt = dojo.logging.LogHandler.prototype;
|
|
|
162 |
var src = dojo.logging.Logger.prototype;
|
|
|
163 |
for (var x = 0; x < names.length; x++) {
|
|
|
164 |
tgt[names[x]] = src[names[x]];
|
|
|
165 |
}
|
|
|
166 |
})();
|
|
|
167 |
dojo.logging.log = new dojo.logging.Logger();
|
|
|
168 |
dojo.logging.log.levels = [{"name":"DEBUG", "level":1}, {"name":"INFO", "level":2}, {"name":"WARNING", "level":3}, {"name":"ERROR", "level":4}, {"name":"CRITICAL", "level":5}];
|
|
|
169 |
dojo.logging.log.loggers = {};
|
|
|
170 |
dojo.logging.log.getLogger = function (name) {
|
|
|
171 |
if (!this.loggers[name]) {
|
|
|
172 |
this.loggers[name] = new dojo.logging.Logger();
|
|
|
173 |
this.loggers[name].parent = this;
|
|
|
174 |
}
|
|
|
175 |
return this.loggers[name];
|
|
|
176 |
};
|
|
|
177 |
dojo.logging.log.getLevelName = function (lvl) {
|
|
|
178 |
for (var x = 0; x < this.levels.length; x++) {
|
|
|
179 |
if (this.levels[x].level == lvl) {
|
|
|
180 |
return this.levels[x].name;
|
|
|
181 |
}
|
|
|
182 |
}
|
|
|
183 |
return null;
|
|
|
184 |
};
|
|
|
185 |
dojo.logging.log.getLevel = function (name) {
|
|
|
186 |
for (var x = 0; x < this.levels.length; x++) {
|
|
|
187 |
if (this.levels[x].name.toUpperCase() == name.toUpperCase()) {
|
|
|
188 |
return this.levels[x].level;
|
|
|
189 |
}
|
|
|
190 |
}
|
|
|
191 |
return null;
|
|
|
192 |
};
|
|
|
193 |
dojo.declare("dojo.logging.MemoryLogHandler", dojo.logging.LogHandler, {initializer:function (level, recordsToKeep, postType, postInterval) {
|
|
|
194 |
dojo.logging.LogHandler.call(this, level);
|
|
|
195 |
this.numRecords = (typeof djConfig["loggingNumRecords"] != "undefined") ? djConfig["loggingNumRecords"] : ((recordsToKeep) ? recordsToKeep : -1);
|
|
|
196 |
this.postType = (typeof djConfig["loggingPostType"] != "undefined") ? djConfig["loggingPostType"] : (postType || -1);
|
|
|
197 |
this.postInterval = (typeof djConfig["loggingPostInterval"] != "undefined") ? djConfig["loggingPostInterval"] : (postType || -1);
|
|
|
198 |
}, emit:function (record) {
|
|
|
199 |
if (!djConfig.isDebug) {
|
|
|
200 |
return;
|
|
|
201 |
}
|
|
|
202 |
var logStr = String(dojo.log.getLevelName(record.level) + ": " + record.time.toLocaleTimeString()) + ": " + record.message;
|
|
|
203 |
if (!dj_undef("println", dojo.hostenv)) {
|
|
|
204 |
dojo.hostenv.println(logStr, record.msgArgs);
|
|
|
205 |
}
|
|
|
206 |
this.data.push(record);
|
|
|
207 |
if (this.numRecords != -1) {
|
|
|
208 |
while (this.data.length > this.numRecords) {
|
|
|
209 |
this.data.shift();
|
|
|
210 |
}
|
|
|
211 |
}
|
|
|
212 |
}});
|
|
|
213 |
dojo.logging.logQueueHandler = new dojo.logging.MemoryLogHandler(0, 50, 0, 10000);
|
|
|
214 |
dojo.logging.log.addHandler(dojo.logging.logQueueHandler);
|
|
|
215 |
dojo.log = dojo.logging.log;
|
|
|
216 |
|