Bug 1798651 Part 1: Make SynchronousTask accept a wait interval, and return result...
[gecko.git] / toolkit / content / aboutLogging.js
blob959e5b1fa19dd8ee363355a48f01a089aafd7b56
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 "use strict";
6 const gEnv = Cc["@mozilla.org/process/environment;1"].getService(
7   Ci.nsIEnvironment
8 );
9 const gDashboard = Cc["@mozilla.org/network/dashboard;1"].getService(
10   Ci.nsIDashboard
12 const gDirServ = Cc["@mozilla.org/file/directory_service;1"].getService(
13   Ci.nsIDirectoryServiceProvider
16 function col(element) {
17   let col = document.createElement("td");
18   let content = document.createTextNode(element);
19   col.appendChild(content);
20   return col;
23 let gInited = false;
24 function init() {
25   if (gInited) {
26     return;
27   }
28   gInited = true;
29   gDashboard.enableLogging = true;
31   let setLogButton = document.getElementById("set-log-file-button");
32   setLogButton.addEventListener("click", setLogFile);
34   let setModulesButton = document.getElementById("set-log-modules-button");
35   setModulesButton.addEventListener("click", setLogModules);
37   let startLoggingButton = document.getElementById("start-logging-button");
38   startLoggingButton.addEventListener("click", startLogging);
40   let stopLoggingButton = document.getElementById("stop-logging-button");
41   stopLoggingButton.addEventListener("click", stopLogging);
43   try {
44     let file = gDirServ.getFile("TmpD", {});
45     file.append("log.txt");
46     document.getElementById("log-file").value = file.path;
47   } catch (e) {
48     console.error(e);
49   }
51   // Update the value of the log file.
52   updateLogFile();
54   // Update the active log modules
55   updateLogModules();
57   // If we can't set the file and the modules at runtime,
58   // the start and stop buttons wouldn't really do anything.
59   if (setLogButton.disabled && setModulesButton.disabled) {
60     startLoggingButton.disabled = true;
61     stopLoggingButton.disabled = true;
62   }
65 function updateLogFile() {
66   let logPath = "";
68   // Try to get the environment variable for the log file
69   logPath = gEnv.get("MOZ_LOG_FILE") || gEnv.get("NSPR_LOG_FILE");
70   let currentLogFile = document.getElementById("current-log-file");
71   let setLogFileButton = document.getElementById("set-log-file-button");
73   // If the log file was set from an env var, we disable the ability to set it
74   // at runtime.
75   if (logPath.length) {
76     currentLogFile.innerText = logPath;
77     setLogFileButton.disabled = true;
78   } else if (gDashboard.getLogPath() != ".moz_log") {
79     // There may be a value set by a pref.
80     currentLogFile.innerText = gDashboard.getLogPath();
81   } else {
82     try {
83       let file = gDirServ.getFile("TmpD", {});
84       file.append("log.txt");
85       document.getElementById("log-file").value = file.path;
86     } catch (e) {
87       console.error(e);
88     }
89     // Fall back to the temp dir
90     currentLogFile.innerText = document.getElementById("log-file").value;
91   }
93   let openLogFileButton = document.getElementById("open-log-file-button");
94   openLogFileButton.disabled = true;
96   if (currentLogFile.innerText.length) {
97     let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile);
98     file.initWithPath(currentLogFile.innerText);
100     if (file.exists()) {
101       openLogFileButton.disabled = false;
102       openLogFileButton.onclick = function(e) {
103         file.reveal();
104       };
105     }
106   }
109 function updateLogModules() {
110   // Try to get the environment variable for the log file
111   let logModules =
112     gEnv.get("MOZ_LOG") ||
113     gEnv.get("MOZ_LOG_MODULES") ||
114     gEnv.get("NSPR_LOG_MODULES");
115   let currentLogModules = document.getElementById("current-log-modules");
116   let setLogModulesButton = document.getElementById("set-log-modules-button");
117   if (logModules.length) {
118     currentLogModules.innerText = logModules;
119     // If the log modules are set by an environment variable at startup, do not
120     // allow changing them throught a pref. It would be difficult to figure out
121     // which ones are enabled and which ones are not. The user probably knows
122     // what he they are doing.
123     setLogModulesButton.disabled = true;
124   } else {
125     let activeLogModules = [];
126     try {
127       if (Services.prefs.getBoolPref("logging.config.add_timestamp")) {
128         activeLogModules.push("timestamp");
129       }
130     } catch (e) {}
131     try {
132       if (Services.prefs.getBoolPref("logging.config.sync")) {
133         activeLogModules.push("sync");
134       }
135     } catch (e) {}
136     try {
137       if (Services.prefs.getBoolPref("logging.config.profilerstacks")) {
138         activeLogModules.push("profilerstacks");
139       }
140     } catch (e) {}
142     let children = Services.prefs.getBranch("logging.").getChildList("");
144     for (let pref of children) {
145       if (pref.startsWith("config.")) {
146         continue;
147       }
149       try {
150         let value = Services.prefs.getIntPref(`logging.${pref}`);
151         activeLogModules.push(`${pref}:${value}`);
152       } catch (e) {
153         console.error(e);
154       }
155     }
157     currentLogModules.innerText = activeLogModules.join(",");
158   }
161 function setLogFile() {
162   let setLogButton = document.getElementById("set-log-file-button");
163   if (setLogButton.disabled) {
164     // There's no point trying since it wouldn't work anyway.
165     return;
166   }
167   let logFile = document.getElementById("log-file").value.trim();
168   Services.prefs.setCharPref("logging.config.LOG_FILE", logFile);
169   updateLogFile();
172 function clearLogModules() {
173   // Turn off all the modules.
174   let children = Services.prefs.getBranch("logging.").getChildList("");
175   for (let pref of children) {
176     if (!pref.startsWith("config.")) {
177       Services.prefs.clearUserPref(`logging.${pref}`);
178     }
179   }
180   Services.prefs.clearUserPref("logging.config.add_timestamp");
181   Services.prefs.clearUserPref("logging.config.sync");
182   updateLogModules();
185 function setLogModules() {
186   let setLogModulesButton = document.getElementById("set-log-modules-button");
187   if (setLogModulesButton.disabled) {
188     // The modules were set via env var, so we shouldn't try to change them.
189     return;
190   }
192   let modules = document.getElementById("log-modules").value.trim();
194   // Clear previously set log modules.
195   clearLogModules();
197   let logModules = modules.split(",");
198   for (let module of logModules) {
199     if (module == "timestamp") {
200       Services.prefs.setBoolPref("logging.config.add_timestamp", true);
201     } else if (module == "rotate") {
202       // XXX: rotate is not yet supported.
203     } else if (module == "append") {
204       // XXX: append is not yet supported.
205     } else if (module == "sync") {
206       Services.prefs.setBoolPref("logging.config.sync", true);
207     } else if (module == "profilerstacks") {
208       Services.prefs.setBoolPref("logging.config.profilerstacks", true);
209     } else {
210       let lastColon = module.lastIndexOf(":");
211       let key = module.slice(0, lastColon);
212       let value = parseInt(module.slice(lastColon + 1), 10);
213       Services.prefs.setIntPref(`logging.${key}`, value);
214     }
215   }
217   updateLogModules();
220 function startLogging() {
221   setLogFile();
222   setLogModules();
225 function stopLogging() {
226   clearLogModules();
227   // clear the log file as well
228   Services.prefs.clearUserPref("logging.config.LOG_FILE");
229   updateLogFile();
232 // We use the pageshow event instead of onload. This is needed because sometimes
233 // the page is loaded via session-restore/bfcache. In such cases we need to call
234 // init() to keep the page behaviour consistent with the ticked checkboxes.
235 // Mostly the issue is with the autorefresh checkbox.
236 window.addEventListener("pageshow", function() {
237   init();