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/. */
6 const gEnv = Cc["@mozilla.org/process/environment;1"].getService(
9 const gDashboard = Cc["@mozilla.org/network/dashboard;1"].getService(
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);
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);
44 let file = gDirServ.getFile("TmpD", {});
45 file.append("log.txt");
46 document.getElementById("log-file").value = file.path;
51 // Update the value of the log file.
54 // Update the active log modules
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;
65 function updateLogFile() {
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
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();
83 let file = gDirServ.getFile("TmpD", {});
84 file.append("log.txt");
85 document.getElementById("log-file").value = file.path;
89 // Fall back to the temp dir
90 currentLogFile.innerText = document.getElementById("log-file").value;
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);
101 openLogFileButton.disabled = false;
102 openLogFileButton.onclick = function(e) {
109 function updateLogModules() {
110 // Try to get the environment variable for the log file
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;
125 let activeLogModules = [];
127 if (Services.prefs.getBoolPref("logging.config.add_timestamp")) {
128 activeLogModules.push("timestamp");
132 if (Services.prefs.getBoolPref("logging.config.sync")) {
133 activeLogModules.push("sync");
137 if (Services.prefs.getBoolPref("logging.config.profilerstacks")) {
138 activeLogModules.push("profilerstacks");
142 let children = Services.prefs.getBranch("logging.").getChildList("");
144 for (let pref of children) {
145 if (pref.startsWith("config.")) {
150 let value = Services.prefs.getIntPref(`logging.${pref}`);
151 activeLogModules.push(`${pref}:${value}`);
157 currentLogModules.innerText = activeLogModules.join(",");
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.
167 let logFile = document.getElementById("log-file").value.trim();
168 Services.prefs.setCharPref("logging.config.LOG_FILE", logFile);
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}`);
180 Services.prefs.clearUserPref("logging.config.add_timestamp");
181 Services.prefs.clearUserPref("logging.config.sync");
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.
192 let modules = document.getElementById("log-modules").value.trim();
194 // Clear previously set log modules.
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);
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);
220 function startLogging() {
225 function stopLogging() {
227 // clear the log file as well
228 Services.prefs.clearUserPref("logging.config.LOG_FILE");
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() {