Bug 1462725 - Add pref-controlled logging to Savant module; r=rhelmer draft
authorBianca Danforth <bdanforth@mozilla.com>
Mon, 04 Jun 2018 16:18:39 -0700
changeset 803868 c86023e74d2205a58eb28a4fbab1d32c51f3796c
parent 803867 4f5846f1931369f5549c67dcb8cb1584c8888d14
child 803869 a2f757a67a4b638bcfec112dff26ca07e63b1517
push id112222
push userbdanforth@mozilla.com
push dateMon, 04 Jun 2018 23:26:13 +0000
reviewersrhelmer
bugs1462725
milestone62.0a1
Bug 1462725 - Add pref-controlled logging to Savant module; r=rhelmer MozReview-Commit-ID: 7dIMl7vHGr6
browser/modules/ShieldStudySavant.jsm
--- a/browser/modules/ShieldStudySavant.jsm
+++ b/browser/modules/ShieldStudySavant.jsm
@@ -4,16 +4,29 @@
 
 "use strict";
 
 var EXPORTED_SYMBOLS = ["ShieldStudySavant"];
 
 ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
 ChromeUtils.import("resource://gre/modules/Services.jsm");
 
+// See LOG_LEVELS in Console.jsm. Examples: "all", "info", "warn", & "error".
+const PREF_LOG_LEVEL = "shield.savant.loglevel";
+
+// Create a new instance of the ConsoleAPI so we can control the maxLogLevel with a pref.
+XPCOMUtils.defineLazyGetter(this, "log", () => {
+  let ConsoleAPI = ChromeUtils.import("resource://gre/modules/Console.jsm", {}).ConsoleAPI;
+  let consoleOptions = {
+    maxLogLevelPref: PREF_LOG_LEVEL,
+    prefix: "ShieldStudySavant",
+  };
+  return new ConsoleAPI(consoleOptions);
+});
+
 class ShieldStudySavantClass {
   constructor() {
     this.SHIELD_STUDY_SAVANT_PREF = "shield.savant.enabled";
   }
 
   init() {
     // check the pref in case Normandy flipped it on before we could add the pref listener
     this.shouldCollect = Services.prefs.getBoolPref(this.SHIELD_STUDY_SAVANT_PREF);
@@ -32,28 +45,31 @@ class ShieldStudySavantClass {
       } else {
         // Normandy has flipped off the pref
         this.endStudy("expired");
       }
     }
   }
 
   enableCollection() {
+    log.debug("Study has been enabled; turning on data collection.");
     // TODO: enable data collection
   }
 
   endStudy(reason) {
     this.disableCollection();
     // TODO: send endStudy ping with reason code
     this.uninit();
   }
 
   disableCollection() {
+    log.debug("Study has been disabled; turning off data collection.");
     // TODO: disable data collection
   }
 
   uninit() {
     Services.prefs.removeObserver(this.SHIELD_STUDY_SAVANT_PREF, this);
     Services.prefs.clearUserPref(this.SHIELD_STUDY_SAVANT_PREF);
+    Services.prefs.clearUserPref(PREF_LOG_LEVEL);
   }
 };
 
 const ShieldStudySavant = new ShieldStudySavantClass();