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) {
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<String>("message")
val error = call.argument<String?>("error")
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)
result.success(null)
} else {
@ -74,11 +74,9 @@ class FlutterLog(messenger: BinaryMessenger) {
}
}
"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 {
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
});