Bug 966674 - Part 1: Clean up a couple of ReferenceErrors. r=gps, a=sledru
authorIrving Reid <irving@mozilla.com>
Tue, 29 Apr 2014 10:09:25 -0400
changeset 199165 aa8268124ffe7508ecc9b346a0fe930ea6210c99
parent 199164 69aff133026910f0ec8738d2160aed72abde06fc
child 199166 7edd79e347ba5ed6c0f75d1d8dbad84f0c5633d9
push id3624
push userasasaki@mozilla.com
push dateMon, 09 Jun 2014 21:49:01 +0000
treeherdermozilla-beta@b1a5da15899a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgps, sledru
bugs966674
milestone31.0a2
Bug 966674 - Part 1: Clean up a couple of ReferenceErrors. r=gps, a=sledru
toolkit/modules/Log.jsm
toolkit/modules/tests/xpcshell/test_Log.js
--- a/toolkit/modules/Log.jsm
+++ b/toolkit/modules/Log.jsm
@@ -254,19 +254,25 @@ Logger.prototype = {
     }
     if (!params) {
       return this.log(this.level, undefined, {"action": action});
     }
     if (typeof params != "object") {
       throw "The params argument is required to be an object.";
     }
 
-    let level = params._level || this.level;
-    if ((typeof level == "string") && level in Log.Level.Numbers) {
-      level = Log.Level.Numbers[level];
+    let level = params._level;
+    if (level) {
+      let ulevel = level.toUpperCase();
+      if (ulevel in Log.Level.Numbers) {
+        level = Log.Level.Numbers[ulevel];
+      }
+    }
+    else {
+      level = this.level;
     }
 
     params.action = action;
     this.log(level, params._message, params);
   },
 
   log: function (level, string, params) {
     if (this.level > level)
@@ -433,30 +439,30 @@ function BasicFormatter(dateFormat) {
 }
 BasicFormatter.prototype = {
   __proto__: Formatter.prototype,
 
   format: function BF_format(message) {
     return message.time + "\t" +
       message.loggerName + "\t" +
       message.levelDesc + "\t" +
-      message.message + "\n";
+      message.message;
   }
 };
 
 /**
  * A formatter that only formats the string message component.
  */
 function MessageOnlyFormatter() {
 }
 MessageOnlyFormatter.prototype = Object.freeze({
   __proto__: Formatter.prototype,
 
   format: function (message) {
-    return message.message + "\n";
+    return message.message;
   },
 });
 
 // Structured formatter that outputs JSON based on message data.
 // This formatter will format unstructured messages by supplying
 // default values.
 function StructuredFormatter() { }
 StructuredFormatter.prototype = {
@@ -499,58 +505,66 @@ Appender.prototype = {
   level: Log.Level.All,
 
   append: function App_append(message) {
     if (message) {
       this.doAppend(this._formatter.format(message));
     }
   },
   toString: function App_toString() {
-    return this._name + " [level=" + this._level +
+    return this._name + " [level=" + this.level +
       ", formatter=" + this._formatter + "]";
   },
-  doAppend: function App_doAppend(message) {}
+  doAppend: function App_doAppend(formatted) {}
 };
 
 /*
  * DumpAppender
  * Logs to standard out
  */
 
 function DumpAppender(formatter) {
   Appender.call(this, formatter);
   this._name = "DumpAppender";
 }
 DumpAppender.prototype = {
   __proto__: Appender.prototype,
 
-  doAppend: function DApp_doAppend(message) {
-    dump(message);
+  doAppend: function DApp_doAppend(formatted) {
+    dump(formatted + "\n");
   }
 };
 
 /*
  * ConsoleAppender
  * Logs to the javascript console
  */
 
 function ConsoleAppender(formatter) {
   Appender.call(this, formatter);
   this._name = "ConsoleAppender";
 }
 ConsoleAppender.prototype = {
   __proto__: Appender.prototype,
 
-  doAppend: function CApp_doAppend(message) {
-    if (message.level > Log.Level.Warn) {
-      Cu.reportError(message);
-      return;
+  // XXX this should be replaced with calls to the Browser Console
+  append: function App_append(message) {
+    if (message) {
+      let m = this._formatter.format(message);
+      if (message.level > Log.Level.Warn) {
+        Cu.reportError(m);
+        return;
+      }
+      this.doAppend(m);
     }
+  },
+
+  doAppend: function CApp_doAppend(formatted) {
     Cc["@mozilla.org/consoleservice;1"].
-      getService(Ci.nsIConsoleService).logStringMessage(message);
+      getService(Ci.nsIConsoleService).logStringMessage(formatted);
   }
 };
 
 /**
  * Append to an nsIStorageStream
  *
  * This writes logging output to an in-memory stream which can later be read
  * back as an nsIInputStream. It can be used to avoid expensive I/O operations
@@ -609,29 +623,29 @@ StorageStreamAppender.prototype = {
     if (!this._outputStream) {
       return;
     }
     this.outputStream.close();
     this._outputStream = null;
     this._ss = null;
   },
 
-  doAppend: function (message) {
-    if (!message) {
+  doAppend: function (formatted) {
+    if (!formatted) {
       return;
     }
     try {
-      this.outputStream.writeString(message);
+      this.outputStream.writeString(formatted + "\n");
     } catch(ex) {
       if (ex.result == Cr.NS_BASE_STREAM_CLOSED) {
         // The underlying output stream is closed, so let's open a new one
         // and try again.
         this._outputStream = null;
       } try {
-          this.outputStream.writeString(message);
+          this.outputStream.writeString(formatted + "\n");
       } catch (ex) {
         // Ah well, we tried, but something seems to be hosed permanently.
       }
     }
   }
 };
 
 /**
@@ -677,18 +691,18 @@ FileAppender.prototype = {
 
     return this._fileReadyPromise.then(_ => {
       if (!this._file) {
         return this._openFile();
       }
     });
   },
 
-  doAppend: function (message) {
-    let array = this._encoder.encode(message);
+  doAppend: function (formatted) {
+    let array = this._encoder.encode(formatted + "\n");
     if (this._file) {
       this._lastWritePromise = this._file.write(array);
     } else {
       this._lastWritePromise = this._getFile().then(_ => {
         this._fileReadyPromise = null;
         if (this._file) {
           return this._file.write(array);
         }
@@ -704,41 +718,41 @@ FileAppender.prototype = {
     });
   }
 };
 
 /**
  * Bounded File appender
  *
  * Writes output to file using OS.File. After the total message size
- * (as defined by message.length) exceeds maxSize, existing messages
+ * (as defined by formatted.length) exceeds maxSize, existing messages
  * will be discarded, and subsequent writes will be appended to a new log file.
  */
 function BoundedFileAppender(path, formatter, maxSize=2*ONE_MEGABYTE) {
   FileAppender.call(this, path, formatter);
   this._name = "BoundedFileAppender";
   this._size = 0;
   this._maxSize = maxSize;
   this._closeFilePromise = null;
 }
 
 BoundedFileAppender.prototype = {
   __proto__: FileAppender.prototype,
 
-  doAppend: function (message) {
+  doAppend: function (formatted) {
     if (!this._removeFilePromise) {
       if (this._size < this._maxSize) {
-        this._size += message.length;
-        return FileAppender.prototype.doAppend.call(this, message);
+        this._size += formatted.length;
+        return FileAppender.prototype.doAppend.call(this, formatted);
       }
       this._removeFilePromise = this.reset();
     }
     this._removeFilePromise.then(_ => {
       this._removeFilePromise = null;
-      this.doAppend(message);
+      this.doAppend(formatted);
     });
   },
 
   reset: function () {
     let fileClosePromise;
     if (this._fileReadyPromise) {
       // An attempt to open the file may still be in progress.
       fileClosePromise = this._fileReadyPromise.then(_ => {
--- a/toolkit/modules/tests/xpcshell/test_Log.js
+++ b/toolkit/modules/tests/xpcshell/test_Log.js
@@ -7,17 +7,17 @@ Cu.import("resource://gre/modules/NetUti
 Cu.import("resource://gre/modules/osfile.jsm");
 
 Cu.import("resource://gre/modules/Log.jsm");
 
 let testFormatter = {
   format: function format(message) {
     return message.loggerName + "\t" +
       message.levelDesc + "\t" +
-      message.message + "\n";
+      message.message;
   }
 };
 
 function MockAppender(formatter) {
   Log.Appender.call(this, formatter);
   this.messages = [];
 }
 MockAppender.prototype = {
@@ -80,18 +80,18 @@ add_test(function test_LoggerWithMessage
   let prefixed = Log.repository.getLoggerWithMessagePrefix(
     "test.logger.prefix", "prefix: ");
 
   log.warn("no prefix");
   prefixed.warn("with prefix");
 
   Assert.equal(appender.messages.length, 2, "2 messages were logged.");
   Assert.deepEqual(appender.messages, [
-    "no prefix\n",
-    "prefix: with prefix\n",
+    "no prefix",
+    "prefix: with prefix",
   ], "Prefix logger works.");
 
   run_next_test();
 });
 
 // A utility method for checking object equivalence.
 // Fields with a reqular expression value in expected will be tested
 // against the corresponding value in actual. Otherwise objects