Bug 1134846 - Add helpers to create loggers that follow the signon.debug pref. r=dolske draft
authorMatthew Noorenberghe <mozilla@noorenberghe.ca>
Fri, 06 Mar 2015 14:09:35 -0800
changeset 249001 4692b08e462e0f5b4dfae3d6e58dda0f0d1dfede
parent 249000 ce2df4cdd681e4fbed22f09d4b843350f7cc9fd4
child 249002 03bb184d12e28cf194817c3789cbae4b20e9a8f3
push id965
push usermozilla@noorenberghe.ca
push dateTue, 10 Mar 2015 01:14:45 +0000
reviewersdolske
bugs1134846
milestone39.0a1
Bug 1134846 - Add helpers to create loggers that follow the signon.debug pref. r=dolske
toolkit/components/passwordmgr/LoginHelper.jsm
--- a/toolkit/components/passwordmgr/LoginHelper.jsm
+++ b/toolkit/components/passwordmgr/LoginHelper.jsm
@@ -18,26 +18,60 @@ this.EXPORTED_SYMBOLS = [
   "LoginHelper",
 ];
 
 ////////////////////////////////////////////////////////////////////////////////
 //// Globals
 
 const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
 
+Cu.import("resource://gre/modules/Services.jsm");
 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 
 ////////////////////////////////////////////////////////////////////////////////
 //// LoginHelper
 
 /**
  * Contains functions shared by different Login Manager components.
  */
 this.LoginHelper = {
   /**
+   * Map of log prefixes to loggers for that prefix so we can change the log level at runtime.
+   */
+  _loggers: new Map(),
+
+  debug: false,
+
+  _onPrefChange() {
+    this.debug = Services.prefs.getBoolPref("signon.debug");
+    for (let logger of this._loggers.values()) {
+      logger.maxLogLevel = this.debug ? "debug" : "error";
+    }
+  },
+
+  _createLogger(logPrefix) {
+    // Create a new instance of the ConsoleAPI so we can control the maxLogLevel with a pref.
+    let ConsoleAPI = Cu.import("resource://gre/modules/devtools/Console.jsm", {}).ConsoleAPI;
+    let consoleOptions = {
+      maxLogLevel: this.debug ? "debug" : "error",
+      prefix: logPrefix,
+    };
+    return new ConsoleAPI(consoleOptions);
+  },
+
+  getLogger(logPrefix) {
+    let logger = this._loggers.get(logPrefix);
+    if (!logger) {
+      logger = this._createLogger(logPrefix);
+      this._loggers.set(logPrefix, logger);
+    }
+    return logger;
+  },
+
+  /**
    * 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.
    */
   checkHostnameValue: function (aHostname)
   {
@@ -226,8 +260,11 @@ this.LoginHelper = {
     }
 
     // Throws if there are bogus values.
     this.checkLoginValues(newLogin);
 
     return newLogin;
   },
 };
+
+Services.prefs.addObserver("signon.", this.LoginHelper._onPrefChange.bind(this.LoginHelper), false);
+this.LoginHelper._onPrefChange(); // read initial values