pipe gecko logs to flutter

This commit is contained in:
Fabian Freund
2025-08-12 21:06:48 +02:00
parent f70cc7acfa
commit 0b467bb2e4
7 changed files with 134 additions and 4 deletions
@@ -27,6 +27,7 @@ import eu.weblibre.flutter_mozilla_components.pigeons.GeckoDownloadsApi
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoEngineSettingsApi
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoFindApi
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoIconsApi
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoLogging
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoMlApi
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoPrefApi
import eu.weblibre.flutter_mozilla_components.pigeons.GeckoSelectionActionController
@@ -44,13 +45,15 @@ import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding
import mozilla.components.browser.state.action.SystemAction
import mozilla.components.feature.addons.logger
import mozilla.components.support.base.ext.getStacktraceAsString
import mozilla.components.support.base.log.Log
import mozilla.components.support.base.log.sink.AndroidLogSink
import mozilla.components.support.base.log.sink.LogSink
import org.mozilla.gecko.util.ThreadUtils.runOnUiThread
class PriorityAwareLogSink(
private val minLogPriority: Log.Priority,
private val androidLogSink: LogSink,
private val geckoLogging: GeckoLogging
) : LogSink {
override fun log(
@@ -63,7 +66,22 @@ class PriorityAwareLogSink(
return
}
androidLogSink.log(priority, tag, throwable, message)
val level = when(priority) {
Log.Priority.DEBUG -> LogLevel.DEBUG
Log.Priority.INFO -> LogLevel.INFO
Log.Priority.WARN -> LogLevel.WARN
Log.Priority.ERROR -> LogLevel.ERROR
};
val logMessage: String = if (throwable != null) {
"$message\n${throwable.getStacktraceAsString()}"
} else {
message
}
runOnUiThread {
geckoLogging.onLog(level, logMessage) { _ -> }
}
}
}
@@ -118,6 +136,8 @@ class GeckoBrowserApiImpl : GeckoBrowserApi {
override fun initialize(logLevel: LogLevel) {
synchronized(this) {
if(!isGeckoInitialized) {
val geckoLogging = GeckoLogging(_flutterPluginBinding.binaryMessenger)
val level = when(logLevel) {
LogLevel.DEBUG -> Log.Priority.DEBUG
LogLevel.INFO -> Log.Priority.INFO
@@ -125,7 +145,7 @@ class GeckoBrowserApiImpl : GeckoBrowserApi {
LogLevel.ERROR -> Log.Priority.ERROR
};
Log.addSink(PriorityAwareLogSink(level, AndroidLogSink("WebLibre")))
Log.addSink(PriorityAwareLogSink(level, geckoLogging))
setupGeckoEngine(level)
isGeckoInitialized = true
@@ -3999,6 +3999,32 @@ class GeckoStateEvents(private val binaryMessenger: BinaryMessenger, private val
}
}
}
/** Generated class from Pigeon that represents Flutter messages that can be called from Kotlin. */
class GeckoLogging(private val binaryMessenger: BinaryMessenger, private val messageChannelSuffix: String = "") {
companion object {
/** The codec used by GeckoLogging. */
val codec: MessageCodec<Any?> by lazy {
GeckoPigeonCodec()
}
}
fun onLog(levelArg: LogLevel, messageArg: String, callback: (Result<Unit>) -> Unit)
{
val separatedMessageChannelSuffix = if (messageChannelSuffix.isNotEmpty()) ".$messageChannelSuffix" else ""
val channelName = "dev.flutter.pigeon.flutter_mozilla_components.GeckoLogging.onLog$separatedMessageChannelSuffix"
val channel = BasicMessageChannel<Any?>(binaryMessenger, channelName, codec)
channel.send(listOf(levelArg, messageArg)) {
if (it is List<*>) {
if (it.size > 1) {
callback(Result.failure(FlutterError(it[0] as String, it[1] as String, it[2] as String?)))
} else {
callback(Result.success(Unit))
}
} else {
callback(Result.failure(GeckoPigeonUtils.createConnectionError(channelName)))
}
}
}
}
/** Generated interface from Pigeon that represents a handler of messages from Flutter. */
interface ReaderViewEvents {
fun onToggleReaderView(enable: Boolean)