Bug 1429174 - Introducing ConsoleUtils for logging messages to console - message level, r=bkelly
authorAndrea Marchesini <amarchesini@mozilla.com>
Wed, 10 Jan 2018 22:06:38 +0100
changeset 452963 6e74b98b535425ea96ecbf65a391e9b6f3f51bc4
parent 452962 75a5873327a61af0923c83720a1f677d832e2b9c
child 452964 28f876e75d47e8c2843332e631145b6a1acf0263
push id1648
push usermtabara@mozilla.com
push dateThu, 01 Mar 2018 12:45:47 +0000
treeherdermozilla-release@cbb9688c2eeb [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbkelly
bugs1429174
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 1429174 - Introducing ConsoleUtils for logging messages to console - message level, r=bkelly
dom/console/ConsoleInstance.cpp
dom/console/ConsoleInstance.h
dom/console/ConsoleUtils.cpp
dom/console/ConsoleUtils.h
dom/console/tests/xpcshell/test_reportForServiceWorkerScope.js
dom/webidl/Console.webidl
--- a/dom/console/ConsoleInstance.cpp
+++ b/dom/console/ConsoleInstance.cpp
@@ -42,16 +42,33 @@ PrefToValue(const nsCString& aPref)
   if (NS_WARN_IF(index < 0)) {
     return ConsoleLogLevel::All;
   }
 
   MOZ_ASSERT(index < (int)ConsoleLogLevel::EndGuard_);
   return static_cast<ConsoleLogLevel>(index);
 }
 
+ConsoleUtils::Level
+WebIDLevelToConsoleUtilsLevel(ConsoleLevel aLevel)
+{
+  switch (aLevel) {
+    case ConsoleLevel::Log:
+      return ConsoleUtils::eLog;
+    case ConsoleLevel::Warning:
+      return ConsoleUtils::eWarning;
+    case ConsoleLevel::Error:
+      return ConsoleUtils::eError;
+    default:
+      break;
+  }
+
+  return ConsoleUtils::eLog;
+}
+
 } // anonymous
 
 ConsoleInstance::ConsoleInstance(const ConsoleInstanceOptions& aOptions)
   : mConsole(new Console(nullptr))
 {
   mConsole->mConsoleID = aOptions.mConsoleID;
   mConsole->mPassedInnerID = aOptions.mInnerID;
 
@@ -186,18 +203,22 @@ ConsoleInstance::Clear(JSContext* aCx)
                            NS_LITERAL_STRING("clear"), data);
 }
 
 void
 ConsoleInstance::ReportForServiceWorkerScope(const nsAString& aScope,
                                              const nsAString& aMessage,
                                              const nsAString& aFilename,
                                              uint32_t aLineNumber,
-                                             uint32_t aColumnNumber)
+                                             uint32_t aColumnNumber,
+                                             ConsoleLevel aLevel)
 {
-  if (NS_IsMainThread()) {
-    ConsoleUtils::ReportForServiceWorkerScope(aScope, aMessage, aFilename,
-                                              aLineNumber, aColumnNumber);
+  if (!NS_IsMainThread()) {
+    return;
   }
+
+  ConsoleUtils::ReportForServiceWorkerScope(aScope, aMessage, aFilename,
+                                            aLineNumber, aColumnNumber,
+                                            WebIDLevelToConsoleUtilsLevel(aLevel));
 }
 
 } // namespace dom
 } // namespace mozilla
--- a/dom/console/ConsoleInstance.h
+++ b/dom/console/ConsoleInstance.h
@@ -94,17 +94,18 @@ public:
   void
   Clear(JSContext* aCx);
 
   // For testing only.
   void ReportForServiceWorkerScope(const nsAString& aScope,
                                    const nsAString& aMessage,
                                    const nsAString& aFilename,
                                    uint32_t aLineNumber,
-                                   uint32_t aColumnNumber);
+                                   uint32_t aColumnNumber,
+                                   ConsoleLevel aLevel);
 
 private:
   ~ConsoleInstance();
 
   RefPtr<Console> mConsole;
 };
 
 } // dom namespace
