Bug 1470333: Part 2 - Avoid creating ConsoleAPI instances when debug logging is not enabled. r?MattN
MozReview-Commit-ID: Edck1SgCcDA
--- a/toolkit/components/passwordmgr/LoginHelper.jsm
+++ b/toolkit/components/passwordmgr/LoginHelper.jsm
@@ -35,34 +35,61 @@ var LoginHelper = {
schemeUpgrades: Services.prefs.getBoolPref("signon.schemeUpgrades"),
insecureAutofill: Services.prefs.getBoolPref("signon.autofillForms.http"),
createLogger(aLogPrefix) {
let getMaxLogLevel = () => {
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.", () => {
this.debug = Services.prefs.getBoolPref("signon.debug");
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);
+ }
+ },
+ };
},
/**
* Due to the way the signons2.txt file is formatted, we need to make
* sure certain field values or characters do not cause the file to
* be parsed incorrectly. Reject hostnames that we can't store correctly.
*
* @throws String with English message in case validation failed.