Backed out 2 changesets (bug 1828100) for casuing mochitest failures on Console.cpp. CLOSED TREE

Backed out changeset 86f30e3a63c3 (bug 1828100)
Backed out changeset 32a5be517c83 (bug 1828100)
This commit is contained in:
Tamas Szentpeteri 2024-10-11 16:48:46 +03:00
parent 90ccf26f68
commit f38c45c270
9 changed files with 67 additions and 290 deletions

View File

@ -874,14 +874,11 @@ already_AddRefed<Console> Console::CreateForWorklet(JSContext* aCx,
}
Console::Console(JSContext* aCx, nsIGlobalObject* aGlobal,
uint64_t aOuterWindowID, uint64_t aInnerWindowID,
const nsAString& aPrefix)
uint64_t aOuterWindowID, uint64_t aInnerWindowID)
: mGlobal(aGlobal),
mOuterID(aOuterWindowID),
mInnerID(aInnerWindowID),
mDumpToStdout(false),
mLogModule(nullptr),
mPrefix(aPrefix),
mChromeInstance(false),
mCurrentLogLevel(WebIDLLogLevelToInteger(ConsoleLogLevel::All)),
mStatus(eUnknown),
@ -893,13 +890,6 @@ Console::Console(JSContext* aCx, nsIGlobalObject* aGlobal,
mDumpToStdout = StaticPrefs::devtools_console_stdout_content();
}
// By default, the console uses "console" MOZ_LOG module name,
// but ConsoleInstance may pass a custom prefix which we will use a module
// name.
mLogModule = mPrefix.IsEmpty()
? LogModule::Get("console")
: LogModule::Get(NS_ConvertUTF16toUTF8(mPrefix).get());
mozilla::HoldJSObjects(this);
}
@ -1136,8 +1126,7 @@ void Console::ProfileMethodInternal(JSContext* aCx, MethodName aMethodName,
return;
}
MaybeExecuteDumpFunction(aCx, aMethodName, aAction, aData, nullptr,
DOMHighResTimeStamp(0.0));
MaybeExecuteDumpFunction(aCx, aAction, aData, nullptr);
if (WorkletThread::IsOnWorkletThread()) {
RefPtr<ConsoleProfileWorkletRunnable> runnable =
@ -1407,14 +1396,14 @@ void Console::MethodInternal(JSContext* aCx, MethodName aMethodName,
// Before processing this CallData differently, it's time to call the dump
// function.
//
// Only log the stack trace for console.trace() and console.assert()
if (aMethodName == MethodTrace || aMethodName == MethodAssert) {
MaybeExecuteDumpFunction(aCx, aMethodName, aMethodString, aData, stack,
monotonicTimer);
MaybeExecuteDumpFunction(aCx, aMethodString, aData, stack);
} else if ((aMethodName == MethodTime || aMethodName == MethodTimeEnd) &&
!aData.IsEmpty()) {
MaybeExecuteDumpFunctionForTime(aCx, aMethodName, aMethodString,
monotonicTimer, aData[0]);
} else {
MaybeExecuteDumpFunction(aCx, aMethodName, aMethodString, aData, nullptr,
monotonicTimer);
MaybeExecuteDumpFunction(aCx, aMethodString, aData, nullptr);
}
if (NS_IsMainThread()) {
@ -2701,45 +2690,20 @@ void Console::StringifyElement(Element* aElement, nsAString& aOut) {
aOut.AppendLiteral(">");
}
void Console::MaybeExecuteDumpFunction(JSContext* aCx, MethodName aMethodName,
const nsAString& aMethodString,
void Console::MaybeExecuteDumpFunction(JSContext* aCx,
const nsAString& aMethodName,
const Sequence<JS::Value>& aData,
nsIStackFrame* aStack,
DOMHighResTimeStamp aMonotonicTimer) {
if (mLogModule->ShouldLog(InternalLogLevelToMozLog(aMethodName))) {
nsString message = GetDumpMessage(aCx, aMethodName, aMethodString, aData,
aStack, aMonotonicTimer, true);
MOZ_LOG(mLogModule, InternalLogLevelToMozLog(aMethodName),
("%s", NS_ConvertUTF16toUTF8(message).get()));
}
nsIStackFrame* aStack) {
if (!mDumpFunction && !mDumpToStdout) {
return;
}
nsString message = GetDumpMessage(aCx, aMethodName, aMethodString, aData,
aStack, aMonotonicTimer, false);
ExecuteDumpFunction(message);
}
nsString Console::GetDumpMessage(JSContext* aCx, MethodName aMethodName,
const nsAString& aMethodString,
const Sequence<JS::Value>& aData,
nsIStackFrame* aStack,
DOMHighResTimeStamp aMonotonicTimer,
bool aIsForMozLog) {
nsString message;
// MOZ_LOG already logs either console or the prefix
if (!aIsForMozLog) {
message.AssignLiteral("console.");
} else {
message.AssignLiteral("");
}
message.Append(aMethodString);
nsAutoString message;
message.AssignLiteral("console.");
message.Append(aMethodName);
message.AppendLiteral(": ");
if (!aIsForMozLog && !mPrefix.IsEmpty()) {
if (!mPrefix.IsEmpty()) {
message.Append(mPrefix);
message.AppendLiteral(": ");
}
@ -2764,7 +2728,7 @@ nsString Console::GetDumpMessage(JSContext* aCx, MethodName aMethodName,
nsAutoJSString string;
if (NS_WARN_IF(!string.init(aCx, jsString))) {
return message;
return;
}
if (i != 0) {
@ -2774,11 +2738,6 @@ nsString Console::GetDumpMessage(JSContext* aCx, MethodName aMethodName,
message.Append(string);
}
if (aMethodName == MethodTime || aMethodName == MethodTimeEnd) {
message.AppendLiteral(" @ ");
message.AppendFloat(aMonotonicTimer);
}
message.AppendLiteral("\n");
// aStack can be null.
@ -2810,7 +2769,45 @@ nsString Console::GetDumpMessage(JSContext* aCx, MethodName aMethodName,
stack.swap(caller);
}
return message;
ExecuteDumpFunction(message);
}
void Console::MaybeExecuteDumpFunctionForTime(JSContext* aCx,
MethodName aMethodName,
const nsAString& aMethodString,
uint64_t aMonotonicTimer,
const JS::Value& aData) {
if (!mDumpFunction && !mDumpToStdout) {
return;
}
nsAutoString message;
message.AssignLiteral("console.");
message.Append(aMethodString);
message.AppendLiteral(": ");
if (!mPrefix.IsEmpty()) {
message.Append(mPrefix);
message.AppendLiteral(": ");
}
JS::Rooted<JS::Value> v(aCx, aData);
JS::Rooted<JSString*> jsString(aCx, JS_ValueToSource(aCx, v));
if (!jsString) {
return;
}
nsAutoJSString string;
if (NS_WARN_IF(!string.init(aCx, jsString))) {
return;
}
message.Append(string);
message.AppendLiteral(" @ ");
message.AppendInt(aMonotonicTimer);
message.AppendLiteral("\n");
ExecuteDumpFunction(message);
}
void Console::ExecuteDumpFunction(const nsAString& aMessage) {
@ -2932,60 +2929,6 @@ uint32_t Console::InternalLogLevelToInteger(MethodName aName) const {
}
}
LogLevel Console::InternalLogLevelToMozLog(MethodName aName) const {
switch (aName) {
case MethodLog:
return LogLevel::Info;
case MethodInfo:
return LogLevel::Info;
case MethodWarn:
return LogLevel::Warning;
case MethodError:
return LogLevel::Error;
case MethodException:
return LogLevel::Error;
case MethodDebug:
return LogLevel::Debug;
case MethodTable:
return LogLevel::Info;
case MethodTrace:
return LogLevel::Info;
case MethodDir:
return LogLevel::Info;
case MethodDirxml:
return LogLevel::Info;
case MethodGroup:
return LogLevel::Info;
case MethodGroupCollapsed:
return LogLevel::Info;
case MethodGroupEnd:
return LogLevel::Info;
case MethodTime:
return LogLevel::Info;
case MethodTimeLog:
return LogLevel::Info;
case MethodTimeEnd:
return LogLevel::Info;
case MethodTimeStamp:
return LogLevel::Info;
case MethodAssert:
return LogLevel::Error;
case MethodCount:
return LogLevel::Info;
case MethodCountReset:
return LogLevel::Info;
case MethodClear:
return LogLevel::Info;
case MethodProfile:
return LogLevel::Info;
case MethodProfileEnd:
return LogLevel::Info;
default:
MOZ_CRASH("MethodName is out of sync with the Console implementation!");
return LogLevel::Disabled;
}
}
bool Console::ArgumentData::Initialize(JSContext* aCx,
const Sequence<JS::Value>& aArguments) {
mGlobal = JS::CurrentGlobalOrNull(aCx);

View File

@ -147,7 +147,7 @@ class Console final : public nsIObserver, public nsSupportsWeakReference {
private:
Console(JSContext* aCx, nsIGlobalObject* aGlobal, uint64_t aOuterWindowID,
uint64_t aInnerWIndowID, const nsAString& aPrefix = u""_ns);
uint64_t aInnerWIndowID);
~Console();
void Initialize(ErrorResult& aRv);
@ -359,19 +359,15 @@ class Console final : public nsIObserver, public nsSupportsWeakReference {
void StringifyElement(Element* aElement, nsAString& aOut);
MOZ_CAN_RUN_SCRIPT
void MaybeExecuteDumpFunction(JSContext* aCx, MethodName aMethodName,
const nsAString& aMethodString,
void MaybeExecuteDumpFunction(JSContext* aCx, const nsAString& aMethodName,
const Sequence<JS::Value>& aData,
nsIStackFrame* aStack,
DOMHighResTimeStamp aMonotonicTimer);
nsIStackFrame* aStack);
MOZ_CAN_RUN_SCRIPT
nsString GetDumpMessage(JSContext* aCx, MethodName aMethodName,
const nsAString& aMethodString,
const Sequence<JS::Value>& aData,
nsIStackFrame* aStack,
DOMHighResTimeStamp aMonotonicTimer,
bool aIsForMozLog);
void MaybeExecuteDumpFunctionForTime(JSContext* aCx, MethodName aMethodName,
const nsAString& aMethodString,
uint64_t aMonotonicTimer,
const JS::Value& aData);
MOZ_CAN_RUN_SCRIPT
void ExecuteDumpFunction(const nsAString& aMessage);
@ -381,7 +377,6 @@ class Console final : public nsIObserver, public nsSupportsWeakReference {
uint32_t WebIDLLogLevelToInteger(ConsoleLogLevel aLevel) const;
uint32_t InternalLogLevelToInteger(MethodName aName) const;
LogLevel InternalLogLevelToMozLog(MethodName aName) const;
class ArgumentData {
public:
@ -430,7 +425,6 @@ class Console final : public nsIObserver, public nsSupportsWeakReference {
nsString mPassedInnerID;
RefPtr<ConsoleInstanceDumpCallback> mDumpFunction;
bool mDumpToStdout;
LogModule* mLogModule;
nsString mPrefix;
bool mChromeInstance;
uint32_t mCurrentLogLevel;

View File

@ -46,7 +46,7 @@ ConsoleUtils::Level WebIDLevelToConsoleUtilsLevel(ConsoleLevel aLevel) {
ConsoleInstance::ConsoleInstance(JSContext* aCx,
const ConsoleInstanceOptions& aOptions)
: mMaxLogLevel(ConsoleLogLevel::All),
mConsole(new Console(aCx, nullptr, 0, 0, aOptions.mPrefix)) {
mConsole(new Console(aCx, nullptr, 0, 0)) {
mConsole->mConsoleID = aOptions.mConsoleID;
mConsole->mPassedInnerID = aOptions.mInnerID;
@ -54,6 +54,8 @@ ConsoleInstance::ConsoleInstance(JSContext* aCx,
mConsole->mDumpFunction = &aOptions.mDump.Value();
}
mConsole->mPrefix = aOptions.mPrefix;
// Let's inform that this is a custom instance.
mConsole->mChromeInstance = true;

View File

@ -51,7 +51,6 @@ LOCAL_INCLUDES += [
MOCHITEST_MANIFESTS += ["tests/mochitest.toml"]
MOCHITEST_CHROME_MANIFESTS += ["tests/chrome.toml"]
XPCSHELL_TESTS_MANIFESTS += ["tests/xpcshell/xpcshell.toml"]
BROWSER_CHROME_MANIFESTS += ["tests/browser.toml"]
include("/ipc/chromium/chromium-config.mozbuild")

View File

@ -1,4 +0,0 @@
[DEFAULT]
support-files = []
["browser_console_to_mozlog.js"]

View File

@ -1,60 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/* Use console API to log via MOZ_LOG to stdout/file/profiler */
// Use background task in order to control MOZ_LOG env variable passed to another gecko run
const { BackgroundTasksTestUtils } = ChromeUtils.importESModule(
"resource://testing-common/BackgroundTasksTestUtils.sys.mjs"
);
BackgroundTasksTestUtils.init(this);
const do_backgroundtask = BackgroundTasksTestUtils.do_backgroundtask.bind(
BackgroundTasksTestUtils
);
add_task(async function test_console_to_mozlog() {
const lines = [];
const promise = do_backgroundtask("console", {
onStdoutLine: (line, _proc) => {
dump(`STDOUT: ${line}`);
lines.push(line);
},
extraEnv: {
MOZ_LOG: "console:5,my-prefix:2",
},
});
const exitCode = await promise;
is(exitCode, 0);
const pidLine = lines.find(line => line.includes("CONSOLE-PID"));
ok(pidLine, "Found the line where the parent process PID is logged");
const [, pid] = pidLine.split(":");
ok(pid, "Got the pid out of the PID line");
// Each MOZ_LOG / console api call starts with a description of the process and thread where it is logged
const threadPrefix = `[Parent ${pid}: Main Thread]: `;
const expectedLogs = [
`I/console log: "foo"`,
`D/console debug: "bar"`,
// Bug 1923985: For now, the console API level isn't synchronized with MOZ_LOG one.
// shouldLogLog should be false because of my-prefix set to level 2.
`E/my-prefix error: ({shouldLogError:true, shouldLogLog:true})`,
`W/my-prefix warn: "warning"`,
];
for (const expected of expectedLogs) {
ok(
lines.some(line => line.includes(`${threadPrefix}${expected}`)),
`Found ${expected}`
);
}
// The console.log call with my-prefix isn't logged because of log level set to "2" for my-prefix
ok(
!lines.some(line => line.includes("not-logged")),
"Logs blocked by too verbose level aren't visible in stdout"
);
});

View File

@ -68,7 +68,6 @@ TESTING_JS_MODULES += [
TESTING_JS_MODULES.backgroundtasks += [
"tests/BackgroundTask_automaticrestart.sys.mjs",
"tests/BackgroundTask_backgroundtask_specific_pref.sys.mjs",
"tests/BackgroundTask_console.sys.mjs",
"tests/BackgroundTask_crash.sys.mjs",
"tests/BackgroundTask_file_exists.sys.mjs",
"tests/BackgroundTask_jsdebugger.sys.mjs",

View File

@ -1,25 +0,0 @@
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
export async function runBackgroundTask(_commandLine) {
// Disable logging of Console to stdout in order to focus on MOZ_LOG ones
Services.prefs.setBoolPref("devtools.console.stdout.chrome", false);
// Log the main thread PID so that the test can identify it easily
const pid = Services.appinfo.processID;
dump(`CONSOLE-PID:${pid}\n`);
console.log("foo");
console.debug("bar");
const prefixed = console.createInstance({ prefix: "my-prefix" });
prefixed.error({
shouldLogError: prefixed.shouldLog("Error"),
shouldLogLog: prefixed.shouldLog("Log"),
});
prefixed.warn("warning");
prefixed.log("not-logged");
return 0;
}

View File

@ -1,9 +1,8 @@
Gecko Logging
=============
A minimal logging framework is provided for use in core Gecko code,
written in C++ and enabled for all builds and is thread-safe.
It can be accessed via C++, JavaScript or Rust.
A minimal C++ logging framework is provided for use in core Gecko code. It is
enabled for all builds and is thread-safe.
This page covers enabling logging for particular logging module, configuring
the logging output, and how to use the logging facilities in native code.
@ -501,73 +500,3 @@ Example Usage
MOZ_LOG(sLogger, LogLevel::Error, ("i should be 10!"));
}
}
Logging from JavaScript via the ``console`` API
+++++++++++++++++++++++++++++++++++++++++++++++
Any call made to a ``console`` API from JavaScript will be logged through the
``MOZ_LOG`` pipeline.
- Web Pages as well as privileged context using ``console`` API expose to
JavaScript will automatically generate MOZ_LOG messages under the ``console``
module name.
- Privileged context can use a specific module name by instantiating their own
console object:
``const logger = console.createInstance({ prefix: "module-name" })``,
``prefix`` value will be used as the MOZ_LOG module name.
More info about ``console.createInstance`` is available on the
`JavaScript Logging page </toolkit/javascript-logging.html>`_
When using the ``console`` API, the console methods calls will be visible
in the Developer Tools, as well as through MOZ_LOG stdout, file or profiler
outputs.
Note that because of `Bug 1923985
<https://bugzilla.mozilla.org/show_bug.cgi?id=1923985>`_,
there is some discrepancies between console log level and MOZ_LOG one.
So that ``console.shouldLog()`` only consider the level set by
``createInstance``'s ``maxLogLevel{Pref}`` arguments.
.. code-block:: javascript
// The following two logs can be visible through MOZ_LOG by using:
// MOZ_LOG=console:5
// Both call will be logged through "console" module name.
// Any console API call from privileged or content page will be logged.
console.log("Doing stuff.");
console.error("Error happened");
// The following two other logs can be visible through MOZ_LOG by using:
// MOZ_LOG=example_logger:5
// From a privileged context, you can instantiate your own console object
// with a specific module name, here "example_logger":
const logger = console.createInstance({ prefix: "example_logger" });
logger.warn("something failed");
logger.debug("some debug info");
Console API levels
------------------
+----------------------+---------------+
| Console API Method | MOZ_LOG Level |
+======================+===============+
| console.error() | 1 (Error) |
| console.assert() | |
+----------------------+---------------+
| console.warn() | 2 (Warning) |
+----------------------+---------------+
| All other methods, | 3 (Info) |
| but console.debug() | |
+----------------------+---------------+
| console.debug() | 4 (Debug |
+----------------------+---------------+