Differentiate console message behavior based on target OS (#37912)

Printing using log macro for Android and OhOS, while retaining original
stderr behavior for other platforms.

Fixes: #37877

Signed-off-by: abdelrahman1234567 <boudyalex321@gmail.com>
This commit is contained in:
Abdelrahman Hossam 2025-07-07 17:14:12 +08:00 committed by GitHub
parent ee8bd14f3b
commit 3d4868592a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -106,6 +106,7 @@ impl Console {
// we're finished with stdout. Since the stderr lock is reentrant, there is
// no risk of deadlock if the callback ends up trying to write to stderr for
// any reason.
#[cfg(not(any(target_os = "android", target_env = "ohos")))]
fn with_stderr_lock<F>(f: F)
where
F: FnOnce(),
@ -335,11 +336,24 @@ fn stringify_handle_values(messages: &[HandleValue]) -> DOMString {
}
fn console_message(global: &GlobalScope, message: &DOMString) {
with_stderr_lock(move || {
let prefix = global.current_group_label().unwrap_or_default();
let message = format!("{}{}", prefix, message);
println!("{}", message);
})
let prefix = global.current_group_label().unwrap_or_default();
let formatted_message = format!("{}{}", prefix, message);
// On ohos / android stdout and stderr don't go anywhere useful,
// and are redirected in servoshell to the logger again by a
// dedicated thread. Better to directly log to the system logger
// on ohos / android to avoid this.
#[cfg(any(target_os = "android", target_env = "ohos"))]
{
info!("{}", formatted_message);
}
#[cfg(not(any(target_os = "android", target_env = "ohos")))]
{
with_stderr_lock(move || {
println!("{}", formatted_message);
});
}
}
#[derive(Debug, Eq, PartialEq)]