From dd3b91a05a2fc3789bd88b1f9d070b6926b42bc4 Mon Sep 17 00:00:00 2001 From: Adam Velebil Date: Fri, 22 Sep 2023 18:56:12 +0200 Subject: [PATCH] add BufferAppender --- android/app/src/main/assets/logback.xml | 8 ++- .../authenticator/logging/BufferAppender.kt | 54 +++++++++++++++++++ .../authenticator/logging/FlutterLog.kt | 11 +++- .../com/yubico/authenticator/logging/Log.kt | 20 ++----- 4 files changed, 73 insertions(+), 20 deletions(-) create mode 100644 android/app/src/main/kotlin/com/yubico/authenticator/logging/BufferAppender.kt diff --git a/android/app/src/main/assets/logback.xml b/android/app/src/main/assets/logback.xml index 2d8d6982..b53ea3b4 100644 --- a/android/app/src/main/assets/logback.xml +++ b/android/app/src/main/assets/logback.xml @@ -24,8 +24,14 @@ - + + + %d{HH:mm:ss:SSS} [%thread] %-5level %logger{36} - %X{app} %msg + + + + \ No newline at end of file diff --git a/android/app/src/main/kotlin/com/yubico/authenticator/logging/BufferAppender.kt b/android/app/src/main/kotlin/com/yubico/authenticator/logging/BufferAppender.kt new file mode 100644 index 00000000..fc0dfca9 --- /dev/null +++ b/android/app/src/main/kotlin/com/yubico/authenticator/logging/BufferAppender.kt @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2023 Yubico. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.yubico.authenticator.logging + +import ch.qos.logback.classic.encoder.PatternLayoutEncoder +import ch.qos.logback.classic.spi.ILoggingEvent +import ch.qos.logback.core.UnsynchronizedAppenderBase + +class BufferAppender : UnsynchronizedAppenderBase() { + + private var encoder: PatternLayoutEncoder? = null + + private val buffer = arrayListOf() + + public override fun append(event: ILoggingEvent) { + if (!isStarted) { + return + } + + if (buffer.size > MAX_BUFFER_SIZE) { + buffer.removeAt(0) + } + + buffer.add(encoder!!.layout.doLayout(event)) + } + + fun getEncoder(): PatternLayoutEncoder? = encoder + + fun setEncoder(encoder: PatternLayoutEncoder?) { + this.encoder = encoder + } + + fun getLogBuffer(): ArrayList { + return buffer + } + + companion object { + private const val MAX_BUFFER_SIZE = 1000 + } +} \ No newline at end of file diff --git a/android/app/src/main/kotlin/com/yubico/authenticator/logging/FlutterLog.kt b/android/app/src/main/kotlin/com/yubico/authenticator/logging/FlutterLog.kt index 6d095c6a..71f86fac 100644 --- a/android/app/src/main/kotlin/com/yubico/authenticator/logging/FlutterLog.kt +++ b/android/app/src/main/kotlin/com/yubico/authenticator/logging/FlutterLog.kt @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 Yubico. + * Copyright (C) 2022-2023 Yubico. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,11 +18,18 @@ package com.yubico.authenticator.logging import io.flutter.plugin.common.BinaryMessenger import io.flutter.plugin.common.MethodChannel +import org.slf4j.Logger +import org.slf4j.LoggerFactory class FlutterLog(messenger: BinaryMessenger) { private var channel = MethodChannel(messenger, "android.log.redirect") + private val bufferAppender = + (LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME) as ch.qos.logback.classic.Logger) + .getAppender("buffer") as BufferAppender + init { + channel.setMethodCallHandler { call, result -> when (call.method) { @@ -53,7 +60,7 @@ class FlutterLog(messenger: BinaryMessenger) { result.success(null) } "getLogs" -> { - result.success(Log.getBuffer()) + result.success(bufferAppender.getLogBuffer()) } else -> { result.notImplemented() diff --git a/android/app/src/main/kotlin/com/yubico/authenticator/logging/Log.kt b/android/app/src/main/kotlin/com/yubico/authenticator/logging/Log.kt index ce11c8a4..1250bd9e 100644 --- a/android/app/src/main/kotlin/com/yubico/authenticator/logging/Log.kt +++ b/android/app/src/main/kotlin/com/yubico/authenticator/logging/Log.kt @@ -24,7 +24,7 @@ import org.slf4j.LoggerFactory object Log { - private val logger = LoggerFactory.getLogger("com.yubico.authenticator") + private val logger = LoggerFactory.getLogger("com.yubico.authenticator.Log") enum class LogLevel { TRAFFIC, @@ -34,13 +34,6 @@ object Log { ERROR } - private const val MAX_BUFFER_SIZE = 1000 - private val buffer = arrayListOf() - - fun getBuffer() : List { - return buffer - } - private var level = if (BuildConfig.DEBUG) { LogLevel.DEBUG } else { @@ -56,17 +49,10 @@ object Log { return } - if (buffer.size > MAX_BUFFER_SIZE) { - buffer.removeAt(0) - } - val logMessage = (if (error == null) - "[$loggerName] ${level.name}: $message" + "$message [$loggerName]" else - "[$loggerName] ${level.name}: $message (err: $error)" - ).also { - buffer.add(it) - } + "$message [$loggerName] (err: $error)") when (level) { LogLevel.TRAFFIC -> logger.trace(logMessage)