Bug 1425574 - Fill the feature gap between Console.jsm and Console API - part 9 - maxLogLevel pref, r=smaug, r=bgrins
authorAndrea Marchesini <amarchesini@mozilla.com>
Thu, 04 Jan 2018 19:19:44 +0100
changeset 449556 edb150980a7f29cb7e7125b4805964e7b094c503
parent 449555 7a4e812ee5adc626ba19828f770d720f0b9db9cd
child 449557 5ce40a4ffe442f8f4cf4cd839f54bdc936567787
push id8527
push userCallek@gmail.com
push dateThu, 11 Jan 2018 21:05:50 +0000
treeherdermozilla-beta@95342d212a7a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug, bgrins
bugs1425574
milestone59.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1425574 - Fill the feature gap between Console.jsm and Console API - part 9 - maxLogLevel pref, r=smaug, r=bgrins
dom/console/ConsoleInstance.cpp
dom/console/tests/console.jsm
dom/console/tests/test_jsm.xul
dom/webidl/Console.webidl
--- a/dom/console/ConsoleInstance.cpp
+++ b/dom/console/ConsoleInstance.cpp
@@ -14,16 +14,44 @@ NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Co
 
 NS_IMPL_CYCLE_COLLECTING_ADDREF(ConsoleInstance)
 NS_IMPL_CYCLE_COLLECTING_RELEASE(ConsoleInstance)
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ConsoleInstance)
   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
 NS_INTERFACE_MAP_END
 
