Diffstat (limited to 'frontend/gamma/js/MochiKit/Logging.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/gamma/js/MochiKit/Logging.js | 262 |
1 files changed, 262 insertions, 0 deletions
diff --git a/frontend/gamma/js/MochiKit/Logging.js b/frontend/gamma/js/MochiKit/Logging.js new file mode 100644 index 0000000..f00996b --- a/dev/null +++ b/frontend/gamma/js/MochiKit/Logging.js | |||
@@ -0,0 +1,262 @@ | |||
1 | /*** | ||
2 | |||
3 | MochiKit.Logging 1.5 | ||
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._module('Logging', '1.5', ['Base']); | ||
12 | |||
13 | /** @id MochiKit.Logging.LogMessage */ | ||
14 | MochiKit.Logging.LogMessage = function (num, level, info) { | ||
15 | this.num = num; | ||
16 | this.level = level; | ||
17 | this.info = info; | ||
18 | this.timestamp = new Date(); | ||
19 | }; | ||
20 | |||
21 | MochiKit.Logging.LogMessage.prototype = { | ||
22 | /** @id MochiKit.Logging.LogMessage.prototype.repr */ | ||
23 | repr: function () { | ||
24 | var m = MochiKit.Base; | ||
25 | return 'LogMessage(' + | ||
26 | m.map( | ||
27 | m.repr, | ||
28 | [this.num, this.level, this.info] | ||
29 | ).join(', ') + ')'; | ||
30 | }, | ||
31 | /** @id MochiKit.Logging.LogMessage.prototype.toString */ | ||
32 | toString: MochiKit.Base.forwardCall("repr") | ||
33 | }; | ||
34 | |||
35 | MochiKit.Base.update(MochiKit.Logging, { | ||
36 | /** @id MochiKit.Logging.logLevelAtLeast */ | ||
37 | logLevelAtLeast: function (minLevel) { | ||
38 | var self = MochiKit.Logging; | ||
39 | if (typeof(minLevel) == 'string') { | ||
40 | minLevel = self.LogLevel[minLevel]; | ||
41 | } | ||
42 | return function (msg) { | ||
43 | var msgLevel = msg.level; | ||
44 | if (typeof(msgLevel) == 'string') { | ||
45 | msgLevel = self.LogLevel[msgLevel]; | ||
46 | } | ||
47 | return msgLevel >= minLevel; | ||
48 | }; | ||
49 | }, | ||
50 | |||
51 | /** @id MochiKit.Logging.isLogMessage */ | ||
52 | isLogMessage: function (/* ... */) { | ||
53 | var LogMessage = MochiKit.Logging.LogMessage; | ||
54 | for (var i = 0; i < arguments.length; i++) { | ||
55 | if (!(arguments[i] instanceof LogMessage)) { | ||
56 | return false; | ||
57 | } | ||
58 | } | ||
59 | return true; | ||
60 | }, | ||
61 | |||
62 | /** @id MochiKit.Logging.compareLogMessage */ | ||
63 | compareLogMessage: function (a, b) { | ||
64 | return MochiKit.Base.compare([a.level, a.info], [b.level, b.info]); | ||
65 | }, | ||
66 | |||
67 | /** @id MochiKit.Logging.alertListener */ | ||
68 | alertListener: function (msg) { | ||
69 | alert( | ||
70 | "num: " + msg.num + | ||
71 | "\nlevel: " + msg.level + | ||
72 | "\ninfo: " + msg.info.join(" ") | ||
73 | ); | ||
74 | } | ||
75 | |||
76 | }); | ||
77 | |||
78 | /** @id MochiKit.Logging.Logger */ | ||
79 | MochiKit.Logging.Logger = function (/* optional */maxSize) { | ||
80 | this.counter = 0; | ||
81 | if (typeof(maxSize) == 'undefined' || maxSize === null) { | ||
82 | maxSize = -1; | ||
83 | } | ||
84 | this.maxSize = maxSize; | ||
85 | this._messages = []; | ||
86 | this.listeners = {}; | ||
87 | this.useNativeConsole = false; | ||
88 | }; | ||
89 | |||
90 | MochiKit.Logging.Logger.prototype = { | ||
91 | /** @id MochiKit.Logging.Logger.prototype.clear */ | ||
92 | clear: function () { | ||
93 | this._messages.splice(0, this._messages.length); | ||
94 | }, | ||
95 | |||
96 | /** @id MochiKit.Logging.Logger.prototype.logToConsole */ | ||
97 | logToConsole: function (msg) { | ||
98 | if (typeof(window) != "undefined" && window.console | ||
99 | && window.console.log) { | ||
100 | // Safari and FireBug 0.4 | ||
101 | // Percent replacement is a workaround for cute Safari crashing bug | ||
102 | window.console.log(msg.replace(/%/g, '\uFF05')); | ||
103 | } else if (typeof(opera) != "undefined" && opera.postError) { | ||
104 | // Opera | ||
105 | opera.postError(msg); | ||
106 | } else if (typeof(Debug) != "undefined" && Debug.writeln) { | ||
107 | // IE Web Development Helper (?) | ||
108 | // http://www.nikhilk.net/Entry.aspx?id=93 | ||
109 | Debug.writeln(msg); | ||
110 | } else if (typeof(debug) != "undefined" && debug.trace) { | ||
111 | // Atlas framework (?) | ||
112 | // http://www.nikhilk.net/Entry.aspx?id=93 | ||
113 | debug.trace(msg); | ||
114 | } | ||
115 | }, | ||
116 | |||
117 | /** @id MochiKit.Logging.Logger.prototype.dispatchListeners */ | ||
118 | dispatchListeners: function (msg) { | ||
119 | for (var k in this.listeners) { | ||
120 | var pair = this.listeners[k]; | ||
121 | if (pair.ident != k || (pair[0] && !pair[0](msg))) { | ||
122 | continue; | ||
123 | } | ||
124 | pair[1](msg); | ||
125 | } | ||
126 | }, | ||
127 | |||
128 | /** @id MochiKit.Logging.Logger.prototype.addListener */ | ||
129 | addListener: function (ident, filter, listener) { | ||
130 | if (typeof(filter) == 'string') { | ||
131 | filter = MochiKit.Logging.logLevelAtLeast(filter); | ||
132 | } | ||
133 | var entry = [filter, listener]; | ||
134 | entry.ident = ident; | ||
135 | this.listeners[ident] = entry; | ||
136 | }, | ||
137 | |||
138 | /** @id MochiKit.Logging.Logger.prototype.removeListener */ | ||
139 | removeListener: function (ident) { | ||
140 | delete this.listeners[ident]; | ||
141 | }, | ||
142 | |||
143 | /** @id MochiKit.Logging.Logger.prototype.baseLog */ | ||
144 | baseLog: function (level, message/*, ...*/) { | ||
145 | if (typeof(level) == "number") { | ||
146 | if (level >= MochiKit.Logging.LogLevel.FATAL) { | ||
147 | level = 'FATAL'; | ||
148 | } else if (level >= MochiKit.Logging.LogLevel.ERROR) { | ||
149 | level = 'ERROR'; | ||
150 | } else if (level >= MochiKit.Logging.LogLevel.WARNING) { | ||
151 | level = 'WARNING'; | ||
152 | } else if (level >= MochiKit.Logging.LogLevel.INFO) { | ||
153 | level = 'INFO'; | ||
154 | } else { | ||
155 | level = 'DEBUG'; | ||
156 | } | ||
157 | } | ||
158 | var msg = new MochiKit.Logging.LogMessage( | ||
159 | this.counter, | ||
160 | level, | ||
161 | MochiKit.Base.extend(null, arguments, 1) | ||
162 | ); | ||
163 | this._messages.push(msg); | ||
164 | this.dispatchListeners(msg); | ||
165 | if (this.useNativeConsole) { | ||
166 | this.logToConsole(msg.level + ": " + msg.info.join(" ")); | ||
167 | } | ||
168 | this.counter += 1; | ||
169 | while (this.maxSize >= 0 && this._messages.length > this.maxSize) { | ||
170 | this._messages.shift(); | ||
171 | } | ||
172 | }, | ||
173 | |||
174 | /** @id MochiKit.Logging.Logger.prototype.getMessages */ | ||
175 | getMessages: function (howMany) { | ||
176 | var firstMsg = 0; | ||
177 | if (!(typeof(howMany) == 'undefined' || howMany === null)) { | ||
178 | firstMsg = Math.max(0, this._messages.length - howMany); | ||
179 | } | ||
180 | return this._messages.slice(firstMsg); | ||
181 | }, | ||
182 | |||
183 | /** @id MochiKit.Logging.Logger.prototype.getMessageText */ | ||
184 | getMessageText: function (howMany) { | ||
185 | if (typeof(howMany) == 'undefined' || howMany === null) { | ||
186 | howMany = 30; | ||
187 | } | ||
188 | var messages = this.getMessages(howMany); | ||
189 | if (messages.length) { | ||
190 | var lst = map(function (m) { | ||
191 | return '\n [' + m.num + '] ' + m.level + ': ' + m.info.join(' '); | ||
192 | }, messages); | ||
193 | lst.unshift('LAST ' + messages.length + ' MESSAGES:'); | ||
194 | return lst.join(''); | ||
195 | } | ||
196 | return ''; | ||
197 | }, | ||
198 | |||
199 | /** @id MochiKit.Logging.Logger.prototype.debuggingBookmarklet */ | ||
200 | debuggingBookmarklet: function (inline) { | ||
201 | if (typeof(MochiKit.LoggingPane) == "undefined") { | ||
202 | alert(this.getMessageText()); | ||
203 | } else { | ||
204 | MochiKit.LoggingPane.createLoggingPane(inline || false); | ||
205 | } | ||
206 | } | ||
207 | }; | ||
208 | |||
209 | MochiKit.Logging.__new__ = function () { | ||
210 | this.LogLevel = { | ||
211 | ERROR: 40, | ||
212 | FATAL: 50, | ||
213 | WARNING: 30, | ||
214 | INFO: 20, | ||
215 | DEBUG: 10 | ||
216 | }; | ||
217 | |||
218 | var m = MochiKit.Base; | ||
219 | m.registerComparator("LogMessage", | ||
220 | this.isLogMessage, | ||
221 | this.compareLogMessage | ||
222 | ); | ||
223 | |||
224 | var partial = m.partial; | ||
225 | |||
226 | var Logger = this.Logger; | ||
227 | var baseLog = Logger.prototype.baseLog; | ||
228 | m.update(this.Logger.prototype, { | ||
229 | debug: partial(baseLog, 'DEBUG'), | ||
230 | log: partial(baseLog, 'INFO'), | ||
231 | error: partial(baseLog, 'ERROR'), | ||
232 | fatal: partial(baseLog, 'FATAL'), | ||
233 | warning: partial(baseLog, 'WARNING') | ||
234 | }); | ||
235 | |||
236 | // indirectly find logger so it can be replaced | ||
237 | var self = this; | ||
238 | var connectLog = function (name) { | ||
239 | return function () { | ||
240 | self.logger[name].apply(self.logger, arguments); | ||
241 | }; | ||
242 | }; | ||
243 | |||
244 | /** @id MochiKit.Logging.log */ | ||
245 | this.log = connectLog('log'); | ||
246 | /** @id MochiKit.Logging.logError */ | ||
247 | this.logError = connectLog('error'); | ||
248 | /** @id MochiKit.Logging.logDebug */ | ||
249 | this.logDebug = connectLog('debug'); | ||
250 | /** @id MochiKit.Logging.logFatal */ | ||
251 | this.logFatal = connectLog('fatal'); | ||
252 | /** @id MochiKit.Logging.logWarning */ | ||
253 | this.logWarning = connectLog('warning'); | ||
254 | this.logger = new Logger(); | ||
255 | this.logger.useNativeConsole = true; | ||
256 | |||
257 | m.nameFunctions(this); | ||
258 | }; | ||
259 | |||
260 | MochiKit.Logging.__new__(); | ||
261 | |||
262 | MochiKit.Base._exportSymbols(this, MochiKit.Logging); | ||