Bug 1306921 - Add buttons to start-stop logging to about:networking r=jaws
authorValentin Gosu <valentin.gosu@gmail.com>
Thu, 06 Oct 2016 17:18:36 +0200
changeset 316787 651f1169e0f94c17df7020d9c5a7885ecdb64578
parent 316786 8cca966520005440b8f3a2219f702655e4032897
child 316788 f69e779a1903080a67621df6f22c236912cdca82
push id82540
push uservalentin.gosu@gmail.com
push dateThu, 06 Oct 2016 15:20:20 +0000
treeherdermozilla-inbound@651f1169e0f9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjaws
bugs1306921
milestone52.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1306921 - Add buttons to start-stop logging to about:networking r=jaws
toolkit/content/aboutNetworking.js
toolkit/content/aboutNetworking.xhtml
toolkit/locales/en-US/chrome/global/aboutNetworking.dtd
--- a/toolkit/content/aboutNetworking.js
+++ b/toolkit/content/aboutNetworking.js
@@ -177,29 +177,42 @@ function init() {
   });
 
   let setLogButton = document.getElementById("set-log-file-button");
   setLogButton.addEventListener("click", setLogFile);
 
   let setModulesButton = document.getElementById("set-log-modules-button");
   setModulesButton.addEventListener("click", setLogModules);
 
+  let startLoggingButton = document.getElementById("start-logging-button");
+  startLoggingButton.addEventListener("click", startLogging);
+
+  let stopLoggingButton = document.getElementById("stop-logging-button");
+  stopLoggingButton.addEventListener("click", stopLogging);
+
   try {
     let file = gDirServ.getFile("TmpD",  {});
     file.append("log.txt");
     document.getElementById("log-file").value = file.path;
   } catch (e) {
     console.error(e);
   }
 
   // Update the value of the log file.
   updateLogFile();
 
   // Update the active log modules
   updateLogModules();
+
+  // If we can't set the file and the modules at runtime,
+  // the start and stop buttons wouldn't really do anything.
+  if (setLogButton.disabled && setModulesButton.disabled) {
+    startLoggingButton.disabled = true;
+    stopLoggingButton.disabled = true;
+  }
 }
 
 function updateLogFile() {
   let logPath = "";
 
   // Try to get the environment variable for the log file
   logPath = gEnv.get("MOZ_LOG_FILE") || gEnv.get("NSPR_LOG_FILE");
   let currentLogFile = document.getElementById("current-log-file");
@@ -208,17 +221,16 @@ function updateLogFile() {
   // If the log file was set from an env var, we disable the ability to set it
   // at runtime.
   if (logPath.length > 0) {
     currentLogFile.innerText = logPath;
     setLogFileButton.disabled = true;
   } else {
     // There may be a value set by a pref.
     currentLogFile.innerText = gDashboard.getLogPath();
-    setLogFileButton.disabled = false;
   }
 }
 
 function updateLogModules() {
   // Try to get the environment variable for the log file
   let logModules = gEnv.get("MOZ_LOG") ||
                    gEnv.get("MOZ_LOG_MODULES") ||
                    gEnv.get("NSPR_LOG_MODULES");
@@ -259,60 +271,82 @@ function updateLogModules() {
       }
     }
 
     currentLogModules.innerText = activeLogModules.join(",");
   }
 }
 
 function setLogFile() {
+  let setLogButton = document.getElementById("set-log-file-button");
+  if (setLogButton.disabled) {
+    // There's no point trying since it wouldn't work anyway.
+    return;
+  }
   let logFile = document.getElementById("log-file").value.trim();
   Services.prefs.setCharPref("logging.config.LOG_FILE", logFile);
   updateLogFile();
 }
 
