Updated on 2026-08-14
This commit is contained in:
parent
f748b8539d
commit
434c459728
13 changed files with 103 additions and 114 deletions
|
|
@ -0,0 +1,48 @@
|
|||
package com.tangem.tap.common.log
|
||||
|
||||
import android.util.Log
|
||||
import com.orhanobut.logger.AndroidLogAdapter
|
||||
import com.orhanobut.logger.Logger
|
||||
import com.tangem.domain.settings.repositories.SettingsRepository
|
||||
import com.tangem.wallet.BuildConfig
|
||||
import timber.log.Timber
|
||||
|
||||
/**
|
||||
* Tangem app logger
|
||||
*
|
||||
* @property settingsRepository repository for saving logs
|
||||
*
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
class TangemAppLoggerInitializer(
|
||||
private val settingsRepository: SettingsRepository,
|
||||
) {
|
||||
|
||||
/** Initialize */
|
||||
fun initialize() {
|
||||
if (IS_LOG_ENABLED) {
|
||||
Logger.addLogAdapter(AndroidLogAdapter(TimberFormatStrategy()))
|
||||
}
|
||||
|
||||
Timber.plant(tree = createTimberTree())
|
||||
}
|
||||
|
||||
private fun createTimberTree(): Timber.Tree {
|
||||
return object : Timber.DebugTree() {
|
||||
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
|
||||
if (IS_LOG_ENABLED) {
|
||||
Logger.log(priority, tag, message, t)
|
||||
}
|
||||
|
||||
if (PERMITTED_PRIORITY.contains(priority)) {
|
||||
settingsRepository.saveLogMessage(message)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private companion object {
|
||||
val IS_LOG_ENABLED: Boolean = BuildConfig.LOG_ENABLED
|
||||
val PERMITTED_PRIORITY = listOf(Log.ERROR, Log.INFO)
|
||||
}
|
||||
}
|
||||
|
|
@ -4,11 +4,6 @@ import com.tangem.Log
|
|||
import com.tangem.LogFormat
|
||||
import com.tangem.TangemSdkLogger
|
||||
import com.tangem.domain.settings.repositories.SettingsRepository
|
||||
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
|
||||
/**
|
||||
* CardSDK logger implementation
|
||||
|
|
@ -16,7 +11,6 @@ import kotlinx.coroutines.sync.withLock
|
|||
* @property levels logging levels
|
||||
* @property messageFormatter message formatter
|
||||
* @property settingsRepository settings repository
|
||||
* @property dispatchers coroutine dispatchers provider
|
||||
*
|
||||
[REDACTED_AUTHOR]
|
||||
*/
|
||||
|
|
@ -24,19 +18,11 @@ internal class TangemCardSDKLogger(
|
|||
private val levels: List<Log.Level>,
|
||||
private val messageFormatter: LogFormat,
|
||||
private val settingsRepository: SettingsRepository,
|
||||
private val dispatchers: CoroutineDispatcherProvider,
|
||||
) : TangemSdkLogger {
|
||||
|
||||
private val scope = CoroutineScope(dispatchers.main)
|
||||
private val mutex = Mutex()
|
||||
|
||||
override fun log(message: () -> String, level: Log.Level) {
|
||||
if (!levels.contains(level)) return
|
||||
|
||||
scope.launch(dispatchers.main) {
|
||||
mutex.withLock {
|
||||
settingsRepository.updateAppLogs(message = messageFormatter.format(message, level))
|
||||
}
|
||||
}
|
||||
settingsRepository.saveLogMessage(message = messageFormatter.format(message, level))
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue