refactor: extract SLF4J loggers for re-use

This commit is contained in:
Harsh Shandilya 2023-01-28 17:44:31 +05:30
parent ce54200092
commit ca032a1737
No known key found for this signature in database
4 changed files with 143 additions and 132 deletions

View file

@ -4,6 +4,7 @@
*/ */
package app.passwordstore.util.git.sshj package app.passwordstore.util.git.sshj
import app.passwordstore.util.log.LogcatLogger
import com.github.michaelbull.result.runCatching import com.github.michaelbull.result.runCatching
import com.hierynomus.sshj.key.KeyAlgorithms import com.hierynomus.sshj.key.KeyAlgorithms
import com.hierynomus.sshj.transport.cipher.BlockCiphers import com.hierynomus.sshj.transport.cipher.BlockCiphers
@ -12,11 +13,7 @@ import com.hierynomus.sshj.transport.kex.ExtInfoClientFactory
import com.hierynomus.sshj.transport.mac.Macs import com.hierynomus.sshj.transport.mac.Macs
import com.hierynomus.sshj.userauth.keyprovider.OpenSSHKeyV1KeyFile import com.hierynomus.sshj.userauth.keyprovider.OpenSSHKeyV1KeyFile
import java.security.Security import java.security.Security
import logcat.LogPriority.ERROR
import logcat.LogPriority.INFO
import logcat.LogPriority.VERBOSE import logcat.LogPriority.VERBOSE
import logcat.LogPriority.WARN
import logcat.asLog
import logcat.logcat import logcat.logcat
import net.schmizz.keepalive.KeepAliveProvider import net.schmizz.keepalive.KeepAliveProvider
import net.schmizz.sshj.ConfigImpl import net.schmizz.sshj.ConfigImpl
@ -34,7 +31,6 @@ import net.schmizz.sshj.userauth.keyprovider.PKCS8KeyFile
import net.schmizz.sshj.userauth.keyprovider.PuTTYKeyFile import net.schmizz.sshj.userauth.keyprovider.PuTTYKeyFile
import org.bouncycastle.jce.provider.BouncyCastleProvider import org.bouncycastle.jce.provider.BouncyCastleProvider
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.Marker
fun setUpBouncyCastleForSshj() { fun setUpBouncyCastleForSshj() {
// Replace the Android BC provider with the Java BouncyCastle provider since the former does // Replace the Android BC provider with the Java BouncyCastle provider since the former does
@ -60,132 +56,7 @@ fun setUpBouncyCastleForSshj() {
SecurityUtils.setSecurityProvider(null) SecurityUtils.setSecurityProvider(null)
} }
private abstract class AbstractLogger(private val name: String) : Logger { private object LogcatLoggerFactory : LoggerFactory {
abstract fun t(message: String, t: Throwable? = null, vararg args: Any?)
abstract fun d(message: String, t: Throwable? = null, vararg args: Any?)
abstract fun i(message: String, t: Throwable? = null, vararg args: Any?)
abstract fun w(message: String, t: Throwable? = null, vararg args: Any?)
abstract fun e(message: String, t: Throwable? = null, vararg args: Any?)
override fun getName() = name
override fun isTraceEnabled(marker: Marker?): Boolean = isTraceEnabled
override fun isDebugEnabled(marker: Marker?): Boolean = isDebugEnabled
override fun isInfoEnabled(marker: Marker?): Boolean = isInfoEnabled
override fun isWarnEnabled(marker: Marker?): Boolean = isWarnEnabled
override fun isErrorEnabled(marker: Marker?): Boolean = isErrorEnabled
override fun trace(msg: String) = t(msg)
override fun trace(format: String, arg: Any?) = t(format, null, arg)
override fun trace(format: String, arg1: Any?, arg2: Any?) = t(format, null, arg1, arg2)
override fun trace(format: String, vararg arguments: Any?) = t(format, null, *arguments)
override fun trace(msg: String, t: Throwable?) = t(msg, t)
override fun trace(marker: Marker, msg: String) = trace(msg)
override fun trace(marker: Marker?, format: String, arg: Any?) = trace(format, arg)
override fun trace(marker: Marker?, format: String, arg1: Any?, arg2: Any?) =
trace(format, arg1, arg2)
override fun trace(marker: Marker?, format: String, vararg arguments: Any?) =
trace(format, *arguments)
override fun trace(marker: Marker?, msg: String, t: Throwable?) = trace(msg, t)
override fun debug(msg: String) = d(msg)
override fun debug(format: String, arg: Any?) = d(format, null, arg)
override fun debug(format: String, arg1: Any?, arg2: Any?) = d(format, null, arg1, arg2)
override fun debug(format: String, vararg arguments: Any?) = d(format, null, *arguments)
override fun debug(msg: String, t: Throwable?) = d(msg, t)
override fun debug(marker: Marker, msg: String) = debug(msg)
override fun debug(marker: Marker?, format: String, arg: Any?) = debug(format, arg)
override fun debug(marker: Marker?, format: String, arg1: Any?, arg2: Any?) =
debug(format, arg1, arg2)
override fun debug(marker: Marker?, format: String, vararg arguments: Any?) =
debug(format, *arguments)
override fun debug(marker: Marker?, msg: String, t: Throwable?) = debug(msg, t)
override fun info(msg: String) = i(msg)
override fun info(format: String, arg: Any?) = i(format, null, arg)
override fun info(format: String, arg1: Any?, arg2: Any?) = i(format, null, arg1, arg2)
override fun info(format: String, vararg arguments: Any?) = i(format, null, *arguments)
override fun info(msg: String, t: Throwable?) = i(msg, t)
override fun info(marker: Marker, msg: String) = info(msg)
override fun info(marker: Marker?, format: String, arg: Any?) = info(format, arg)
override fun info(marker: Marker?, format: String, arg1: Any?, arg2: Any?) =
info(format, arg1, arg2)
override fun info(marker: Marker?, format: String, vararg arguments: Any?) =
info(format, *arguments)
override fun info(marker: Marker?, msg: String, t: Throwable?) = info(msg, t)
override fun warn(msg: String) = w(msg)
override fun warn(format: String, arg: Any?) = w(format, null, arg)
override fun warn(format: String, arg1: Any?, arg2: Any?) = w(format, null, arg1, arg2)
override fun warn(format: String, vararg arguments: Any?) = w(format, null, *arguments)
override fun warn(msg: String, t: Throwable?) = w(msg, t)
override fun warn(marker: Marker, msg: String) = warn(msg)
override fun warn(marker: Marker?, format: String, arg: Any?) = warn(format, arg)
override fun warn(marker: Marker?, format: String, arg1: Any?, arg2: Any?) =
warn(format, arg1, arg2)
override fun warn(marker: Marker?, format: String, vararg arguments: Any?) =
warn(format, *arguments)
override fun warn(marker: Marker?, msg: String, t: Throwable?) = warn(msg, t)
override fun error(msg: String) = e(msg)
override fun error(format: String, arg: Any?) = e(format, null, arg)
override fun error(format: String, arg1: Any?, arg2: Any?) = e(format, null, arg1, arg2)
override fun error(format: String, vararg arguments: Any?) = e(format, null, *arguments)
override fun error(msg: String, t: Throwable?) = e(msg, t)
override fun error(marker: Marker, msg: String) = error(msg)
override fun error(marker: Marker?, format: String, arg: Any?) = error(format, arg)
override fun error(marker: Marker?, format: String, arg1: Any?, arg2: Any?) =
error(format, arg1, arg2)
override fun error(marker: Marker?, format: String, vararg arguments: Any?) =
error(format, *arguments)
override fun error(marker: Marker?, msg: String, t: Throwable?) = error(msg, t)
}
object LogcatLoggerFactory : LoggerFactory {
private class LogcatLogger(name: String) : AbstractLogger(name) {
override fun isTraceEnabled() = true
override fun isDebugEnabled() = true
override fun isInfoEnabled() = true
override fun isWarnEnabled() = true
override fun isErrorEnabled() = true
// Replace slf4j's "{}" format string style with standard Java's "%s".
// The supposedly redundant escape on the } is not redundant.
@Suppress("RegExpRedundantEscape")
private fun String.fix() = replace("""(?!<\\)\{\}""".toRegex(), "%s")
override fun t(message: String, t: Throwable?, vararg args: Any?) {
logcat(name, VERBOSE) { message.fix().format(*args) + (t?.asLog() ?: "") }
}
override fun d(message: String, t: Throwable?, vararg args: Any?) {
logcat(name) { message.fix().format(*args) + (t?.asLog() ?: "") }
}
override fun i(message: String, t: Throwable?, vararg args: Any?) {
logcat(name, INFO) { message.fix().format(*args) + (t?.asLog() ?: "") }
}
override fun w(message: String, t: Throwable?, vararg args: Any?) {
logcat(name, WARN) { message.fix().format(*args) + (t?.asLog() ?: "") }
}
override fun e(message: String, t: Throwable?, vararg args: Any?) {
logcat(name, ERROR) { message.fix().format(*args) + (t?.asLog() ?: "") }
}
}
override fun getLogger(name: String): Logger { override fun getLogger(name: String): Logger {
return LogcatLogger(name) return LogcatLogger(name)

View file

@ -0,0 +1,100 @@
package app.passwordstore.util.log
import org.slf4j.Logger
import org.slf4j.Marker
/**
* A bridge class to ease the implementation of the default SLF4J [Logger] interface for Android
* users.
*/
abstract class AbstractLogger(private val name: String) : Logger {
abstract fun t(message: String, t: Throwable? = null, vararg args: Any?)
abstract fun d(message: String, t: Throwable? = null, vararg args: Any?)
abstract fun i(message: String, t: Throwable? = null, vararg args: Any?)
abstract fun w(message: String, t: Throwable? = null, vararg args: Any?)
abstract fun e(message: String, t: Throwable? = null, vararg args: Any?)
override fun getName() = name
override fun isTraceEnabled(marker: Marker?): Boolean = isTraceEnabled
override fun isDebugEnabled(marker: Marker?): Boolean = isDebugEnabled
override fun isInfoEnabled(marker: Marker?): Boolean = isInfoEnabled
override fun isWarnEnabled(marker: Marker?): Boolean = isWarnEnabled
override fun isErrorEnabled(marker: Marker?): Boolean = isErrorEnabled
override fun trace(msg: String) = t(msg)
override fun trace(format: String, arg: Any?) = t(format, null, arg)
override fun trace(format: String, arg1: Any?, arg2: Any?) = t(format, null, arg1, arg2)
override fun trace(format: String, vararg arguments: Any?) = t(format, null, *arguments)
override fun trace(msg: String, t: Throwable?) = t(msg, t)
override fun trace(marker: Marker, msg: String) = trace(msg)
override fun trace(marker: Marker?, format: String, arg: Any?) = trace(format, arg)
override fun trace(marker: Marker?, format: String, arg1: Any?, arg2: Any?) =
trace(format, arg1, arg2)
override fun trace(marker: Marker?, format: String, vararg arguments: Any?) =
trace(format, *arguments)
override fun trace(marker: Marker?, msg: String, t: Throwable?) = trace(msg, t)
override fun debug(msg: String) = d(msg)
override fun debug(format: String, arg: Any?) = d(format, null, arg)
override fun debug(format: String, arg1: Any?, arg2: Any?) = d(format, null, arg1, arg2)
override fun debug(format: String, vararg arguments: Any?) = d(format, null, *arguments)
override fun debug(msg: String, t: Throwable?) = d(msg, t)
override fun debug(marker: Marker, msg: String) = debug(msg)
override fun debug(marker: Marker?, format: String, arg: Any?) = debug(format, arg)
override fun debug(marker: Marker?, format: String, arg1: Any?, arg2: Any?) =
debug(format, arg1, arg2)
override fun debug(marker: Marker?, format: String, vararg arguments: Any?) =
debug(format, *arguments)
override fun debug(marker: Marker?, msg: String, t: Throwable?) = debug(msg, t)
override fun info(msg: String) = i(msg)
override fun info(format: String, arg: Any?) = i(format, null, arg)
override fun info(format: String, arg1: Any?, arg2: Any?) = i(format, null, arg1, arg2)
override fun info(format: String, vararg arguments: Any?) = i(format, null, *arguments)
override fun info(msg: String, t: Throwable?) = i(msg, t)
override fun info(marker: Marker, msg: String) = info(msg)
override fun info(marker: Marker?, format: String, arg: Any?) = info(format, arg)
override fun info(marker: Marker?, format: String, arg1: Any?, arg2: Any?) =
info(format, arg1, arg2)
override fun info(marker: Marker?, format: String, vararg arguments: Any?) =
info(format, *arguments)
override fun info(marker: Marker?, msg: String, t: Throwable?) = info(msg, t)
override fun warn(msg: String) = w(msg)
override fun warn(format: String, arg: Any?) = w(format, null, arg)
override fun warn(format: String, arg1: Any?, arg2: Any?) = w(format, null, arg1, arg2)
override fun warn(format: String, vararg arguments: Any?) = w(format, null, *arguments)
override fun warn(msg: String, t: Throwable?) = w(msg, t)
override fun warn(marker: Marker, msg: String) = warn(msg)
override fun warn(marker: Marker?, format: String, arg: Any?) = warn(format, arg)
override fun warn(marker: Marker?, format: String, arg1: Any?, arg2: Any?) =
warn(format, arg1, arg2)
override fun warn(marker: Marker?, format: String, vararg arguments: Any?) =
warn(format, *arguments)
override fun warn(marker: Marker?, msg: String, t: Throwable?) = warn(msg, t)
override fun error(msg: String) = e(msg)
override fun error(format: String, arg: Any?) = e(format, null, arg)
override fun error(format: String, arg1: Any?, arg2: Any?) = e(format, null, arg1, arg2)
override fun error(format: String, vararg arguments: Any?) = e(format, null, *arguments)
override fun error(msg: String, t: Throwable?) = e(msg, t)
override fun error(marker: Marker, msg: String) = error(msg)
override fun error(marker: Marker?, format: String, arg: Any?) = error(format, arg)
override fun error(marker: Marker?, format: String, arg1: Any?, arg2: Any?) =
error(format, arg1, arg2)
override fun error(marker: Marker?, format: String, vararg arguments: Any?) =
error(format, *arguments)
override fun error(marker: Marker?, msg: String, t: Throwable?) = error(msg, t)
}

View file

@ -0,0 +1,40 @@
package app.passwordstore.util.log
import logcat.LogPriority
import logcat.asLog
import logcat.logcat
/** An implementation of [AbstractLogger] that forwards logging calls to [logcat]. */
class LogcatLogger(name: String) : AbstractLogger(name) {
override fun isTraceEnabled() = true
override fun isDebugEnabled() = true
override fun isInfoEnabled() = true
override fun isWarnEnabled() = true
override fun isErrorEnabled() = true
// Replace slf4j's "{}" format string style with standard Java's "%s".
// The supposedly redundant escape on the } is not redundant.
@Suppress("RegExpRedundantEscape")
private fun String.fix() = replace("""(?!<\\)\{\}""".toRegex(), "%s")
override fun t(message: String, t: Throwable?, vararg args: Any?) {
logcat(name, LogPriority.VERBOSE) { message.fix().format(*args) + (t?.asLog() ?: "") }
}
override fun d(message: String, t: Throwable?, vararg args: Any?) {
logcat(name) { message.fix().format(*args) + (t?.asLog() ?: "") }
}
override fun i(message: String, t: Throwable?, vararg args: Any?) {
logcat(name, LogPriority.INFO) { message.fix().format(*args) + (t?.asLog() ?: "") }
}
override fun w(message: String, t: Throwable?, vararg args: Any?) {
logcat(name, LogPriority.WARN) { message.fix().format(*args) + (t?.asLog() ?: "") }
}
override fun e(message: String, t: Throwable?, vararg args: Any?) {
logcat(name, LogPriority.ERROR) { message.fix().format(*args) + (t?.asLog() ?: "") }
}
}

View file

@ -81,8 +81,8 @@
<ID>SpreadOperator:ErrorMessages.kt$GitException.PushException$(res, *fmt)</ID> <ID>SpreadOperator:ErrorMessages.kt$GitException.PushException$(res, *fmt)</ID>
<ID>ThrowsCount:GitCommandExecutor.kt$GitCommandExecutor$suspend fun execute(): Result&lt;Unit, Throwable&gt;</ID> <ID>ThrowsCount:GitCommandExecutor.kt$GitCommandExecutor$suspend fun execute(): Result&lt;Unit, Throwable&gt;</ID>
<ID>ThrowsCount:SshKey.kt$SshKey$fun import(uri: Uri)</ID> <ID>ThrowsCount:SshKey.kt$SshKey$fun import(uri: Uri)</ID>
<ID>TooManyFunctions:AbstractLogger.kt$AbstractLogger : Logger</ID>
<ID>TooManyFunctions:PasswordStore.kt$PasswordStore : BaseGitActivity</ID> <ID>TooManyFunctions:PasswordStore.kt$PasswordStore : BaseGitActivity</ID>
<ID>TooManyFunctions:SshjConfig.kt$AbstractLogger : Logger</ID>
<ID>TopLevelPropertyNaming:AutofillMatcher.kt$private const val PREFERENCES_AUTOFILL_APP_MATCHES = "oreo_autofill_app_matches"</ID> <ID>TopLevelPropertyNaming:AutofillMatcher.kt$private const val PREFERENCES_AUTOFILL_APP_MATCHES = "oreo_autofill_app_matches"</ID>
<ID>TopLevelPropertyNaming:AutofillMatcher.kt$private const val PREFERENCES_AUTOFILL_WEB_MATCHES = "oreo_autofill_web_matches"</ID> <ID>TopLevelPropertyNaming:AutofillMatcher.kt$private const val PREFERENCES_AUTOFILL_WEB_MATCHES = "oreo_autofill_web_matches"</ID>
<ID>TopLevelPropertyNaming:PasswordStore.kt$const val PASSWORD_FRAGMENT_TAG = "PasswordsList"</ID> <ID>TopLevelPropertyNaming:PasswordStore.kt$const val PASSWORD_FRAGMENT_TAG = "PasswordsList"</ID>