+function clearLogModules() {
+  // Turn off all the modules.
+  let children = Services.prefs.getBranch("logging.").getChildList("", {});
+  for (let pref of children) {
+    if (!pref.startsWith("config.")) {
+      Services.prefs.clearUserPref(`logging.${pref}`);
+    }
+  }
+  Services.prefs.clearUserPref("logging.config.add_timestamp");
+  Services.prefs.clearUserPref("logging.config.sync");
+  updateLogModules();
+}
+
 function setLogModules() {
-  let modules = document.getElementById("log-modules").value.trim();
-  if (modules.length == 0) {
-    // Turn off all the modules.
-    let children = Services.prefs.getBranch("logging.").getChildList("", {});
-    for (let pref of children) {
-      if (!pref.startsWith("config.")) {
-        Services.prefs.clearUserPref(`logging.${pref}`);
-      }
-    }
-    Services.prefs.clearUserPref("logging.config.add_timestamp");
-    Services.prefs.clearUserPref("logging.config.sync");
-    updateLogModules();
+  let setLogModulesButton = document.getElementById("set-log-modules-button");
+  if (setLogModulesButton.disabled) {
+    // The modules were set via env var, so we shouldn't try to change them.
     return;
   }
 
+  let modules = document.getElementById("log-modules").value.trim();
+
+  // Clear previously set log modules.
+  clearLogModules();
+
   let logModules = modules.split(",");
-  let isSync = false;
-  let addTimestamp = false;
   for (let module of logModules) {
     if (module == "timestamp") {
-      addTimestamp = true;
+      Services.prefs.setBoolPref("logging.config.add_timestamp", true);
     } else if (module == "rotate") {
       // XXX: rotate is not yet supported.
     } else if (module == "append") {
       // XXX: append is not yet supported.
     } else if (module == "sync") {
-      isSync = true;
+      Services.prefs.setBoolPref("logging.config.sync", true);
     } else {
       let [key, value] = module.split(":");
       Services.prefs.setIntPref(`logging.${key}`, parseInt(value, 10));
     }
   }
-  Services.prefs.setBoolPref("logging.config.add_timestamp", addTimestamp);
-  Services.prefs.setBoolPref("logging.config.sync", isSync);
 
   updateLogModules();
 }
 
+function startLogging() {
+  setLogFile();
+  setLogModules();
+}
+
+function stopLogging() {
+  clearLogModules();
+  // clear the log file as well
+  Services.prefs.clearUserPref("logging.config.LOG_FILE");
+  updateLogFile();
+}
+
 function confirm () {
   let div = document.getElementById("warning_message");
   div.classList.remove("active");
   div.hidden = true;
   let warnBox = document.getElementById("warncheck");
   Services.prefs.setBoolPref("network.warnOnAboutNetworking", warnBox.checked);
 }
 
--- a/toolkit/content/aboutNetworking.xhtml
+++ b/toolkit/content/aboutNetworking.xhtml
@@ -138,23 +138,31 @@
                   <tbody id="dnslookuptool_content" />
               </table>
           </div>
 
           <div id="logging" class="tab" hidden="true">
             <div>
               &aboutNetworking.logTutorial;
             </div>
+            <br/>
+            <div>
+              <button id="start-logging-button"> &aboutNetworking.startLogging; </button>
+              <button id="stop-logging-button"> &aboutNetworking.stopLogging; </button>
+            </div>
+            <br/>
+            <br/>
             <div>
               &aboutNetworking.currentLogFile; <div id="current-log-file"></div><br/>
               <input type="text" name="log-file" id="log-file"></input>
               <button id="set-log-file-button"> &aboutNetworking.setLogFile; </button>
             </div>
             <div>
               &aboutNetworking.currentLogModules; <div id="current-log-modules"></div><br/>
               <input type="text" name="log-modules" id="log-modules" value="timestamp,sync,nsHttp:5,nsSocketTransport:5,nsStreamPump:5,nsHostResolver:5"></input>
               <button id="set-log-modules-button"> &aboutNetworking.setLogModules; </button>
             </div>
           </div>
 
         </div>
     </body>
 </html>
+
--- a/toolkit/locales/en-US/chrome/global/aboutNetworking.dtd
+++ b/toolkit/locales/en-US/chrome/global/aboutNetworking.dtd
@@ -30,12 +30,14 @@
 <!ENTITY aboutNetworking.bytesSent             "Bytes Sent">
 <!ENTITY aboutNetworking.bytesReceived         "Bytes Received">
 <!ENTITY aboutNetworking.logging               "Logging">
 <!ENTITY aboutNetworking.logTutorial           "See <a href='https://developer.mozilla.org/docs/Mozilla/Debugging/HTTP_logging'>HTTP Logging</a> for instructions on how to use this tool.">
 <!ENTITY aboutNetworking.currentLogFile        "Current Log File:">
 <!ENTITY aboutNetworking.currentLogModules     "Current Log Modules:">
 <!ENTITY aboutNetworking.setLogFile            "Set Log File">
 <!ENTITY aboutNetworking.setLogModules         "Set Log Modules">
+<!ENTITY aboutNetworking.startLogging          "Start Logging">
+<!ENTITY aboutNetworking.stopLogging           "Stop Logging">
 <!ENTITY aboutNetworking.dnsLookup             "DNS Lookup">
 <!ENTITY aboutNetworking.dnsLookupButton       "Resolve">
 <!ENTITY aboutNetworking.dnsDomain             "Domain">
 <!ENTITY aboutNetworking.dnsLookupTableColumn  "IPs">