testing/mochitest/tests/MochiKit-1.4.2/MochiKit/Logging.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial