avoid depending on flutter log level value

This commit is contained in:
Adam Velebil 2022-05-09 15:31:13 +02:00
parent 99708c99ef
commit b0a1d16012
No known key found for this signature in database
GPG Key ID: AC6D6B9D715FC084
2 changed files with 16 additions and 18 deletions

View File

@ -6,23 +6,18 @@ import io.flutter.plugin.common.MethodChannel
class FlutterLog(messenger: BinaryMessenger) { class FlutterLog(messenger: BinaryMessenger) {
private enum class LogLevel(val value: Int) { private enum class LogLevel {
TRAFFIC(500), TRAFFIC,
DEBUG(700), DEBUG,
INFO(800), INFO,
WARNING(900), WARNING,
ERROR(1000); ERROR
companion object {
fun fromInt(value: Int?) = values().firstOrNull { it.value == value }
}
} }
private var _channel = MethodChannel(messenger, "android.log.redirect") private var _channel = MethodChannel(messenger, "android.log.redirect")
private var _level = LogLevel.INFO private var _level = LogLevel.INFO
companion object { companion object {
private const val TAG = "yubico-authenticator" private const val TAG = "yubico-authenticator"
private lateinit var instance: FlutterLog private lateinit var instance: FlutterLog
@ -31,6 +26,10 @@ class FlutterLog(messenger: BinaryMessenger) {
instance = FlutterLog(messenger) instance = FlutterLog(messenger)
} }
private val logLevelFromArgument: (String?) -> LogLevel = { argValue ->
LogLevel.valueOf(argValue?.uppercase() ?: "INFO")
}
@Suppress("unused") @Suppress("unused")
fun t(tag: String, message: String, error: String? = null) { fun t(tag: String, message: String, error: String? = null) {
instance.log(LogLevel.TRAFFIC, tag, message, error) instance.log(LogLevel.TRAFFIC, tag, message, error)
@ -59,14 +58,15 @@ class FlutterLog(messenger: BinaryMessenger) {
init { init {
_channel.setMethodCallHandler { call, result -> _channel.setMethodCallHandler { call, result ->
when (call.method) { when (call.method) {
"log" -> { "log" -> {
val message = call.argument<String>("message") val message = call.argument<String>("message")
val error = call.argument<String?>("error") val error = call.argument<String?>("error")
val loggerName = call.argument<String>("loggerName") val loggerName = call.argument<String>("loggerName")
val level = LogLevel.fromInt(call.argument<Int>("level")) val level = logLevelFromArgument(call.argument("level"))
if (level != null && loggerName != null && message != null) { if (loggerName != null && message != null) {
log(level, loggerName, message, error) log(level, loggerName, message, error)
result.success(null) result.success(null)
} else { } else {
@ -74,11 +74,9 @@ class FlutterLog(messenger: BinaryMessenger) {
} }
} }
"setLevel" -> { "setLevel" -> {
_level = LogLevel.fromInt(call.argument<Int>("level")) ?: LogLevel.INFO _level = logLevelFromArgument(call.argument("level"))
} }
} }
} }
} }

View File

@ -17,14 +17,14 @@ class AndroidLogger {
void setLogLevel(Level level) async { void setLogLevel(Level level) async {
await _channel.invokeMethod('setLevel', { await _channel.invokeMethod('setLevel', {
'level': level.value, 'level': level.name,
}); });
} }
void log(LogRecord record) async { void log(LogRecord record) async {
await _channel.invokeMethod('log', { await _channel.invokeMethod('log', {
'loggerName': record.loggerName, 'loggerName': record.loggerName,
'level': record.level.value, 'level': record.level.name,
'message': record.message, 'message': record.message,
'error': record.error 'error': record.error
}); });