Bug 1169290 - Define log level conversion table in function scope. r?maja_zf draft
authorAndreas Tolfsen <ato@sny.no>
Sat, 27 Jan 2018 18:00:40 +0000
changeset 753890 7e664c78991e82af54cee3470cc7f970a19dc5f1
parent 753889 06113a1109dc0c223a4e8ecb1c82c0a3413d697a
child 753891 f49e626aac8b73bdb2f1ba53eb4c90cbb6deb4a0
push id98715
push userbmo:ato@sny.no
push dateMon, 12 Feb 2018 16:37:16 +0000
reviewersmaja_zf
bugs1169290
milestone60.0a1
Bug 1169290 - Define log level conversion table in function scope. r?maja_zf Instead of assigning a Map object to the global scope, this patch moves the table to be initialised inside LogLevel.get. This would cause a slight detriment to performance if it was called a lot, but it isn't, so we instead free up some memory by letting us free the map sooner. MozReview-Commit-ID: 9U4q71d57A
testing/marionette/components/marionette.js
--- a/testing/marionette/components/marionette.js
+++ b/testing/marionette/components/marionette.js
@@ -23,37 +23,16 @@ const MARIONETTE_CONTRACT_ID = "@mozilla
 const MARIONETTE_CID = Components.ID("{786a1369-dca5-4adc-8486-33d23c88010a}");
 
 const PREF_PORT = "marionette.port";
 const PREF_PORT_FALLBACK = "marionette.defaultPrefs.port";
 const PREF_LOG_LEVEL = "marionette.log.level";
 const PREF_LOG_LEVEL_FALLBACK = "marionette.logging";
 
 const DEFAULT_LOG_LEVEL = "info";
-const LOG_LEVELS = new class extends Map {
-  constructor() {
-    super([
-      ["fatal", Log.Level.Fatal],
-      ["error", Log.Level.Error],
-      ["warn", Log.Level.Warn],
-      ["info", Log.Level.Info],
-      ["config", Log.Level.Config],
-      ["debug", Log.Level.Debug],
-      ["trace", Log.Level.Trace],
-    ]);
-  }
-
-  get(level) {
-    let s = String(level).toLowerCase();
-    if (!this.has(s)) {
-      return DEFAULT_LOG_LEVEL;
-    }
-    return super.get(s);
-  }
-};
 
 // Complements -marionette flag for starting the Marionette server.
 // We also set this if Marionette is running in order to start the server
 // again after a Firefox restart.
 const ENV_ENABLED = "MOZ_MARIONETTE";
 
 // Besides starting based on existing prefs in a profile and a command
 // line flag, we also support inheriting prefs out of an env var, and to
@@ -63,16 +42,35 @@ const ENV_ENABLED = "MOZ_MARIONETTE";
 // a different profile in order to test things like Firefox refresh.
 // The environment variable itself, if present, is interpreted as a
 // JSON structure, with the keys mapping to preference names in the
 // "marionette." branch, and the values to the values of those prefs. So
 // something like {"port": 4444} would result in the marionette.port
 // pref being set to 4444.
 const ENV_PRESERVE_PREFS = "MOZ_MARIONETTE_PREF_STATE_ACROSS_RESTARTS";
 
+const LogLevel = {
+  get(level) {
+    let levels = new Map([
+      ["fatal", Log.Level.Fatal],
+      ["error", Log.Level.Error],
+      ["warn", Log.Level.Warn],
+      ["info", Log.Level.Info],
+      ["config", Log.Level.Config],
+      ["debug", Log.Level.Debug],
+      ["trace", Log.Level.Trace],
+    ]);
+
+    let s = String(level).toLowerCase();
+    if (!levels.has(s)) {
+      return DEFAULT_LOG_LEVEL;
+    }
+    return levels.get(s);
+  },
+};
 
 function getPrefVal(pref) {
   const {PREF_STRING, PREF_BOOL, PREF_INT, PREF_INVALID} = Ci.nsIPrefBranch;
 
   let type = Services.prefs.getPrefType(pref);
   switch (type) {
     case PREF_STRING:
       return Services.prefs.getStringPref(pref);
@@ -108,17 +106,17 @@ function getPref(preferred, fallback) {
 // This shim can be removed when Firefox 55 ships.
 const prefs = {
   get port() {
     return getPref(PREF_PORT, PREF_PORT_FALLBACK);
   },
 
   get logLevel() {
     let s = getPref(PREF_LOG_LEVEL, PREF_LOG_LEVEL_FALLBACK);
-    return LOG_LEVELS.get(s);
+    return LogLevel.get(s);
   },
 
   readFromEnvironment(key) {
     const env = Cc["@mozilla.org/process/environment;1"]
         .getService(Ci.nsIEnvironment);
 
     if (env.exists(key)) {
       let prefs;