control log level

This commit is contained in:
Fabian Freund
2025-08-11 10:33:20 +02:00
parent 70ef78653e
commit cf3da71ca5
12 changed files with 259 additions and 180 deletions
@@ -28,11 +28,13 @@ import mozilla.components.feature.downloads.DefaultFileSizeFormatter
import mozilla.components.feature.downloads.DownloadEstimator
import mozilla.components.feature.downloads.FileSizeFormatter
import mozilla.components.support.base.android.NotificationsDelegate
import mozilla.components.support.base.log.Log
class Components(private val context: Context,
val flutterEvents: GeckoStateEvents,
val readerViewController: ReaderViewController,
val selectionAction: SelectionActionDelegate,
val logLevel: Log.Priority,
private val addonEvents: GeckoAddonEvents,
private val tabContentEvents: GeckoTabContentEvents,
private val extensionEvents: BrowserExtensionEvents
@@ -17,6 +17,7 @@ import mozilla.components.concept.engine.DefaultSettings
import mozilla.components.concept.engine.Engine
import mozilla.components.concept.fetch.Client
import mozilla.components.feature.webcompat.WebCompatFeature
import mozilla.components.support.base.log.Log
import mozilla.components.support.base.log.logger.Logger
import org.mozilla.geckoview.GeckoRuntime
import org.mozilla.geckoview.GeckoRuntimeSettings
@@ -24,6 +25,10 @@ import org.mozilla.geckoview.GeckoRuntimeSettings
object EngineProvider {
private var runtime: GeckoRuntime? = null
private val components by lazy {
requireNotNull(GlobalComponents.components) { "Components not initialized" }
}
@Synchronized
fun getOrCreateRuntime(context: Context): GeckoRuntime {
if (runtime == null) {
@@ -38,9 +43,8 @@ object EngineProvider {
builder.aboutConfigEnabled(true)
builder.extensionsProcessEnabled(true)
builder.extensionsWebAPIEnabled(true)
// Disable output for now to improve performance
// builder.consoleOutput(true)
builder.debugLogging(components.logLevel == Log.Priority.DEBUG)
builder.consoleOutput(components.logLevel == Log.Priority.DEBUG)
runtime = GeckoRuntime.create(context, builder.build())
}
@@ -52,7 +52,8 @@ object GlobalComponents {
selectionAction: SelectionActionDelegate,
addonEvents: GeckoAddonEvents,
tabContentEvents: GeckoTabContentEvents,
extensionEvents: BrowserExtensionEvents
extensionEvents: BrowserExtensionEvents,
logLevel: Log.Priority
) {
Logger.debug("Creating new components")
@@ -61,6 +62,7 @@ object GlobalComponents {
flutterEvents,
readerViewController,
selectionAction,
logLevel,
addonEvents,
tabContentEvents,
extensionEvents
@@ -37,6 +37,7 @@ import eu.weblibre.flutter_mozilla_components.pigeons.GeckoSuggestionApi
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoSuggestionEvents
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoTabContentEvents
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoTabsApi
import eu.weblibre.flutter_mozilla_components.pigeons.LogLevel
import eu.weblibre.flutter_mozilla_components.pigeons.ReaderViewController
import eu.weblibre.flutter_mozilla_components.pigeons.ReaderViewEvents
import io.flutter.embedding.engine.plugins.FlutterPlugin
@@ -45,6 +46,26 @@ import mozilla.components.browser.state.action.SystemAction
import mozilla.components.feature.addons.logger
import mozilla.components.support.base.log.Log
import mozilla.components.support.base.log.sink.AndroidLogSink
import mozilla.components.support.base.log.sink.LogSink
class PriorityAwareLogSink(
private val minLogPriority: Log.Priority,
private val androidLogSink: LogSink,
) : LogSink {
override fun log(
priority: Log.Priority,
tag: String?,
throwable: Throwable?,
message: String,
) {
if (priority < minLogPriority) {
return
}
androidLogSink.log(priority, tag, throwable, message)
}
}
/**
* Implementation of GeckoBrowserApi that handles browser-related operations
@@ -94,12 +115,19 @@ class GeckoBrowserApiImpl : GeckoBrowserApi {
isPlatformViewRegistered = false
}
override fun initialize() {
override fun initialize(logLevel: LogLevel) {
synchronized(this) {
if(!isGeckoInitialized) {
Log.addSink(AndroidLogSink())
val level = when(logLevel) {
LogLevel.DEBUG -> Log.Priority.DEBUG
LogLevel.INFO -> Log.Priority.INFO
LogLevel.WARN -> Log.Priority.WARN
LogLevel.ERROR -> Log.Priority.ERROR
};
setupGeckoEngine()
Log.addSink(PriorityAwareLogSink(level, AndroidLogSink("WebLibre")))
setupGeckoEngine(level)
isGeckoInitialized = true
}
}
@@ -115,7 +143,7 @@ class GeckoBrowserApiImpl : GeckoBrowserApi {
return false
}
private fun setupGeckoEngine() {
private fun setupGeckoEngine(logLevel: Log.Priority) {
val selectionActionEvents = GeckoSelectionActionEvents(_flutterPluginBinding.binaryMessenger)
val selectionActionDelegate = DefaultSelectionActionDelegate(selectionActionEvents) { actions ->
@@ -144,7 +172,8 @@ class GeckoBrowserApiImpl : GeckoBrowserApi {
selectionActionDelegate,
addonEvents,
tabContentEvents,
extensionEvents
extensionEvents,
logLevel
)
GeckoEngineSettingsApi.setUp(_flutterPluginBinding.binaryMessenger, GeckoEngineSettingsApiImpl())
@@ -159,7 +159,6 @@ class Events(
}
.debounce { 50 }
.collect { tab ->
logger.info("title: ${tab.content.title} ${tab.content.url}")
flutterEvents.onTabContentStateChange(
System.currentTimeMillis(),
TabContentState(
@@ -1,4 +1,4 @@
// Autogenerated from Pigeon (v25.5.0), do not edit directly.
// Autogenerated from Pigeon (v26.0.0), do not edit directly.
// See also: https://pub.dev/packages/pigeon
@file:Suppress("UNCHECKED_CAST", "ArrayInDataClass")
@@ -286,6 +286,19 @@ enum class DownloadStatus(val raw: Int) {
}
}
enum class LogLevel(val raw: Int) {
DEBUG(0),
INFO(1),
WARN(2),
ERROR(3);
companion object {
fun ofRaw(raw: Int): LogLevel? {
return values().firstOrNull { it.raw == raw }
}
}
}
/**
* Translation options that map to the Gecko Translations Options.
*
@@ -2003,196 +2016,201 @@ private open class GeckoPigeonCodec : StandardMessageCodec() {
}
}
143.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
TranslationOptions.fromList(it)
return (readValue(buffer) as Long?)?.let {
LogLevel.ofRaw(it.toInt())
}
}
144.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
ReaderState.fromList(it)
TranslationOptions.fromList(it)
}
}
145.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
LastMediaAccessState.fromList(it)
ReaderState.fromList(it)
}
}
146.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
HistoryMetadataKey.fromList(it)
LastMediaAccessState.fromList(it)
}
}
147.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
PackageCategoryValue.fromList(it)
HistoryMetadataKey.fromList(it)
}
}
148.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
ExternalPackage.fromList(it)
PackageCategoryValue.fromList(it)
}
}
149.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
LoadUrlFlagsValue.fromList(it)
ExternalPackage.fromList(it)
}
}
150.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
SourceValue.fromList(it)
LoadUrlFlagsValue.fromList(it)
}
}
151.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
TabState.fromList(it)
SourceValue.fromList(it)
}
}
152.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
RecoverableTab.fromList(it)
TabState.fromList(it)
}
}
153.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
RecoverableBrowserState.fromList(it)
RecoverableTab.fromList(it)
}
}
154.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
IconRequest.fromList(it)
RecoverableBrowserState.fromList(it)
}
}
155.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
ResourceSize.fromList(it)
IconRequest.fromList(it)
}
}
156.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
Resource.fromList(it)
ResourceSize.fromList(it)
}
}
157.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
IconResult.fromList(it)
Resource.fromList(it)
}
}
158.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
CookiePartitionKey.fromList(it)
IconResult.fromList(it)
}
}
159.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
Cookie.fromList(it)
CookiePartitionKey.fromList(it)
}
}
160.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
HistoryItem.fromList(it)
Cookie.fromList(it)
}
}
161.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
HistoryState.fromList(it)
HistoryItem.fromList(it)
}
}
162.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
ReaderableState.fromList(it)
HistoryState.fromList(it)
}
}
163.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
SecurityInfoState.fromList(it)
ReaderableState.fromList(it)
}
}
164.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
TabContentState.fromList(it)
SecurityInfoState.fromList(it)
}
}
165.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
FindResultState.fromList(it)
TabContentState.fromList(it)
}
}
166.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
CustomSelectionAction.fromList(it)
FindResultState.fromList(it)
}
}
167.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
WebExtensionData.fromList(it)
CustomSelectionAction.fromList(it)
}
}
168.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
GeckoSuggestion.fromList(it)
WebExtensionData.fromList(it)
}
}
169.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
TabContent.fromList(it)
GeckoSuggestion.fromList(it)
}
}
170.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
GeckoEngineSettings.fromList(it)
TabContent.fromList(it)
}
}
171.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
AutocompleteResult.fromList(it)
GeckoEngineSettings.fromList(it)
}
}
172.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
UnknownHitResult.fromList(it)
AutocompleteResult.fromList(it)
}
}
173.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
ImageHitResult.fromList(it)
UnknownHitResult.fromList(it)
}
}
174.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
VideoHitResult.fromList(it)
ImageHitResult.fromList(it)
}
}
175.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
AudioHitResult.fromList(it)
VideoHitResult.fromList(it)
}
}
176.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
ImageSrcHitResult.fromList(it)
AudioHitResult.fromList(it)
}
}
177.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
PhoneHitResult.fromList(it)
ImageSrcHitResult.fromList(it)
}
}
178.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
EmailHitResult.fromList(it)
PhoneHitResult.fromList(it)
}
}
179.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
GeoHitResult.fromList(it)
EmailHitResult.fromList(it)
}
}
180.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
DownloadState.fromList(it)
GeoHitResult.fromList(it)
}
}
181.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
DownloadState.fromList(it)
}
}
182.toByte() -> {
return (readValue(buffer) as? List<Any?>)?.let {
ShareInternetResourceState.fromList(it)
}
@@ -2258,162 +2276,166 @@ private open class GeckoPigeonCodec : StandardMessageCodec() {
stream.write(142)
writeValue(stream, value.raw)
}
is TranslationOptions -> {
is LogLevel -> {
stream.write(143)
writeValue(stream, value.toList())
writeValue(stream, value.raw)
}
is ReaderState -> {
is TranslationOptions -> {
stream.write(144)
writeValue(stream, value.toList())
}
is LastMediaAccessState -> {
is ReaderState -> {
stream.write(145)
writeValue(stream, value.toList())
}
is HistoryMetadataKey -> {
is LastMediaAccessState -> {
stream.write(146)
writeValue(stream, value.toList())
}
is PackageCategoryValue -> {
is HistoryMetadataKey -> {
stream.write(147)
writeValue(stream, value.toList())
}
is ExternalPackage -> {
is PackageCategoryValue -> {
stream.write(148)
writeValue(stream, value.toList())
}
is LoadUrlFlagsValue -> {
is ExternalPackage -> {
stream.write(149)
writeValue(stream, value.toList())
}
is SourceValue -> {
is LoadUrlFlagsValue -> {
stream.write(150)
writeValue(stream, value.toList())
}
is TabState -> {
is SourceValue -> {
stream.write(151)
writeValue(stream, value.toList())
}
is RecoverableTab -> {
is TabState -> {
stream.write(152)
writeValue(stream, value.toList())
}
is RecoverableBrowserState -> {
is RecoverableTab -> {
stream.write(153)
writeValue(stream, value.toList())
}
is IconRequest -> {
is RecoverableBrowserState -> {
stream.write(154)
writeValue(stream, value.toList())
}
is ResourceSize -> {
is IconRequest -> {
stream.write(155)
writeValue(stream, value.toList())
}
is Resource -> {
is ResourceSize -> {
stream.write(156)
writeValue(stream, value.toList())
}
is IconResult -> {
is Resource -> {
stream.write(157)
writeValue(stream, value.toList())
}
is CookiePartitionKey -> {
is IconResult -> {
stream.write(158)
writeValue(stream, value.toList())
}
is Cookie -> {
is CookiePartitionKey -> {
stream.write(159)
writeValue(stream, value.toList())
}
is HistoryItem -> {
is Cookie -> {
stream.write(160)
writeValue(stream, value.toList())
}
is HistoryState -> {
is HistoryItem -> {
stream.write(161)
writeValue(stream, value.toList())
}
is ReaderableState -> {
is HistoryState -> {
stream.write(162)
writeValue(stream, value.toList())
}
is SecurityInfoState -> {
is ReaderableState -> {
stream.write(163)
writeValue(stream, value.toList())
}
is TabContentState -> {
is SecurityInfoState -> {
stream.write(164)
writeValue(stream, value.toList())
}
is FindResultState -> {
is TabContentState -> {
stream.write(165)
writeValue(stream, value.toList())
}
is CustomSelectionAction -> {
is FindResultState -> {
stream.write(166)
writeValue(stream, value.toList())
}
is WebExtensionData -> {
is CustomSelectionAction -> {
stream.write(167)
writeValue(stream, value.toList())
}
is GeckoSuggestion -> {
is WebExtensionData -> {
stream.write(168)
writeValue(stream, value.toList())
}
is TabContent -> {
is GeckoSuggestion -> {
stream.write(169)
writeValue(stream, value.toList())
}
is GeckoEngineSettings -> {
is TabContent -> {
stream.write(170)
writeValue(stream, value.toList())
}
is AutocompleteResult -> {
is GeckoEngineSettings -> {
stream.write(171)
writeValue(stream, value.toList())
}
is UnknownHitResult -> {
is AutocompleteResult -> {
stream.write(172)
writeValue(stream, value.toList())
}
is ImageHitResult -> {
is UnknownHitResult -> {
stream.write(173)
writeValue(stream, value.toList())
}
is VideoHitResult -> {
is ImageHitResult -> {
stream.write(174)
writeValue(stream, value.toList())
}
is AudioHitResult -> {
is VideoHitResult -> {
stream.write(175)
writeValue(stream, value.toList())
}
is ImageSrcHitResult -> {
is AudioHitResult -> {
stream.write(176)
writeValue(stream, value.toList())
}
is PhoneHitResult -> {
is ImageSrcHitResult -> {
stream.write(177)
writeValue(stream, value.toList())
}
is EmailHitResult -> {
is PhoneHitResult -> {
stream.write(178)
writeValue(stream, value.toList())
}
is GeoHitResult -> {
is EmailHitResult -> {
stream.write(179)
writeValue(stream, value.toList())
}
is DownloadState -> {
is GeoHitResult -> {
stream.write(180)
writeValue(stream, value.toList())
}
is ShareInternetResourceState -> {
is DownloadState -> {
stream.write(181)
writeValue(stream, value.toList())
}
is ShareInternetResourceState -> {
stream.write(182)
writeValue(stream, value.toList())
}
else -> super.writeValue(stream, value)
}
}
@@ -2422,7 +2444,7 @@ private open class GeckoPigeonCodec : StandardMessageCodec() {
/** Generated interface from Pigeon that represents a handler of messages from Flutter. */
interface GeckoBrowserApi {
fun initialize()
fun initialize(logLevel: LogLevel)
fun showNativeFragment(): Boolean
fun onTrimMemory(level: Long)
@@ -2438,9 +2460,11 @@ interface GeckoBrowserApi {
run {
val channel = BasicMessageChannel<Any?>(binaryMessenger, "dev.flutter.pigeon.flutter_mozilla_components.GeckoBrowserApi.initialize$separatedMessageChannelSuffix", codec)
if (api != null) {
channel.setMessageHandler { _, reply ->
channel.setMessageHandler { message, reply ->
val args = message as List<Any?>
val logLevelArg = args[0] as LogLevel
val wrapped: List<Any?> = try {
api.initialize()
api.initialize(logLevelArg)
listOf(null)
} catch (exception: Throwable) {
GeckoPigeonUtils.wrapError(exception)