+namespace {
+
+ConsoleLogLevel
+PrefToValue(const nsCString& aPref)
+{
+  if (!NS_IsMainThread()) {
+    NS_WARNING("Console.maxLogLevelPref is not supported on workers!");
+    return ConsoleLogLevel::All;
+  }
+
+  nsAutoCString value;
+  nsresult rv = Preferences::GetCString(aPref.get(), value);
+  if (NS_WARN_IF(NS_FAILED(rv))) {
+    return ConsoleLogLevel::All;
+  }
+
+  int index = FindEnumStringIndexImpl(value.get(), value.Length(),
+                                      ConsoleLogLevelValues::strings);
+  if (NS_WARN_IF(index < 0)) {
+    return ConsoleLogLevel::All;
+  }
+
+  MOZ_ASSERT(index < (int)ConsoleLogLevel::EndGuard_);
+  return static_cast<ConsoleLogLevel>(index);
+}
+
+} // anonymous
+
 ConsoleInstance::ConsoleInstance(const ConsoleInstanceOptions& aOptions)
   : mConsole(new Console(nullptr))
 {
   mConsole->mConsoleID = aOptions.mConsoleID;
   mConsole->mPassedInnerID = aOptions.mInnerID;
 
   if (aOptions.mDump.WasPassed()) {
     mConsole->mDumpFunction = &aOptions.mDump.Value();
@@ -35,16 +63,21 @@ ConsoleInstance::ConsoleInstance(const C
   mConsole->mDumpPrefix = aOptions.mPrefix;
 
   // Let's inform that this is a custom instance.
   mConsole->mChromeInstance = true;
 
   if (aOptions.mMaxLogLevel.WasPassed()) {
     mConsole->mMaxLogLevel = aOptions.mMaxLogLevel.Value();
   }
+
+  if (!aOptions.mMaxLogLevelPref.IsEmpty()) {
+    mConsole->mMaxLogLevel =
+      PrefToValue(NS_ConvertUTF16toUTF8(aOptions.mMaxLogLevelPref));
+  }
 }
 
 ConsoleInstance::~ConsoleInstance()
 {}
 
 JSObject*
 ConsoleInstance::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
 {
--- a/dom/console/tests/console.jsm
+++ b/dom/console/tests/console.jsm
@@ -8,15 +8,20 @@ this.ConsoleTest = {
   go: function(dumpFunction) {
     console.log("Hello world!");
     console.createInstance().log("Hello world!");
 
     let c = console.createInstance({
       consoleID: "wow",
       innerID: "CUSTOM INNER",
       dump: dumpFunction,
-      prefix: "_PREFIX_";
+      prefix: "_PREFIX_",
     });
 
     c.log("Hello world!");
     c.trace("Hello world!");
+
+    console.createInstance({ innerID: "LEVEL", maxLogLevel: "off" }).log("Invisible!");
+    console.createInstance({ innerID: "LEVEL",  maxLogLevel: "all" }).log("Hello world!");
+    console.createInstance({ innerID: "LEVEL", maxLogLevelPref: "foo.pref" }).log("Invisible!");
+    console.createInstance({ innerID: "LEVEL", maxLogLevelPref: "pref.test.console" }).log("Hello world!");
   }
 };
--- a/dom/console/tests/test_jsm.xul
+++ b/dom/console/tests/test_jsm.xul
@@ -37,37 +37,44 @@ consoleListener.prototype  = {
       if (obj.innerID == JSM) {
         is(obj.ID, "jsm", "ID and InnerID are correctly set.");
         is(obj.arguments[0], "Hello world!", "Message matches");
         is(obj.consoleID, "", "No consoleID for console API");
 
         // We want to see 2 messages from this innerID, the first is generated
         // by console.log, the second one from createInstance().log();
         ++this.count;
-      } else if (obj.innerID = "CUSTOM INNER") {
+      } else if (obj.innerID == "CUSTOM INNER") {
         is(obj.ID, "jsm", "ID and InnerID are correctly set.");
         is(obj.arguments[0], "Hello world!", "Message matches");
         is(obj.consoleID, "wow", "consoleID is set by consoleInstance");
         ++this.count;
+      } else if (obj.innerID == "LEVEL") {
+        // Nothing special... just we don't want to see 'invisible' messages.
+        is(obj.ID, "jsm", "ID and InnerID are correctly set.");
+        is(obj.arguments[0], "Hello world!", "Message matches");
+        ++this.count;
       }
 
       if (this.count == 4) {
         is(dumpCalled, 2, "Dump has been called!");
         SpecialPowers.removeObserver(this, "console-api-log-event");
         SimpleTest.finish();
       }
     }
   }
 }
 function test() {
   SimpleTest.waitForExplicitFinish();
 
-  var cl = new consoleListener();
-  Components.utils.import(JSM);
-  ConsoleTest.go(dumpFunction);
+  SpecialPowers.pushPrefEnv({set: [["pref.test.console", "log"]]}).then(() => {
+    var cl = new consoleListener();
+    Components.utils.import(JSM);
+    ConsoleTest.go(dumpFunction);
+  });
 }
 
   ]]>
   </script>
 
   <body xmlns="http://www.w3.org/1999/xhtml">
   </body>
 </window>
--- a/dom/webidl/Console.webidl
+++ b/dom/webidl/Console.webidl
@@ -157,14 +157,32 @@ interface ConsoleInstance {
 callback ConsoleInstanceDumpCallback = void (DOMString message);
 
 enum ConsoleLogLevel {
   "all", "debug", "log", "info", "clear", "trace", "timeEnd", "time", "group",
   "groupEnd", "profile", "profileEnd", "dir", "dirxml", "warn", "error", "off"
 };
 
 dictionary ConsoleInstanceOptions {
+  // An optional function to intercept all strings written to stdout.
   ConsoleInstanceDumpCallback dump;
+
+  // An optional prefix string to be printed before the actual logged message.
   DOMString prefix = "";
+
+  // An ID representing the source of the message. Normally the inner ID of a
+  // DOM window.
   DOMString innerID = "";
+
+  // String identified for the console, this will be passed through the console
+  // notifications.
   DOMString consoleID = "";
+
+  // Identifier that allows to filter which messages are logged based on their
+  // log level.
   ConsoleLogLevel maxLogLevel;
+
+  // String pref name which contains the level to use for maxLogLevel. If the
+  // pref doesn't exist, gets removed or it is used in workers, the maxLogLevel
+  // will default to the value passed to this constructor (or "all" if it wasn't
+  // specified).
+  DOMString maxLogLevelPref = "";
 };