diff --git a/android/app/src/main/kotlin/com/yubico/authenticator/FlutterLog.kt b/android/app/src/main/kotlin/com/yubico/authenticator/FlutterLog.kt index 6f42e729..6aa4af12 100644 --- a/android/app/src/main/kotlin/com/yubico/authenticator/FlutterLog.kt +++ b/android/app/src/main/kotlin/com/yubico/authenticator/FlutterLog.kt @@ -6,23 +6,18 @@ import io.flutter.plugin.common.MethodChannel class FlutterLog(messenger: BinaryMessenger) { - private enum class LogLevel(val value: Int) { - TRAFFIC(500), - DEBUG(700), - INFO(800), - WARNING(900), - ERROR(1000); - - companion object { - fun fromInt(value: Int?) = values().firstOrNull { it.value == value } - } + private enum class LogLevel { + TRAFFIC, + DEBUG, + INFO, + WARNING, + ERROR } private var _channel = MethodChannel(messenger, "android.log.redirect") private var _level = LogLevel.INFO companion object { - private const val TAG = "yubico-authenticator" private lateinit var instance: FlutterLog @@ -31,6 +26,10 @@ class FlutterLog(messenger: BinaryMessenger) { instance = FlutterLog(messenger) } + private val logLevelFromArgument: (String?) -> LogLevel = { argValue -> + LogLevel.valueOf(argValue?.uppercase() ?: "INFO") + } + @Suppress("unused") fun t(tag: String, message: String, error: String? = null) { instance.log(LogLevel.TRAFFIC, tag, message, error) @@ -59,14 +58,15 @@ class FlutterLog(messenger: BinaryMessenger) { init { _channel.setMethodCallHandler { call, result -> + when (call.method) { "log" -> { val message = call.argument("message") val error = call.argument("error") val loggerName = call.argument("loggerName") - val level = LogLevel.fromInt(call.argument("level")) + val level = logLevelFromArgument(call.argument("level")) - if (level != null && loggerName != null && message != null) { + if (loggerName != null && message != null) { log(level, loggerName, message, error) result.success(null) } else { @@ -74,11 +74,9 @@ class FlutterLog(messenger: BinaryMessenger) { } } "setLevel" -> { - _level = LogLevel.fromInt(call.argument("level")) ?: LogLevel.INFO + _level = logLevelFromArgument(call.argument("level")) } } - - } } diff --git a/lib/android/logger.dart b/lib/android/logger.dart index 168ecc20..233a60de 100644 --- a/lib/android/logger.dart +++ b/lib/android/logger.dart @@ -17,14 +17,14 @@ class AndroidLogger { void setLogLevel(Level level) async { await _channel.invokeMethod('setLevel', { - 'level': level.value, + 'level': level.name, }); } void log(LogRecord record) async { await _channel.invokeMethod('log', { 'loggerName': record.loggerName, - 'level': record.level.value, + 'level': record.level.name, 'message': record.message, 'error': record.error });