tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit 9663df298d724588d9bdb07c8a460105234d38b4
parent 97a5d86d27b010bd9c943ec2887a17d812df4d24
Author: Makoto Kato <m_kato@ga2.so-net.ne.jp>
Date:   Fri, 24 Oct 2025 06:13:57 +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:
Amobile/android/geckoview/src/main/java/org/mozilla/gecko/MozLog.java | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
Awidget/android/MozLogSupport.cpp | 23+++++++++++++++++++++++
Awidget/android/MozLogSupport.h | 21+++++++++++++++++++++
Mwidget/android/moz.build | 2++
Mwidget/android/nsAppShell.cpp | 3+++
Mxpcom/docs/logging.rst | 18++++++++++++++++++
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(final String name, final int level, final 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 @@ -78,6 +78,7 @@ #include "ScreenHelperAndroid.h" #include "WebExecutorSupport.h" #include "Base64UtilsSupport.h" +#include "MozLogSupport.h" #ifdef DEBUG_ANDROID_EVENTS # define EVLOG(args...) ALOG(args) @@ -433,6 +434,7 @@ nsAppShell::nsAppShell() GeckoThreadSupport::Init(); GeckoAppShellSupport::Init(); XPCOMEventTargetWrapper::Init(); + mozilla::widget::MozLogSupport::Init(); if (XRE_IsGPUProcess()) { mozilla::gl::AndroidSurfaceTexture::Init(); @@ -462,6 +464,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 ++++++++++++++++++++++++++++++++++++