Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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