--- a/dom/console/ConsoleUtils.cpp
+++ b/dom/console/ConsoleUtils.cpp
@@ -35,35 +35,38 @@ ConsoleUtils::GetOrCreate()
 ConsoleUtils::ConsoleUtils() = default;
 ConsoleUtils::~ConsoleUtils() = default;
 
 /* static */ void
 ConsoleUtils::ReportForServiceWorkerScope(const nsAString& aScope,
                                           const nsAString& aMessage,
                                           const nsAString& aFilename,
                                           uint32_t aLineNumber,
-                                          uint32_t aColumnNumber)
+                                          uint32_t aColumnNumber,
+                                          Level aLevel)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   RefPtr<ConsoleUtils> service = ConsoleUtils::GetOrCreate();
   if (NS_WARN_IF(!service)) {
     return;
   }
 
   service->ReportForServiceWorkerScopeInternal(aScope, aMessage, aFilename,
-                                               aLineNumber, aColumnNumber);
+                                               aLineNumber, aColumnNumber,
+                                               aLevel);
 }
 
 void
 ConsoleUtils::ReportForServiceWorkerScopeInternal(const nsAString& aScope,
                                                   const nsAString& aMessage,
                                                   const nsAString& aFilename,
                                                   uint32_t aLineNumber,
-                                                  uint32_t aColumnNumber)
+                                                  uint32_t aColumnNumber,
+                                                  Level aLevel)
 {
   MOZ_ASSERT(NS_IsMainThread());
 
   AutoJSAPI jsapi;
   jsapi.Init();
 
   JSContext* cx = jsapi.cx();
 
@@ -82,17 +85,30 @@ ConsoleUtils::ReportForServiceWorkerScop
   RootedDictionary<ConsoleEvent> event(cx);
 
   event.mID.Construct();
   event.mID.Value().SetAsString() = aScope;
 
   event.mInnerID.Construct();
   event.mInnerID.Value().SetAsString() = NS_LITERAL_STRING("ServiceWorker");
 
-  event.mLevel = NS_LITERAL_STRING("log");
+  switch (aLevel) {
+    case eLog:
+      event.mLevel = NS_LITERAL_STRING("log");
+      break;
+
+    case eWarning:
+      event.mLevel = NS_LITERAL_STRING("warn");
+      break;
+
+    case eError:
+      event.mLevel = NS_LITERAL_STRING("error");
+      break;
+  }
+
   event.mFilename = aFilename;
   event.mLineNumber = aLineNumber;
   event.mColumnNumber = aColumnNumber;
   event.mTimeStamp = JS_Now() / PR_USEC_PER_MSEC;
 
   JS::Rooted<JS::Value> messageValue(cx);
   if (!dom::ToJSValue(cx, aMessage, &messageValue)) {
     return;
--- a/dom/console/ConsoleUtils.h
+++ b/dom/console/ConsoleUtils.h
@@ -14,40 +14,48 @@
 namespace mozilla {
 namespace dom {
 
 class ConsoleUtils final
 {
 public:
   NS_INLINE_DECL_REFCOUNTING(ConsoleUtils)
 
+  enum Level {
+    eLog,
+    eWarning,
+    eError,
+  };
+
   // Main-thread only, reports a console message from a ServiceWorker.
   static void
   ReportForServiceWorkerScope(const nsAString& aScope,
                               const nsAString& aMessage,
                               const nsAString& aFilename,
                               uint32_t aLineNumber,
-                              uint32_t aColumnNumber);
+                              uint32_t aColumnNumber,
+                              Level aLevel);
 
 private:
   ConsoleUtils();
   ~ConsoleUtils();
 
   static ConsoleUtils*
   GetOrCreate();
 
   JSObject*
   GetOrCreateSandbox(JSContext* aCx);
 
   void
   ReportForServiceWorkerScopeInternal(const nsAString& aScope,
                                       const nsAString& aMessage,
                                       const nsAString& aFilename,
                                       uint32_t aLineNumber,
-                                      uint32_t aColumnNumber);
+                                      uint32_t aColumnNumber,
+                                      Level aLevel);
 
   RefPtr<JSObjectHolder> mSandbox;
 };
 
 } // namespace dom
 } // namespace mozilla
 
 #endif /* mozilla_dom_ConsoleUtils_h */
--- a/dom/console/tests/xpcshell/test_reportForServiceWorkerScope.js
+++ b/dom/console/tests/xpcshell/test_reportForServiceWorkerScope.js
@@ -13,21 +13,22 @@ add_task(async function() {
       observe: function(aSubject, aTopic, aData) {
         let obj = aSubject.wrappedJSObject;
         Assert.ok(obj.arguments[0] === "Hello world!", "Message received!");
         Assert.ok(obj.ID === "scope", "The ID is the scope");
         Assert.ok(obj.innerID === "ServiceWorker", "The innerID is ServiceWorker");
         Assert.ok(obj.filename === "filename", "The filename matches");
         Assert.ok(obj.lineNumber === 42, "The lineNumber matches");
         Assert.ok(obj.columnNumber === 24, "The columnNumber matches");
+        Assert.ok(obj.level === "error", "The level is correct");
 
         Services.obs.removeObserver(this, "console-api-log-event");
         resolve();
       }
     };
 
     new consoleListener();
   });
 
   let ci = console.createInstance();
-  ci.reportForServiceWorkerScope("scope", "Hello world!", "filename", 42, 24);
+  ci.reportForServiceWorkerScope("scope", "Hello world!", "filename", 42, 24, "error");
   await p;
 });
--- a/dom/webidl/Console.webidl
+++ b/dom/webidl/Console.webidl
@@ -182,15 +182,18 @@ dictionary ConsoleInstanceOptions {
 
   // 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 = "";
 };
 
+enum ConsoleLevel { "log", "warning", "error" };
+
 // this interface is just for testing
 partial interface ConsoleInstance {
   [ChromeOnly]
   void reportForServiceWorkerScope(DOMString scope, DOMString message,
                                    DOMString filename, unsigned long lineNumber,
-                                   unsigned long columnNumber);
+                                   unsigned long columnNumber,
+                                   ConsoleLevel level);
 };