Bug 1470333: Part 2 - Avoid creating ConsoleAPI instances when debug logging is not enabled. r?MattN draft
authorKris Maglione <maglione.k@gmail.com>
Mon, 25 Jun 2018 20:03:19 -0700
changeset 812910 e67160db3c8a9ff5fe0b7ea96ad11c42238c6fac
parent 812909 f7bb68c8e866e393b97e6672a30b4beddc448997
push id114701
push usermaglione.k@gmail.com
push dateSat, 30 Jun 2018 23:09:19 +0000
reviewersMattN
bugs1470333
milestone63.0a1
Bug 1470333: Part 2 - Avoid creating ConsoleAPI instances when debug logging is not enabled. r?MattN MozReview-Commit-ID: Edck1SgCcDA
toolkit/components/passwordmgr/LoginHelper.jsm
--- a/toolkit/components/passwordmgr/LoginHelper.jsm
+++ b/toolkit/components/passwordmgr/LoginHelper.jsm
@@ -35,34 +35,61 @@ var LoginHelper = {
   schemeUpgrades: Services.prefs.getBoolPref("signon.schemeUpgrades"),
   insecureAutofill: Services.prefs.getBoolPref("signon.autofillForms.http"),
 
   createLogger(aLogPrefix) {
     let getMaxLogLevel = () => {
       return this.debug ? "debug" : "warn";
     };
 
-    // Create a new instance of the ConsoleAPI so we can control the maxLogLevel with a pref.
-    let ConsoleAPI = ChromeUtils.import("resource://gre/modules/Console.jsm", {}).ConsoleAPI;
-    let consoleOptions = {
-      maxLogLevel: getMaxLogLevel(),
-      prefix: aLogPrefix,
-    };
-    let logger = new ConsoleAPI(consoleOptions);
+    let logger;
+    function getConsole() {
+      if (!logger) {
+        // Create a new instance of the ConsoleAPI so we can control the maxLogLevel with a pref.
+        let ConsoleAPI = ChromeUtils.import("resource://gre/modules/Console.jsm", {}).ConsoleAPI;
+        let consoleOptions = {
+          maxLogLevel: getMaxLogLevel(),
+          prefix: aLogPrefix,
+        };
+        logger = new ConsoleAPI(consoleOptions);
+      }
+      return logger;
+    }
 
     // Watch for pref changes and update this.debug and the maxLogLevel for created loggers
     Services.prefs.addObserver("signon.", () => {
       this.debug = Services.prefs.getBoolPref("signon.debug");
       this.formlessCaptureEnabled = Services.prefs.getBoolPref("signon.formlessCapture.enabled");
       this.schemeUpgrades = Services.prefs.getBoolPref("signon.schemeUpgrades");
       this.insecureAutofill = Services.prefs.getBoolPref("signon.autofillForms.http");
-      logger.maxLogLevel = getMaxLogLevel();
+      if (logger) {
+        logger.maxLogLevel = getMaxLogLevel();
+      }
     });
 
-    return logger;
+    return {
+      log: (...args) => {
+        if (this.debug) {
+          getConsole().log(...args);
+        }
+      },
+      error: (...args) => {
+        getConsole().error(...args);
+      },
+      debug: (...args) => {
+        if (this.debug) {
+          getConsole().debug(...args);
+        }
+      },
+      warn: (...args) => {
+        if (this.debug) {
+          getConsole().warn(...args);
+        }
+      },
+    };
   },
 
   /**
    * Due to the way the signons2.txt file is formatted, we need to make
    * sure certain field values or characters do not cause the file to
    * be parsed incorrectly.  Reject hostnames that we can't store correctly.
    *
    * @throws String with English message in case validation failed.