This commit is contained in:
Adam Velebil 2023-09-25 13:41:15 +02:00
commit a90d118d79
No known key found for this signature in database
GPG Key ID: C9B1E4A3CBBD2E10
4 changed files with 73 additions and 20 deletions

View File

@ -24,8 +24,14 @@
</encoder> </encoder>
</appender> </appender>
<!-- Write TRACE (and higher-level) messages to logcat --> <appender name="buffer" class="com.yubico.authenticator.logging.BufferAppender">
<encoder>
<pattern>%d{HH:mm:ss:SSS} [%thread] %-5level %logger{36} - %X{app} %msg</pattern>
</encoder>
</appender>
<root level="TRACE"> <root level="TRACE">
<appender-ref ref="logcat" /> <appender-ref ref="logcat" />
<appender-ref ref="buffer" />
</root> </root>
</configuration> </configuration>

View File

@ -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<ILoggingEvent>() {
private var encoder: PatternLayoutEncoder? = null
private val buffer = arrayListOf<String>()
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<String> {
return buffer
}
companion object {
private const val MAX_BUFFER_SIZE = 1000
}
}

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2022 Yubico. * Copyright (C) 2022-2023 Yubico.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.BinaryMessenger
import io.flutter.plugin.common.MethodChannel import io.flutter.plugin.common.MethodChannel
import org.slf4j.Logger
import org.slf4j.LoggerFactory
class FlutterLog(messenger: BinaryMessenger) { class FlutterLog(messenger: BinaryMessenger) {
private var channel = MethodChannel(messenger, "android.log.redirect") 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 { init {
channel.setMethodCallHandler { call, result -> channel.setMethodCallHandler { call, result ->
when (call.method) { when (call.method) {
@ -53,7 +60,7 @@ class FlutterLog(messenger: BinaryMessenger) {
result.success(null) result.success(null)
} }
"getLogs" -> { "getLogs" -> {
result.success(Log.getBuffer()) result.success(bufferAppender.getLogBuffer())
} }
else -> { else -> {
result.notImplemented() result.notImplemented()

View File

@ -24,7 +24,7 @@ import org.slf4j.LoggerFactory
object Log { object Log {
private val logger = LoggerFactory.getLogger("com.yubico.authenticator") private val logger = LoggerFactory.getLogger("com.yubico.authenticator.Log")
enum class LogLevel { enum class LogLevel {
TRAFFIC, TRAFFIC,
@ -34,13 +34,6 @@ object Log {
ERROR ERROR
} }
private const val MAX_BUFFER_SIZE = 1000
private val buffer = arrayListOf<String>()
fun getBuffer() : List<String> {
return buffer
}
private var level = if (BuildConfig.DEBUG) { private var level = if (BuildConfig.DEBUG) {
LogLevel.DEBUG LogLevel.DEBUG
} else { } else {
@ -56,17 +49,10 @@ object Log {
return return
} }
if (buffer.size > MAX_BUFFER_SIZE) {
buffer.removeAt(0)
}
val logMessage = (if (error == null) val logMessage = (if (error == null)
"[$loggerName] ${level.name}: $message" "$message [$loggerName]"
else else
"[$loggerName] ${level.name}: $message (err: $error)" "$message [$loggerName] (err: $error)")
).also {
buffer.add(it)
}
when (level) { when (level) {
LogLevel.TRAFFIC -> logger.trace(logMessage) LogLevel.TRAFFIC -> logger.trace(logMessage)