Bug 1462725 - Add pref-controlled logging to Savant module; r=rhelmer draft
authorBianca Danforth <bdanforth@mozilla.com>
Wed, 23 May 2018 07:14:02 -0700
changeset 803196 cb8ede76462f9bc33f52940edc3be7b698145456
parent 803195 3f4343b9ca0a2a842013b90ba44208b068bdefcb
child 803197 c43501ded79cdf6cfb0cea65f9ae0a4ff179f2ad
child 803319 04e7182ed09a4fb2139859687cb48bd43de6446c
child 803768 5ed589ee26d9411e36d9a0b6a8c970fcbad91acf
child 803780 471166dec28beead83bee08ea0d3f350e09e921c
push id112042
push userbdanforth@mozilla.com
push dateSat, 02 Jun 2018 00:51:23 +0000
reviewersrhelmer
bugs1462725
milestone62.0a1
Bug 1462725 - Add pref-controlled logging to Savant module; r=rhelmer MozReview-Commit-ID: JFMo1s5lajo
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();