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