Skip to content

Commit 13ffecd

Browse files
committed
Bug 1874957 - Add a shouldLog function to ConsoleInstance. r=dom-core,webidl,saschanaz,peterv
Differential Revision: https://phabricator.services.mozilla.com/D199796
1 parent be3d23d commit 13ffecd

File tree

5 files changed

+49
-0
lines changed

5 files changed

+49
-0
lines changed

dom/console/ConsoleInstance.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
66

77
#include "mozilla/dom/ConsoleInstance.h"
8+
#include "Console.h"
89
#include "mozilla/dom/ConsoleBinding.h"
910
#include "mozilla/Preferences.h"
1011
#include "ConsoleCommon.h"
@@ -243,6 +244,11 @@ void ConsoleInstance::Clear(JSContext* aCx) {
243244
console->MethodInternal(aCx, Console::MethodClear, u"clear"_ns, data);
244245
}
245246

247+
bool ConsoleInstance::ShouldLog(ConsoleLogLevel aLevel) {
248+
return mConsole->mCurrentLogLevel <=
249+
mConsole->WebIDLLogLevelToInteger(aLevel);
250+
}
251+
246252
void ConsoleInstance::ReportForServiceWorkerScope(const nsAString& aScope,
247253
const nsAString& aMessage,
248254
const nsAString& aFilename,

dom/console/ConsoleInstance.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ class ConsoleInstance final : public nsISupports, public nsWrapperCache {
9696
MOZ_CAN_RUN_SCRIPT
9797
void Clear(JSContext* aCx);
9898

99+
bool ShouldLog(ConsoleLogLevel aLevel);
100+
99101
// For testing only.
100102
void ReportForServiceWorkerScope(const nsAString& aScope,
101103
const nsAString& aMessage,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* Any copyright is dedicated to the Public Domain.
2+
http://creativecommons.org/publicdomain/zero/1.0/ */
3+
4+
add_task(async function test_shouldLog_maxLogLevel() {
5+
let ci = console.createInstance({ maxLogLevel: "Warn" });
6+
7+
Assert.ok(
8+
ci.shouldLog("Error"),
9+
"Should return true for logging a higher level"
10+
);
11+
Assert.ok(
12+
ci.shouldLog("Warn"),
13+
"Should return true for logging the same level"
14+
);
15+
Assert.ok(
16+
!ci.shouldLog("Debug"),
17+
"Should return false for logging a lower level;"
18+
);
19+
});
20+
21+
add_task(async function test_shouldLog_maxLogLevelPref() {
22+
Services.prefs.setStringPref("test.log", "Warn");
23+
let ci = console.createInstance({ maxLogLevelPref: "test.log" });
24+
25+
Assert.ok(
26+
!ci.shouldLog("Debug"),
27+
"Should return false for logging a lower level;"
28+
);
29+
30+
Services.prefs.setStringPref("test.log", "Debug");
31+
Assert.ok(
32+
ci.shouldLog("Debug"),
33+
"Should return true for logging a lower level after pref update"
34+
);
35+
});

dom/console/tests/xpcshell/xpcshell.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ support-files = ""
44

55
["test_basic.js"]
66

7+
["test_console_shouldLog.js"]
8+
79
["test_failing_console_listener.js"]
810

911
["test_formatting.js"]

dom/webidl/Console.webidl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ interface ConsoleInstance {
198198

199199
undefined profile(any... data);
200200
undefined profileEnd(any... data);
201+
202+
// Returns true if the given level would log a message. Used for avoiding
203+
// long/significant processing when logging messages.
204+
boolean shouldLog(ConsoleLogLevel level);
201205
};
202206

203207
callback ConsoleInstanceDumpCallback = undefined (DOMString message);

0 commit comments

Comments
 (0)