Bug 1470333: Part 2 - Avoid creating ConsoleAPI instances when debug logging is not enabled. r=MattN

MozReview-Commit-ID: Edck1SgCcDA

--HG--
extra : rebase_source : 1152d01ba7456d06a6f66ac65ceba2ef85e4747d
This commit is contained in:
Kris Maglione 2018-07-04 12:43:21 -07:00
parent 0b96fa7467
commit 5540b0b58f
2 changed files with 36 additions and 10 deletions

View File

@ -42,7 +42,6 @@ const whitelist = {
"resource://gre/modules/XPCOMUtils.jsm",
// Logging related
"resource://gre/modules/Console.jsm", // bug 1470333
"resource://gre/modules/Log.jsm",
// Session store

View File

@ -40,13 +40,19 @@ var LoginHelper = {
return this.debug ? "debug" : "warn";
};
// Create a new instance of the ConsoleAPI so we can control the maxLogLevel with a pref.
let ConsoleAPI = ChromeUtils.import("resource://gre/modules/Console.jsm", {}).ConsoleAPI;
let consoleOptions = {
maxLogLevel: getMaxLogLevel(),
prefix: aLogPrefix,
};
let logger = new ConsoleAPI(consoleOptions);
let logger;
function getConsole() {
if (!logger) {
// Create a new instance of the ConsoleAPI so we can control the maxLogLevel with a pref.
let ConsoleAPI = ChromeUtils.import("resource://gre/modules/Console.jsm", {}).ConsoleAPI;
let consoleOptions = {
maxLogLevel: getMaxLogLevel(),
prefix: aLogPrefix,
};
logger = new ConsoleAPI(consoleOptions);
}
return logger;
}
// Watch for pref changes and update this.debug and the maxLogLevel for created loggers
Services.prefs.addObserver("signon.", () => {
@ -54,10 +60,31 @@ var LoginHelper = {
this.formlessCaptureEnabled = Services.prefs.getBoolPref("signon.formlessCapture.enabled");
this.schemeUpgrades = Services.prefs.getBoolPref("signon.schemeUpgrades");
this.insecureAutofill = Services.prefs.getBoolPref("signon.autofillForms.http");
logger.maxLogLevel = getMaxLogLevel();
if (logger) {
logger.maxLogLevel = getMaxLogLevel();
}
});
return logger;
return {
log: (...args) => {
if (this.debug) {
getConsole().log(...args);
}
},
error: (...args) => {
getConsole().error(...args);
},
debug: (...args) => {
if (this.debug) {
getConsole().debug(...args);
}
},
warn: (...args) => {
if (this.debug) {
getConsole().warn(...args);
}
},
};
},
/**