|
1 /*** |
|
2 |
|
3 MochiKit.Logging 1.4.2 |
|
4 |
|
5 See <http://mochikit.com/> for documentation, downloads, license, etc. |
|
6 |
|
7 (c) 2005 Bob Ippolito. All rights Reserved. |
|
8 |
|
9 ***/ |
|
10 |
|
11 MochiKit.Base._deps('Logging', ['Base']); |
|
12 |
|
13 MochiKit.Logging.NAME = "MochiKit.Logging"; |
|
14 MochiKit.Logging.VERSION = "1.4.2"; |
|
15 MochiKit.Logging.__repr__ = function () { |
|
16 return "[" + this.NAME + " " + this.VERSION + "]"; |
|
17 }; |
|
18 |
|
19 MochiKit.Logging.toString = function () { |
|
20 return this.__repr__(); |
|
21 }; |
|
22 |
|
23 |
|
24 MochiKit.Logging.EXPORT = [ |
|
25 "LogLevel", |
|
26 "LogMessage", |
|
27 "Logger", |
|
28 "alertListener", |
|
29 "logger", |
|
30 "log", |
|
31 "logError", |
|
32 "logDebug", |
|
33 "logFatal", |
|
34 "logWarning" |
|
35 ]; |
|
36 |
|
37 |
|
38 MochiKit.Logging.EXPORT_OK = [ |
|
39 "logLevelAtLeast", |
|
40 "isLogMessage", |
|
41 "compareLogMessage" |
|
42 ]; |
|
43 |
|
44 |
|
45 /** @id MochiKit.Logging.LogMessage */ |
|
46 MochiKit.Logging.LogMessage = function (num, level, info) { |
|
47 this.num = num; |
|
48 this.level = level; |
|
49 this.info = info; |
|
50 this.timestamp = new Date(); |
|
51 }; |
|
52 |
|
53 MochiKit.Logging.LogMessage.prototype = { |
|
54 /** @id MochiKit.Logging.LogMessage.prototype.repr */ |
|
55 repr: function () { |
|
56 var m = MochiKit.Base; |
|
57 return 'LogMessage(' + |
|
58 m.map( |
|
59 m.repr, |
|
60 [this.num, this.level, this.info] |
|
61 ).join(', ') + ')'; |
|
62 }, |
|
63 /** @id MochiKit.Logging.LogMessage.prototype.toString */ |
|
64 toString: MochiKit.Base.forwardCall("repr") |
|
65 }; |
|
66 |
|
67 MochiKit.Base.update(MochiKit.Logging, { |
|
68 /** @id MochiKit.Logging.logLevelAtLeast */ |
|
69 logLevelAtLeast: function (minLevel) { |
|
70 var self = MochiKit.Logging; |
|
71 if (typeof(minLevel) == 'string') { |
|
72 minLevel = self.LogLevel[minLevel]; |
|
73 } |
|
74 return function (msg) { |
|
75 var msgLevel = msg.level; |
|
76 if (typeof(msgLevel) == 'string') { |
|
77 msgLevel = self.LogLevel[msgLevel]; |
|
78 } |
|
79 return msgLevel >= minLevel; |
|
80 }; |
|
81 }, |
|
82 |
|
83 /** @id MochiKit.Logging.isLogMessage */ |
|
84 isLogMessage: function (/* ... */) { |
|
85 var LogMessage = MochiKit.Logging.LogMessage; |
|
86 for (var i = 0; i < arguments.length; i++) { |
|
87 if (!(arguments[i] instanceof LogMessage)) { |
|
88 return false; |
|
89 } |
|
90 } |
|
91 return true; |
|
92 }, |
|
93 |
|
94 /** @id MochiKit.Logging.compareLogMessage */ |
|
95 compareLogMessage: function (a, b) { |
|
96 return MochiKit.Base.compare([a.level, a.info], [b.level, b.info]); |
|
97 }, |
|
98 |
|
99 /** @id MochiKit.Logging.alertListener */ |
|
100 alertListener: function (msg) { |
|
101 alert( |
|
102 "num: " + msg.num + |
|
103 "\nlevel: " + msg.level + |
|
104 "\ninfo: " + msg.info.join(" ") |
|
105 ); |
|
106 } |
|
107 |
|
108 }); |
|
109 |
|
110 /** @id MochiKit.Logging.Logger */ |
|
111 MochiKit.Logging.Logger = function (/* optional */maxSize) { |
|
112 this.counter = 0; |
|
113 if (typeof(maxSize) == 'undefined' || maxSize === null) { |
|
114 maxSize = -1; |
|
115 } |
|
116 this.maxSize = maxSize; |
|
117 this._messages = []; |
|
118 this.listeners = {}; |
|
119 this.useNativeConsole = false; |
|
120 }; |
|
121 |
|
122 MochiKit.Logging.Logger.prototype = { |
|
123 /** @id MochiKit.Logging.Logger.prototype.clear */ |
|
124 clear: function () { |
|
125 this._messages.splice(0, this._messages.length); |
|
126 }, |
|
127 |
|
128 /** @id MochiKit.Logging.Logger.prototype.logToConsole */ |
|
129 logToConsole: function (msg) { |
|
130 if (typeof(window) != "undefined" && window.console |
|
131 && window.console.log) { |
|
132 // Safari and FireBug 0.4 |
|
133 // Percent replacement is a workaround for cute Safari crashing bug |
|
134 window.console.log(msg.replace(/%/g, '\uFF05')); |
|
135 } else if (typeof(opera) != "undefined" && opera.postError) { |
|
136 // Opera |
|
137 opera.postError(msg); |
|
138 } else if (typeof(printfire) == "function") { |
|
139 // FireBug 0.3 and earlier |
|
140 printfire(msg); |
|
141 } else if (typeof(Debug) != "undefined" && Debug.writeln) { |
|
142 // IE Web Development Helper (?) |
|
143 // http://www.nikhilk.net/Entry.aspx?id=93 |
|
144 Debug.writeln(msg); |
|
145 } else if (typeof(debug) != "undefined" && debug.trace) { |
|
146 // Atlas framework (?) |
|
147 // http://www.nikhilk.net/Entry.aspx?id=93 |
|
148 debug.trace(msg); |
|
149 } |
|
150 }, |
|
151 |
|
152 /** @id MochiKit.Logging.Logger.prototype.dispatchListeners */ |
|
153 dispatchListeners: function (msg) { |
|
154 for (var k in this.listeners) { |
|
155 var pair = this.listeners[k]; |
|
156 if (pair.ident != k || (pair[0] && !pair[0](msg))) { |
|
157 continue; |
|
158 } |
|
159 pair[1](msg); |
|
160 } |
|
161 }, |
|
162 |
|
163 /** @id MochiKit.Logging.Logger.prototype.addListener */ |
|
164 addListener: function (ident, filter, listener) { |
|
165 if (typeof(filter) == 'string') { |
|
166 filter = MochiKit.Logging.logLevelAtLeast(filter); |
|
167 } |
|
168 var entry = [filter, listener]; |
|
169 entry.ident = ident; |
|
170 this.listeners[ident] = entry; |
|
171 }, |
|
172 |
|
173 /** @id MochiKit.Logging.Logger.prototype.removeListener */ |
|
174 removeListener: function (ident) { |
|
175 delete this.listeners[ident]; |
|
176 }, |
|
177 |
|
178 /** @id MochiKit.Logging.Logger.prototype.baseLog */ |
|
179 baseLog: function (level, message/*, ...*/) { |
|
180 if (typeof(level) == "number") { |
|
181 if (level >= MochiKit.Logging.LogLevel.FATAL) { |
|
182 level = 'FATAL'; |
|
183 } else if (level >= MochiKit.Logging.LogLevel.ERROR) { |
|
184 level = 'ERROR'; |
|
185 } else if (level >= MochiKit.Logging.LogLevel.WARNING) { |
|
186 level = 'WARNING'; |
|
187 } else if (level >= MochiKit.Logging.LogLevel.INFO) { |
|
188 level = 'INFO'; |
|
189 } else { |
|
190 level = 'DEBUG'; |
|
191 } |
|
192 } |
|
193 var msg = new MochiKit.Logging.LogMessage( |
|
194 this.counter, |
|
195 level, |
|
196 MochiKit.Base.extend(null, arguments, 1) |
|
197 ); |
|
198 this._messages.push(msg); |
|
199 this.dispatchListeners(msg); |
|
200 if (this.useNativeConsole) { |
|
201 this.logToConsole(msg.level + ": " + msg.info.join(" ")); |
|
202 } |
|
203 this.counter += 1; |
|
204 while (this.maxSize >= 0 && this._messages.length > this.maxSize) { |
|
205 this._messages.shift(); |
|
206 } |
|
207 }, |
|
208 |
|
209 /** @id MochiKit.Logging.Logger.prototype.getMessages */ |
|
210 getMessages: function (howMany) { |
|
211 var firstMsg = 0; |
|
212 if (!(typeof(howMany) == 'undefined' || howMany === null)) { |
|
213 firstMsg = Math.max(0, this._messages.length - howMany); |
|
214 } |
|
215 return this._messages.slice(firstMsg); |
|
216 }, |
|
217 |
|
218 /** @id MochiKit.Logging.Logger.prototype.getMessageText */ |
|
219 getMessageText: function (howMany) { |
|
220 if (typeof(howMany) == 'undefined' || howMany === null) { |
|
221 howMany = 30; |
|
222 } |
|
223 var messages = this.getMessages(howMany); |
|
224 if (messages.length) { |
|
225 var lst = map(function (m) { |
|
226 return '\n [' + m.num + '] ' + m.level + ': ' + m.info.join(' '); |
|
227 }, messages); |
|
228 lst.unshift('LAST ' + messages.length + ' MESSAGES:'); |
|
229 return lst.join(''); |
|
230 } |
|
231 return ''; |
|
232 }, |
|
233 |
|
234 /** @id MochiKit.Logging.Logger.prototype.debuggingBookmarklet */ |
|
235 debuggingBookmarklet: function (inline) { |
|
236 if (typeof(MochiKit.LoggingPane) == "undefined") { |
|
237 alert(this.getMessageText()); |
|
238 } else { |
|
239 MochiKit.LoggingPane.createLoggingPane(inline || false); |
|
240 } |
|
241 } |
|
242 }; |
|
243 |
|
244 MochiKit.Logging.__new__ = function () { |
|
245 this.LogLevel = { |
|
246 ERROR: 40, |
|
247 FATAL: 50, |
|
248 WARNING: 30, |
|
249 INFO: 20, |
|
250 DEBUG: 10 |
|
251 }; |
|
252 |
|
253 var m = MochiKit.Base; |
|
254 m.registerComparator("LogMessage", |
|
255 this.isLogMessage, |
|
256 this.compareLogMessage |
|
257 ); |
|
258 |
|
259 var partial = m.partial; |
|
260 |
|
261 var Logger = this.Logger; |
|
262 var baseLog = Logger.prototype.baseLog; |
|
263 m.update(this.Logger.prototype, { |
|
264 debug: partial(baseLog, 'DEBUG'), |
|
265 log: partial(baseLog, 'INFO'), |
|
266 error: partial(baseLog, 'ERROR'), |
|
267 fatal: partial(baseLog, 'FATAL'), |
|
268 warning: partial(baseLog, 'WARNING') |
|
269 }); |
|
270 |
|
271 // indirectly find logger so it can be replaced |
|
272 var self = this; |
|
273 var connectLog = function (name) { |
|
274 return function () { |
|
275 self.logger[name].apply(self.logger, arguments); |
|
276 }; |
|
277 }; |
|
278 |
|
279 /** @id MochiKit.Logging.log */ |
|
280 this.log = connectLog('log'); |
|
281 /** @id MochiKit.Logging.logError */ |
|
282 this.logError = connectLog('error'); |
|
283 /** @id MochiKit.Logging.logDebug */ |
|
284 this.logDebug = connectLog('debug'); |
|
285 /** @id MochiKit.Logging.logFatal */ |
|
286 this.logFatal = connectLog('fatal'); |
|
287 /** @id MochiKit.Logging.logWarning */ |
|
288 this.logWarning = connectLog('warning'); |
|
289 this.logger = new Logger(); |
|
290 this.logger.useNativeConsole = true; |
|
291 |
|
292 this.EXPORT_TAGS = { |
|
293 ":common": this.EXPORT, |
|
294 ":all": m.concat(this.EXPORT, this.EXPORT_OK) |
|
295 }; |
|
296 |
|
297 m.nameFunctions(this); |
|
298 |
|
299 }; |
|
300 |
|
301 if (typeof(printfire) == "undefined" && |
|
302 typeof(document) != "undefined" && document.createEvent && |
|
303 typeof(dispatchEvent) != "undefined") { |
|
304 // FireBug really should be less lame about this global function |
|
305 printfire = function () { |
|
306 printfire.args = arguments; |
|
307 var ev = document.createEvent("Events"); |
|
308 ev.initEvent("printfire", false, true); |
|
309 dispatchEvent(ev); |
|
310 }; |
|
311 } |
|
312 |
|
313 MochiKit.Logging.__new__(); |
|
314 |
|
315 MochiKit.Base._exportSymbols(this, MochiKit.Logging); |