Bug 1172141 - Add a maxLogLevelPref option to the ConsoleAPI constructor to easily control log levels with a preference. r=bgrins

This commit is contained in:
Matthew Noorenberghe 2015-06-08 00:46:18 -07:00
parent a4c7873c17
commit a0eb5da53e
3 changed files with 153 additions and 3 deletions

View File

@ -7,6 +7,15 @@
"use strict";
function onNewMessage(aEvent, aNewMessages) {
for (let msg of aNewMessages) {
// Messages that shouldn't be output contain the substring FAIL_TEST
if (msg.node.textContent.includes("FAIL_TEST")) {
ok(false, "Message shouldn't have been output: " + msg.node.textContent);
}
}
};
add_task(function*() {
let storage = Cc["@mozilla.org/consoleAPI-storage;1"].getService(Ci.nsIConsoleAPIStorage);
storage.clearEvents();
@ -149,10 +158,10 @@ add_task(function* test_prefix() {
};
let console2 = new ConsoleAPI(consoleOptions);
console2.error("Testing a prefix");
console2.log("Below the maxLogLevel");
console2.log("FAIL_TEST: Below the maxLogLevel");
let hud = yield HUDService.toggleBrowserConsole();
hud.ui.on("new-messages", onNewMessage);
yield waitForMessages({
webconsole: hud,
messages: [{
@ -164,5 +173,109 @@ add_task(function* test_prefix() {
});
hud.jsterm.clearOutput(true);
hud.ui.off("new-messages", onNewMessage);
yield HUDService.toggleBrowserConsole();
});
add_task(function* test_maxLogLevelPref_missing() {
let storage = Cc["@mozilla.org/consoleAPI-storage;1"].getService(Ci.nsIConsoleAPIStorage);
storage.clearEvents();
let {ConsoleAPI} = Cu.import("resource://gre/modules/devtools/Console.jsm", {});
let consoleOptions = {
maxLogLevel: "error",
maxLogLevelPref: "testing.maxLogLevel",
};
let console = new ConsoleAPI(consoleOptions);
is(Services.prefs.getPrefType(consoleOptions.maxLogLevelPref),
Services.prefs.PREF_INVALID,
"Check log level pref is missing");
// Since the maxLogLevelPref doesn't exist, we should fallback to the passed
// maxLogLevel of "error".
console.warn("FAIL_TEST: Below the maxLogLevel");
console.error("Error should be shown");
let hud = yield HUDService.toggleBrowserConsole();
hud.ui.on("new-messages", onNewMessage);
yield waitForMessages({
webconsole: hud,
messages: [{
name: "defaulting to error level",
severity: SEVERITY_ERROR,
text: "Error should be shown",
}],
});
hud.jsterm.clearOutput(true);
hud.ui.off("new-messages", onNewMessage);
yield HUDService.toggleBrowserConsole();
});
add_task(function* test_maxLogLevelPref() {
let storage = Cc["@mozilla.org/consoleAPI-storage;1"].getService(Ci.nsIConsoleAPIStorage);
storage.clearEvents();
let {ConsoleAPI} = Cu.import("resource://gre/modules/devtools/Console.jsm", {});
let consoleOptions = {
maxLogLevel: "error",
maxLogLevelPref: "testing.maxLogLevel",
};
info("Setting the pref to warn");
Services.prefs.setCharPref(consoleOptions.maxLogLevelPref, "Warn");
let console = new ConsoleAPI(consoleOptions);
is(console.maxLogLevel, "warn", "Check pref was read at initialization");
console.info("FAIL_TEST: info is below the maxLogLevel");
console.error("Error should be shown");
console.warn("Warn should be shown due to the initial pref value");
info("Setting the pref to info");
Services.prefs.setCharPref(consoleOptions.maxLogLevelPref, "INFO");
is(console.maxLogLevel, "info", "Check pref was lowercased");
console.info("info should be shown due to the pref change being observed");
info("Clearing the pref");
Services.prefs.clearUserPref(consoleOptions.maxLogLevelPref);
console.warn("FAIL_TEST: Shouldn't be shown due to defaulting to error");
console.error("Should be shown due to defaulting to error");
let hud = yield HUDService.toggleBrowserConsole();
hud.ui.on("new-messages", onNewMessage);
yield waitForMessages({
webconsole: hud,
messages: [{
name: "error > warn",
severity: SEVERITY_ERROR,
text: "Error should be shown",
},
{
name: "warn is the inital pref value",
severity: SEVERITY_WARNING,
text: "Warn should be shown due to the initial pref value",
},
{
name: "pref changed to info",
severity: SEVERITY_INFO,
text: "info should be shown due to the pref change being observed",
},
{
name: "default to intial maxLogLevel if pref is removed",
severity: SEVERITY_ERROR,
text: "Should be shown due to defaulting to error",
}],
});
hud.jsterm.clearOutput(true);
hud.ui.off("new-messages", onNewMessage);
yield HUDService.toggleBrowserConsole();
});

View File

@ -907,6 +907,7 @@ function openDebugger(aOptions = {})
* - source: object of the shape { url, line }. This is used to
* match the source URL and line number of the error message or
* console API call.
* - prefix: prefix text to check for in the prefix element.
* - stacktrace: array of objects of the form { file, fn, line } that
* can match frames in the stacktrace associated with the message.
* - groupDepth: number used to check the depth of the message in

View File

@ -581,6 +581,11 @@ function sendConsoleAPIMessage(aConsole, aLevel, aFrame, aArgs, aOptions = {})
* If falsy value, all messages will be logged.
* If wrong value that doesn't match any key of
* LOG_LEVELS, no message will be logged
* - maxLogLevelPref {string} : String pref name which contains the
* level to use for maxLogLevel. If the pref doesn't
* exist or gets removed, the maxLogLevel will default
* to the value passed to this constructor (or "all"
* if it wasn't specified).
* - dump {function} : An optional function to intercept all strings
* written to stdout
* - innerID {string}: An ID representing the source of the message.
@ -595,10 +600,24 @@ function ConsoleAPI(aConsoleOptions = {}) {
// in order to avoid runtime checks on each console method call.
this.dump = aConsoleOptions.dump || dump;
this.prefix = aConsoleOptions.prefix || "";
this.maxLogLevel = aConsoleOptions.maxLogLevel || "all";
this.maxLogLevel = aConsoleOptions.maxLogLevel;
this.innerID = aConsoleOptions.innerID || null;
this.consoleID = aConsoleOptions.consoleID || "";
// Setup maxLogLevelPref watching
let updateMaxLogLevel = () => {
if (Services.prefs.getPrefType(aConsoleOptions.maxLogLevelPref) == Services.prefs.PREF_STRING) {
this._maxLogLevel = Services.prefs.getCharPref(aConsoleOptions.maxLogLevelPref).toLowerCase();
} else {
this._maxLogLevel = this._maxExplicitLogLevel;
}
};
if (aConsoleOptions.maxLogLevelPref) {
updateMaxLogLevel();
Services.prefs.addObserver(aConsoleOptions.maxLogLevelPref, updateMaxLogLevel, false);
}
// Bind all the functions to this object.
for (let prop in this) {
if (typeof(this[prop]) === "function") {
@ -608,6 +627,15 @@ function ConsoleAPI(aConsoleOptions = {}) {
}
ConsoleAPI.prototype = {
/**
* The last log level that was specified via the constructor or setter. This
* is used as a fallback if the pref doesn't exist or is removed.
*/
_maxExplicitLogLevel: null,
/**
* The current log level via all methods of setting (pref or via the API).
*/
_maxLogLevel: null,
debug: createMultiLineDumper("debug"),
log: createDumper("log"),
info: createDumper("info"),
@ -655,6 +683,14 @@ ConsoleAPI.prototype = {
dumpMessage(this, "timeEnd",
"'" + timer.name + "' " + timer.duration + "ms");
},
get maxLogLevel() {
return this._maxLogLevel || "all";
},
set maxLogLevel(aValue) {
this._maxLogLevel = this._maxExplicitLogLevel = aValue;
},
};
this.console = new ConsoleAPI();