From 3d4868592a7f4f464b55c9f4f8ea2518379c3a39 Mon Sep 17 00:00:00 2001 From: Abdelrahman Hossam <43494928+abdelrahman1234567@users.noreply.github.com> Date: Mon, 7 Jul 2025 17:14:12 +0800 Subject: [PATCH] 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 --- components/script/dom/console.rs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/components/script/dom/console.rs b/components/script/dom/console.rs index 8d8df9197b4..ef419de738c 100644 --- a/components/script/dom/console.rs +++ b/components/script/dom/console.rs @@ -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) 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)]