commit 5bb2705578cd0477b8310cce793b53b1199382e8
parent 3a5c9591a5d4ce190554cb2f3f0b4fc5eb0c7dc1
Author: Makoto Kato <m_kato@ga2.so-net.ne.jp>
Date: Wed, 22 Oct 2025 11:39:35 +0000
Bug 1994474 - Part 1. Add MOZ_LOG JNI interface to use MOZ_LOG on Java. r=geckoview-reviewers,tcampbell
Actually, we have about:logging to get logs via MOZ_LOG.
This fix adds JNI interface to use MOZ_LOG on Java side.
Differential Revision: https://phabricator.services.mozilla.com/D269353
Diffstat:
6 files changed, 119 insertions(+), 0 deletions(-)
diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/MozLog.java b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/MozLog.java
@@ -0,0 +1,52 @@
+/* -*- Mode: Java; c-basic-offset: 2; tab-width: 20; indent-tabs-mode: nil; -*-
+ * 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/. */
+
+package org.mozilla.gecko;
+
+import org.mozilla.gecko.annotation.WrapForJNI;
+
+public class MozLog {
+ private static final String LOG_TAG = "MozLog";
+
+ public static final int LOG_LEVEL_DISABLE = 0;
+ public static final int LOG_LEVEL_ERROR = 1;
+ public static final int LOG_LEVEL_WARNING = 2;
+ public static final int LOG_LEVEL_INFO = 3;
+ public static final int LOG_LEVEL_DEBUG = 4;
+ public static final int LOG_LEVEL_VERBOSE = 5;
+
+ @WrapForJNI(stubName = "Print")
+ private static native void printNative(String name, int level, String message);
+
+ public static void print(String name, int level, String message) {
+ if (GeckoThread.isRunning()) {
+ printNative(name, level, message);
+ return;
+ }
+
+ GeckoThread.queueNativeCall(
+ MozLog.class, "printNative", String.class, name, level, String.class, message);
+ }
+
+ public static void d(final String name, final String message) {
+ print(name, LOG_LEVEL_DEBUG, message);
+ }
+
+ public static void e(final String name, final String message) {
+ print(name, LOG_LEVEL_ERROR, message);
+ }
+
+ public static void i(final String name, final String message) {
+ print(name, LOG_LEVEL_INFO, message);
+ }
+
+ public static void v(final String name, final String message) {
+ print(name, LOG_LEVEL_VERBOSE, message);
+ }
+
+ public static void w(final String name, final String message) {
+ print(name, LOG_LEVEL_WARNING, message);
+ }
+}
diff --git a/widget/android/MozLogSupport.cpp b/widget/android/MozLogSupport.cpp
@@ -0,0 +1,23 @@
+/* -*- Mode: c++; c-basic-offset: 2; tab-width: 20; indent-tabs-mode: nil; -*-
+ * 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/. */
+
+#include "MozLogSupport.h"
+#include "mozilla/Logging.h"
+
+namespace mozilla::widget {
+
+// staitc
+void MozLogSupport::Print(jni::String::Param aName, int32_t aLogLevel,
+ jni::String::Param aMessage) {
+ LogModule* logModule = LogModule::Get(aName->ToCString().get());
+ LogLevel logLevel = ToLogLevel(aLogLevel);
+ if (!MOZ_LOG_TEST(logModule, logLevel)) {
+ return; // Don't log if the level is not enabled.
+ }
+
+ MOZ_LOG(logModule, logLevel, ("%s", aMessage->ToCString().get()));
+}
+
+} // namespace mozilla::widget
diff --git a/widget/android/MozLogSupport.h b/widget/android/MozLogSupport.h
@@ -0,0 +1,21 @@
+/* -*- Mode: c++; c-basic-offset: 2; tab-width: 20; indent-tabs-mode: nil; -*-
+ * 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/. */
+
+#ifndef MozLogSupport_h__
+#define MozLogSupport_h__
+
+#include "mozilla/java/MozLogNatives.h"
+
+namespace mozilla::widget {
+
+class MozLogSupport final : public java::MozLog::Natives<MozLogSupport> {
+ public:
+ static void Print(mozilla::jni::String::Param aName, int32_t aLogLevel,
+ mozilla::jni::String::Param aMessage);
+};
+
+} // namespace mozilla::widget
+
+#endif
diff --git a/widget/android/moz.build b/widget/android/moz.build
@@ -66,6 +66,7 @@ classes_with_WrapForJNI = [
"Image",
"ImageDecoder",
"MediaDrmProxy",
+ "MozLog",
"PanZoomController",
"Sample",
"SampleBuffer",
@@ -143,6 +144,7 @@ UNIFIED_SOURCES += [
"GfxInfo.cpp",
"ImageDecoderSupport.cpp",
"InProcessAndroidCompositorWidget.cpp",
+ "MozLogSupport.cpp",
"nsAppShell.cpp",
"nsClipboard.cpp",
"nsDeviceContextAndroid.cpp",
diff --git a/widget/android/nsAppShell.cpp b/widget/android/nsAppShell.cpp
@@ -79,6 +79,7 @@
#include "ScreenHelperAndroid.h"
#include "WebExecutorSupport.h"
#include "Base64UtilsSupport.h"
+#include "MozLogSupport.h"
#ifdef DEBUG_ANDROID_EVENTS
# define EVLOG(args...) ALOG(args)
@@ -434,6 +435,7 @@ nsAppShell::nsAppShell()
GeckoThreadSupport::Init();
GeckoAppShellSupport::Init();
XPCOMEventTargetWrapper::Init();
+ mozilla::widget::MozLogSupport::Init();
if (XRE_IsGPUProcess()) {
mozilla::gl::AndroidSurfaceTexture::Init();
@@ -463,6 +465,7 @@ nsAppShell::nsAppShell()
mozilla::widget::Base64UtilsSupport::Init();
nsWindow::InitNatives();
mozilla::gl::AndroidSurfaceTexture::Init();
+ mozilla::widget::MozLogSupport::Init();
java::GeckoThread::SetState(java::GeckoThread::State::JNI_READY());
diff --git a/xpcom/docs/logging.rst b/xpcom/docs/logging.rst
@@ -557,6 +557,24 @@ So that ``console.shouldLog()`` only consider the level set by
logger.debug("some debug info");
+Logging from Java
++++++++++++++++++
+
+In GeckoView, the Java code can log messages using the `org.mozilla.gecko.MozLog` class.
+
+.. code-block:: java
+
+ import org.mozilla.gecko.MozLog;
+
+ public class Example {
+ public void doStuff() {
+ final String MODULE = "GeckoSample";
+ MozLog.d(MODULE, "Doing stuff");
+ MozLog.w(MODULE, "Warning");
+ MozLog.e(MODULE, "Error happened");
+ }
+ }
+
Logging web page errors and warnings
++++++++++++++++++++++++++++++++++++