author | Giulio Cesare Solaroli <giulio.cesare@clipperz.com> | 2013-08-30 15:56:53 (UTC) |
---|---|---|
committer | Giulio Cesare Solaroli <giulio.cesare@clipperz.com> | 2013-08-30 21:23:42 (UTC) |
commit | 20bea94ab6b91c85b171dcf86baba0a64169d508 (patch) (unidiff) | |
tree | 6e38e91498dcdb861620eba1e237d1026fe79cc5 /frontend/delta/js/MochiKit/Logging.js | |
parent | bde3c7b98523112ade9c5bbf7390c4ecb494cd2e (diff) | |
download | clipperz-20bea94ab6b91c85b171dcf86baba0a64169d508.zip clipperz-20bea94ab6b91c85b171dcf86baba0a64169d508.tar.gz clipperz-20bea94ab6b91c85b171dcf86baba0a64169d508.tar.bz2 |
First release of /delta version
Diffstat (limited to 'frontend/delta/js/MochiKit/Logging.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/delta/js/MochiKit/Logging.js | 285 |
1 files changed, 285 insertions, 0 deletions
diff --git a/frontend/delta/js/MochiKit/Logging.js b/frontend/delta/js/MochiKit/Logging.js new file mode 100644 index 0000000..34070bc --- a/dev/null +++ b/frontend/delta/js/MochiKit/Logging.js | |||
@@ -0,0 +1,285 @@ | |||
1 | /* | ||
2 | |||
3 | Copyright 2008-2013 Clipperz Srl | ||
4 | |||
5 | This file is part of Clipperz, the online password manager. | ||
6 | For further information about its features and functionalities please | ||
7 | refer to http://www.clipperz.com. | ||
8 | |||
9 | * Clipperz is free software: you can redistribute it and/or modify it | ||
10 | under the terms of the GNU Affero General Public License as published | ||
11 | by the Free Software Foundation, either version 3 of the License, or | ||
12 | (at your option) any later version. | ||
13 | |||
14 | * Clipperz is distributed in the hope that it will be useful, but | ||
15 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
17 | See the GNU Affero General Public License for more details. | ||
18 | |||
19 | * You should have received a copy of the GNU Affero General Public | ||
20 | License along with Clipperz. If not, see http://www.gnu.org/licenses/. | ||
21 | |||
22 | */ | ||
23 | |||
24 | /*** | ||
25 | |||
26 | MochiKit.Logging 1.5 | ||
27 | |||
28 | See <http://mochikit.com/> for documentation, downloads, license, etc. | ||
29 | |||
30 | (c) 2005 Bob Ippolito. All rights Reserved. | ||
31 | |||
32 | ***/ | ||
33 | |||
34 | MochiKit.Base.module(MochiKit, 'Logging', '1.5', ['Base']); | ||
35 | |||
36 | /** @id MochiKit.Logging.LogMessage */ | ||
37 | MochiKit.Logging.LogMessage = function (num, level, info) { | ||
38 | this.num = num; | ||
39 | this.level = level; | ||
40 | this.info = info; | ||
41 | this.timestamp = new Date(); | ||
42 | }; | ||
43 | |||
44 | MochiKit.Logging.LogMessage.prototype = { | ||
45 | /** @id MochiKit.Logging.LogMessage.prototype.repr */ | ||
46 | repr: function () { | ||
47 | var m = MochiKit.Base; | ||
48 | return 'LogMessage(' + | ||
49 | m.map( | ||
50 | m.repr, | ||
51 | [this.num, this.level, this.info] | ||
52 | ).join(', ') + ')'; | ||
53 | }, | ||
54 | /** @id MochiKit.Logging.LogMessage.prototype.toString */ | ||
55 | toString: MochiKit.Base.forwardCall("repr") | ||
56 | }; | ||
57 | |||
58 | MochiKit.Base.update(MochiKit.Logging, { | ||
59 | /** @id MochiKit.Logging.logLevelAtLeast */ | ||
60 | logLevelAtLeast: function (minLevel) { | ||
61 | var self = MochiKit.Logging; | ||
62 | if (typeof(minLevel) == 'string') { | ||
63 | minLevel = self.LogLevel[minLevel]; | ||
64 | } | ||
65 | return function (msg) { | ||
66 | var msgLevel = msg.level; | ||
67 | if (typeof(msgLevel) == 'string') { | ||
68 | msgLevel = self.LogLevel[msgLevel]; | ||
69 | } | ||
70 | return msgLevel >= minLevel; | ||
71 | }; | ||
72 | }, | ||
73 | |||
74 | /** @id MochiKit.Logging.isLogMessage */ | ||
75 | isLogMessage: function (/* ... */) { | ||
76 | var LogMessage = MochiKit.Logging.LogMessage; | ||
77 | for (var i = 0; i < arguments.length; i++) { | ||
78 | if (!(arguments[i] instanceof LogMessage)) { | ||
79 | return false; | ||
80 | } | ||
81 | } | ||
82 | return true; | ||
83 | }, | ||
84 | |||
85 | /** @id MochiKit.Logging.compareLogMessage */ | ||
86 | compareLogMessage: function (a, b) { | ||
87 | return MochiKit.Base.compare([a.level, a.info], [b.level, b.info]); | ||
88 | }, | ||
89 | |||
90 | /** @id MochiKit.Logging.alertListener */ | ||
91 | alertListener: function (msg) { | ||
92 | alert( | ||
93 | "num: " + msg.num + | ||
94 | "\nlevel: " + msg.level + | ||
95 | "\ninfo: " + msg.info.join(" ") | ||
96 | ); | ||
97 | } | ||
98 | |||
99 | }); | ||
100 | |||
101 | /** @id MochiKit.Logging.Logger */ | ||
102 | MochiKit.Logging.Logger = function (/* optional */maxSize) { | ||
103 | this.counter = 0; | ||
104 | if (typeof(maxSize) == 'undefined' || maxSize === null) { | ||
105 | maxSize = -1; | ||
106 | } | ||
107 | this.maxSize = maxSize; | ||
108 | this._messages = []; | ||
109 | this.listeners = {}; | ||
110 | this.useNativeConsole = false; | ||
111 | }; | ||
112 | |||
113 | MochiKit.Logging.Logger.prototype = { | ||
114 | /** @id MochiKit.Logging.Logger.prototype.clear */ | ||
115 | clear: function () { | ||
116 | this._messages.splice(0, this._messages.length); | ||
117 | }, | ||
118 | |||
119 | /** @id MochiKit.Logging.Logger.prototype.logToConsole */ | ||
120 | logToConsole: function (msg) { | ||
121 | if (typeof(window) != "undefined" && window.console | ||
122 | && window.console.log) { | ||
123 | // Safari and FireBug 0.4 | ||
124 | // Percent replacement is a workaround for cute Safari crashing bug | ||
125 | window.console.log(msg.replace(/%/g, '\uFF05')); | ||
126 | } else if (typeof(opera) != "undefined" && opera.postError) { | ||
127 | // Opera | ||
128 | opera.postError(msg); | ||
129 | } else if (typeof(Debug) != "undefined" && Debug.writeln) { | ||
130 | // IE Web Development Helper (?) | ||
131 | // http://www.nikhilk.net/Entry.aspx?id=93 | ||
132 | Debug.writeln(msg); | ||
133 | } else if (typeof(debug) != "undefined" && debug.trace) { | ||
134 | // Atlas framework (?) | ||
135 | // http://www.nikhilk.net/Entry.aspx?id=93 | ||
136 | debug.trace(msg); | ||
137 | } | ||
138 | }, | ||
139 | |||
140 | /** @id MochiKit.Logging.Logger.prototype.dispatchListeners */ | ||
141 | dispatchListeners: function (msg) { | ||
142 | for (var k in this.listeners) { | ||
143 | var pair = this.listeners[k]; | ||
144 | if (pair.ident != k || (pair[0] && !pair[0](msg))) { | ||
145 | continue; | ||
146 | } | ||
147 | pair[1](msg); | ||
148 | } | ||
149 | }, | ||
150 | |||
151 | /** @id MochiKit.Logging.Logger.prototype.addListener */ | ||
152 | addListener: function (ident, filter, listener) { | ||
153 | if (typeof(filter) == 'string') { | ||
154 | filter = MochiKit.Logging.logLevelAtLeast(filter); | ||
155 | } | ||
156 | var entry = [filter, listener]; | ||
157 | entry.ident = ident; | ||
158 | this.listeners[ident] = entry; | ||
159 | }, | ||
160 | |||
161 | /** @id MochiKit.Logging.Logger.prototype.removeListener */ | ||
162 | removeListener: function (ident) { | ||
163 | delete this.listeners[ident]; | ||
164 | }, | ||
165 | |||
166 | /** @id MochiKit.Logging.Logger.prototype.baseLog */ | ||
167 | baseLog: function (level, message/*, ...*/) { | ||
168 | if (typeof(level) == "number") { | ||
169 | if (level >= MochiKit.Logging.LogLevel.FATAL) { | ||
170 | level = 'FATAL'; | ||
171 | } else if (level >= MochiKit.Logging.LogLevel.ERROR) { | ||
172 | level = 'ERROR'; | ||
173 | } else if (level >= MochiKit.Logging.LogLevel.WARNING) { | ||
174 | level = 'WARNING'; | ||
175 | } else if (level >= MochiKit.Logging.LogLevel.INFO) { | ||
176 | level = 'INFO'; | ||
177 | } else { | ||
178 | level = 'DEBUG'; | ||
179 | } | ||
180 | } | ||
181 | var msg = new MochiKit.Logging.LogMessage( | ||
182 | this.counter, | ||
183 | level, | ||
184 | MochiKit.Base.extend(null, arguments, 1) | ||
185 | ); | ||
186 | this._messages.push(msg); | ||
187 | this.dispatchListeners(msg); | ||
188 | if (this.useNativeConsole) { | ||
189 | this.logToConsole(msg.level + ": " + msg.info.join(" ")); | ||
190 | } | ||
191 | this.counter += 1; | ||
192 | while (this.maxSize >= 0 && this._messages.length > this.maxSize) { | ||
193 | this._messages.shift(); | ||
194 | } | ||
195 | }, | ||
196 | |||
197 | /** @id MochiKit.Logging.Logger.prototype.getMessages */ | ||
198 | getMessages: function (howMany) { | ||
199 | var firstMsg = 0; | ||
200 | if (!(typeof(howMany) == 'undefined' || howMany === null)) { | ||
201 | firstMsg = Math.max(0, this._messages.length - howMany); | ||
202 | } | ||
203 | return this._messages.slice(firstMsg); | ||
204 | }, | ||
205 | |||
206 | /** @id MochiKit.Logging.Logger.prototype.getMessageText */ | ||
207 | getMessageText: function (howMany) { | ||
208 | if (typeof(howMany) == 'undefined' || howMany === null) { | ||
209 | howMany = 30; | ||
210 | } | ||
211 | var messages = this.getMessages(howMany); | ||
212 | if (messages.length) { | ||
213 | var lst = MochiKit.Base.map(function (m) { | ||
214 | return '\n [' + m.num + '] ' + m.level + ': ' + m.info.join(' '); | ||
215 | }, messages); | ||
216 | lst.unshift('LAST ' + messages.length + ' MESSAGES:'); | ||
217 | return lst.join(''); | ||
218 | } | ||
219 | return ''; | ||
220 | }, | ||
221 | |||
222 | /** @id MochiKit.Logging.Logger.prototype.debuggingBookmarklet */ | ||
223 | debuggingBookmarklet: function (inline) { | ||
224 | if (typeof(MochiKit.LoggingPane) == "undefined") { | ||
225 | alert(this.getMessageText()); | ||
226 | } else { | ||
227 | MochiKit.LoggingPane.createLoggingPane(inline || false); | ||
228 | } | ||
229 | } | ||
230 | }; | ||
231 | |||
232 | MochiKit.Logging.__new__ = function () { | ||
233 | this.LogLevel = { | ||
234 | ERROR: 40, | ||
235 | FATAL: 50, | ||
236 | WARNING: 30, | ||
237 | INFO: 20, | ||
238 | DEBUG: 10 | ||
239 | }; | ||
240 | |||
241 | var m = MochiKit.Base; | ||
242 | m.registerComparator("LogMessage", | ||
243 | this.isLogMessage, | ||
244 | this.compareLogMessage | ||
245 | ); | ||
246 | |||
247 | var partial = m.partial; | ||
248 | |||
249 | var Logger = this.Logger; | ||
250 | var baseLog = Logger.prototype.baseLog; | ||
251 | m.update(this.Logger.prototype, { | ||
252 | debug: partial(baseLog, 'DEBUG'), | ||
253 | log: partial(baseLog, 'INFO'), | ||
254 | error: partial(baseLog, 'ERROR'), | ||
255 | fatal: partial(baseLog, 'FATAL'), | ||
256 | warning: partial(baseLog, 'WARNING') | ||
257 | }); | ||
258 | |||
259 | // indirectly find logger so it can be replaced | ||
260 | var self = this; | ||
261 | var connectLog = function (name) { | ||
262 | return function () { | ||
263 | self.logger[name].apply(self.logger, arguments); | ||
264 | }; | ||
265 | }; | ||
266 | |||
267 | /** @id MochiKit.Logging.log */ | ||
268 | this.log = connectLog('log'); | ||
269 | /** @id MochiKit.Logging.logError */ | ||
270 | this.logError = connectLog('error'); | ||
271 | /** @id MochiKit.Logging.logDebug */ | ||
272 | this.logDebug = connectLog('debug'); | ||
273 | /** @id MochiKit.Logging.logFatal */ | ||
274 | this.logFatal = connectLog('fatal'); | ||
275 | /** @id MochiKit.Logging.logWarning */ | ||
276 | this.logWarning = connectLog('warning'); | ||
277 | this.logger = new Logger(); | ||
278 | this.logger.useNativeConsole = true; | ||
279 | |||
280 | m.nameFunctions(this); | ||
281 | }; | ||
282 | |||
283 | MochiKit.Logging.__new__(); | ||
284 | |||
285 | MochiKit.Base._exportSymbols(this, MochiKit.Logging); | ||