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

     1 /***
     3 MochiKit.Logging 1.4.2
     5 See <http://mochikit.com/> for documentation, downloads, license, etc.
     7 (c) 2005 Bob Ippolito.  All rights Reserved.
     9 ***/
    11 MochiKit.Base._deps('Logging', ['Base']);
    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 };
    19 MochiKit.Logging.toString = function () {
    20     return this.__repr__();
    21 };
    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 ];
    38 MochiKit.Logging.EXPORT_OK = [
    39     "logLevelAtLeast",
    40     "isLogMessage",
    41     "compareLogMessage"
    42 ];
    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 };
    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 };
    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     },
    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     },
    94     /** @id MochiKit.Logging.compareLogMessage */
    95     compareLogMessage: function (a, b) {
    96         return MochiKit.Base.compare([a.level, a.info], [b.level, b.info]);
    97     },
    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     }
   108 });
   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 };
   122 MochiKit.Logging.Logger.prototype = {
   123     /** @id MochiKit.Logging.Logger.prototype.clear */
   124     clear: function () {
   125         this._messages.splice(0, this._messages.length);
   126     },
   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     },
   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     },
   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     },
   173     /** @id MochiKit.Logging.Logger.prototype.removeListener */
   174     removeListener: function (ident) {
   175         delete this.listeners[ident];
   176     },
   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     },
   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     },
   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     },
   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 };
   244 MochiKit.Logging.__new__ = function () {
   245     this.LogLevel = {
   246         ERROR: 40,
   247         FATAL: 50,
   248         WARNING: 30,
   249         INFO: 20,
   250         DEBUG: 10
   251     };
   253     var m = MochiKit.Base;
   254     m.registerComparator("LogMessage",
   255         this.isLogMessage,
   256         this.compareLogMessage
   257     );
   259     var partial = m.partial;
   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     });
   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     };
   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;
   292     this.EXPORT_TAGS = {
   293         ":common": this.EXPORT,
   294         ":all": m.concat(this.EXPORT, this.EXPORT_OK)
   295     };
   297     m.nameFunctions(this);
   299 };
   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 }
   313 MochiKit.Logging.__new__();
   315 MochiKit.Base._exportSymbols(this, MochiKit.Logging);

mercurial