refactor(logging): introduce custom FastLogger and replace direct Android logging
This commit is contained in:
@@ -2,7 +2,6 @@ package dev.meloda.fast
|
||||
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.util.Log
|
||||
import androidx.appcompat.app.AppCompatDelegate
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.os.LocaleListCompat
|
||||
@@ -23,6 +22,7 @@ import dev.meloda.fast.datastore.AppSettings
|
||||
import dev.meloda.fast.datastore.UserSettings
|
||||
import dev.meloda.fast.domain.GetCurrentAccountUseCase
|
||||
import dev.meloda.fast.domain.LoadUserByIdUseCase
|
||||
import dev.meloda.fast.logger.FastLogger
|
||||
import dev.meloda.fast.model.BaseError
|
||||
import dev.meloda.fast.model.api.domain.VkUser
|
||||
import dev.meloda.fast.navigation.Main
|
||||
@@ -67,7 +67,8 @@ class MainViewModelImpl(
|
||||
private val getCurrentAccountUseCase: GetCurrentAccountUseCase,
|
||||
private val loadUserByIdUseCase: LoadUserByIdUseCase,
|
||||
private val userSettings: UserSettings,
|
||||
private val longPollController: LongPollController
|
||||
private val longPollController: LongPollController,
|
||||
private val logger: FastLogger
|
||||
) : MainViewModel, ViewModel() {
|
||||
|
||||
override val startDestination = MutableStateFlow<Any?>(null)
|
||||
@@ -203,7 +204,10 @@ class MainViewModelImpl(
|
||||
viewModelScope.launch(Dispatchers.IO) {
|
||||
val currentAccount = getCurrentAccountUseCase()
|
||||
|
||||
Log.d("MainViewModel", "currentAccount: $currentAccount")
|
||||
logger.debug(
|
||||
this@MainViewModelImpl::class,
|
||||
"loadAccounts(): currentAccount: $currentAccount"
|
||||
)
|
||||
|
||||
listenLongPollState()
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@ import com.skydoves.compose.stability.runtime.ComposeStabilityAnalyzer
|
||||
import dev.meloda.fast.auth.BuildConfig
|
||||
import dev.meloda.fast.common.di.applicationModule
|
||||
import dev.meloda.fast.datastore.AppSettings
|
||||
import dev.meloda.fast.logger.FastLogLevel
|
||||
import dev.meloda.fast.logger.FastLogger
|
||||
import dev.meloda.fast.presentation.CrashActivity
|
||||
import org.koin.android.ext.android.get
|
||||
import org.koin.android.ext.koin.androidContext
|
||||
@@ -30,6 +32,14 @@ class AppGlobal : Application(), ImageLoaderFactory {
|
||||
|
||||
initKoin()
|
||||
initCrashHandler()
|
||||
|
||||
val logLevel =
|
||||
if (BuildConfig.DEBUG) FastLogLevel.DEBUG
|
||||
else FastLogLevel.ERROR
|
||||
|
||||
get<FastLogger>()
|
||||
.apply { setLogLevel(logLevel) }
|
||||
.also { FastLogger.setInstance(it) }
|
||||
}
|
||||
|
||||
override fun newImageLoader(): ImageLoader = get()
|
||||
|
||||
@@ -19,6 +19,7 @@ import dev.meloda.fast.convos.di.createChatModule
|
||||
import dev.meloda.fast.domain.di.domainModule
|
||||
import dev.meloda.fast.friends.di.friendsModule
|
||||
import dev.meloda.fast.languagepicker.di.languagePickerModule
|
||||
import dev.meloda.fast.logger.loggerModule
|
||||
import dev.meloda.fast.messageshistory.di.messagesHistoryModule
|
||||
import dev.meloda.fast.photoviewer.di.photoViewModule
|
||||
import dev.meloda.fast.profile.di.profileModule
|
||||
@@ -49,6 +50,8 @@ val applicationModule = module {
|
||||
createChatModule
|
||||
)
|
||||
|
||||
includes(loggerModule)
|
||||
|
||||
// TODO: 14/05/2024, Danil Nikolaev: extract all operations with preferences to standalone class
|
||||
singleOf(PreferenceManager::getDefaultSharedPreferences)
|
||||
single<Resources> { androidContext().resources }
|
||||
|
||||
@@ -9,12 +9,11 @@ import android.content.res.Configuration
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.provider.Settings
|
||||
import android.util.Log
|
||||
import androidx.activity.SystemBarStyle
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.CompositionLocalProvider
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.graphics.toArgb
|
||||
import androidx.core.content.ContextCompat
|
||||
@@ -24,10 +23,13 @@ import dev.meloda.fast.MainViewModel
|
||||
import dev.meloda.fast.MainViewModelImpl
|
||||
import dev.meloda.fast.common.AppConstants
|
||||
import dev.meloda.fast.datastore.AppSettings
|
||||
import dev.meloda.fast.logger.FastLogger
|
||||
import dev.meloda.fast.service.OnlineService
|
||||
import dev.meloda.fast.service.longpolling.LongPollingService
|
||||
import dev.meloda.fast.ui.R
|
||||
import dev.meloda.fast.ui.common.LocalLogger
|
||||
import org.koin.androidx.compose.koinViewModel
|
||||
import org.koin.compose.koinInject
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
@@ -64,25 +66,26 @@ class MainActivity : AppCompatActivity() {
|
||||
requestNotificationPermissions()
|
||||
|
||||
setContent {
|
||||
val viewModel: MainViewModel = koinViewModel<MainViewModelImpl>()
|
||||
LaunchedEffect(viewModel) {
|
||||
Log.d("VM_CREATE", "onCreate: viewModel: $viewModel")
|
||||
}
|
||||
val logger: FastLogger = koinInject()
|
||||
|
||||
val viewModel: MainViewModel = koinViewModel<MainViewModelImpl>()
|
||||
LifecycleResumeEffect(true) {
|
||||
viewModel.onAppResumed(intent)
|
||||
onPauseOrDispose {}
|
||||
}
|
||||
|
||||
RootScreen(
|
||||
toggleLongPollService = { enable, inBackground ->
|
||||
toggleLongPollService(
|
||||
enable = enable,
|
||||
inBackground = inBackground ?: AppSettings.Experimental.longPollInBackground
|
||||
)
|
||||
},
|
||||
toggleOnlineService = ::toggleOnlineService
|
||||
)
|
||||
CompositionLocalProvider(LocalLogger provides logger) {
|
||||
RootScreen(
|
||||
toggleLongPollService = { enable, inBackground ->
|
||||
toggleLongPollService(
|
||||
enable = enable,
|
||||
inBackground = inBackground
|
||||
?: AppSettings.Experimental.longPollInBackground
|
||||
)
|
||||
},
|
||||
toggleOnlineService = ::toggleOnlineService
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ import android.Manifest
|
||||
import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.provider.Settings
|
||||
import android.util.Log
|
||||
import androidx.activity.compose.LocalActivity
|
||||
import androidx.compose.animation.core.tween
|
||||
import androidx.compose.animation.fadeIn
|
||||
@@ -63,6 +62,7 @@ import dev.meloda.fast.photoviewer.presentation.PhotoViewDialog
|
||||
import dev.meloda.fast.settings.navigation.navigateToSettings
|
||||
import dev.meloda.fast.settings.navigation.settingsScreen
|
||||
import dev.meloda.fast.ui.R
|
||||
import dev.meloda.fast.ui.common.LocalLogger
|
||||
import dev.meloda.fast.ui.common.LocalSizeConfig
|
||||
import dev.meloda.fast.ui.model.DeviceSize
|
||||
import dev.meloda.fast.ui.model.SizeConfig
|
||||
@@ -83,6 +83,7 @@ fun RootScreen(
|
||||
toggleLongPollService: (enable: Boolean, inBackground: Boolean?) -> Unit,
|
||||
toggleOnlineService: (enable: Boolean) -> Unit
|
||||
) {
|
||||
val logger = LocalLogger.current
|
||||
val resources = LocalResources.current
|
||||
|
||||
val userSettings: UserSettings = koinInject()
|
||||
@@ -92,10 +93,6 @@ fun RootScreen(
|
||||
val longPollStateToApply by longPollController.stateToApply.collectAsStateWithLifecycle()
|
||||
|
||||
val viewModel: MainViewModel = koinViewModel<MainViewModelImpl>()
|
||||
LaunchedEffect(viewModel) {
|
||||
Log.d("VM_CREATE", "RootScreen(): viewModel: $viewModel")
|
||||
}
|
||||
|
||||
val currentUser: VkUser? by viewModel.currentUser.collectAsStateWithLifecycle()
|
||||
|
||||
val permissionState =
|
||||
@@ -126,13 +123,12 @@ fun RootScreen(
|
||||
}
|
||||
|
||||
LifecycleResumeEffect(longPollStateToApply) {
|
||||
Log.d("LongPollMainActivity", "longPollStateToApply: $longPollStateToApply")
|
||||
logger.debug("RootScreen", "longPollStateToApply: $longPollStateToApply")
|
||||
if (longPollStateToApply != LongPollState.Background) {
|
||||
if (longPollStateToApply.isLaunched() && longPollCurrentState.isLaunched()
|
||||
&& longPollCurrentState != longPollStateToApply
|
||||
) {
|
||||
toggleLongPollService(false, null)
|
||||
Log.d("LongPoll", "recreate()")
|
||||
}
|
||||
|
||||
toggleLongPollService(
|
||||
|
||||
@@ -6,8 +6,9 @@ import android.os.IBinder
|
||||
import android.util.Log
|
||||
import dev.meloda.fast.common.extensions.createTimerFlow
|
||||
import dev.meloda.fast.data.UserConfig
|
||||
import dev.meloda.fast.domain.AccountUseCase
|
||||
import dev.meloda.fast.data.processState
|
||||
import dev.meloda.fast.domain.AccountUseCase
|
||||
import dev.meloda.fast.logger.FastLogger
|
||||
import kotlinx.coroutines.CoroutineExceptionHandler
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -24,11 +25,12 @@ import kotlin.time.Duration.Companion.minutes
|
||||
|
||||
class OnlineService : Service() {
|
||||
|
||||
private val logger: FastLogger by inject()
|
||||
|
||||
private val job = SupervisorJob()
|
||||
|
||||
private val exceptionHandler = CoroutineExceptionHandler { _, throwable ->
|
||||
Log.d(TAG, "error: $throwable")
|
||||
throwable.printStackTrace()
|
||||
logger.error(this::class.java, "CoroutineException", throwable)
|
||||
}
|
||||
|
||||
private val coroutineContext: CoroutineContext
|
||||
@@ -42,17 +44,20 @@ class OnlineService : Service() {
|
||||
private var onlineJob: Job? = null
|
||||
|
||||
override fun onBind(intent: Intent?): IBinder? {
|
||||
Log.d(STATE_TAG, "onBind: intent: $intent")
|
||||
logger.debug(this::class, "STATE: onBind(): intent: $intent")
|
||||
return null
|
||||
}
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
if (startId > 1) return START_STICKY
|
||||
|
||||
Log.d(STATE_TAG, "onStartCommand: flags: $flags; startId: $startId\ninstance: $this")
|
||||
logger.debug(
|
||||
this::class,
|
||||
"STATE: onStartCommand(): flags: %s; startId: %s;\ninstance: %s"
|
||||
.format("$flags", "$startId", "$this")
|
||||
)
|
||||
|
||||
createTimer()
|
||||
|
||||
return START_STICKY
|
||||
}
|
||||
|
||||
@@ -68,13 +73,13 @@ class OnlineService : Service() {
|
||||
private fun setOnline() {
|
||||
if (onlineJob != null) return
|
||||
|
||||
Log.d(TAG, "setOnline()")
|
||||
logger.debug(this::class, "setOnline()")
|
||||
|
||||
onlineJob = coroutineScope.launch {
|
||||
val token = UserConfig.fastToken ?: UserConfig.accessToken
|
||||
|
||||
if (token.isBlank()) {
|
||||
Log.d(TAG, "setOnline: token is empty")
|
||||
logger.debug(this::class, "setOnline(): token is empty")
|
||||
return@launch
|
||||
}
|
||||
|
||||
@@ -84,10 +89,10 @@ class OnlineService : Service() {
|
||||
).onEach { state ->
|
||||
state.processState(
|
||||
error = { error ->
|
||||
Log.w(TAG, "setOnline(): error: $error")
|
||||
logger.error(this@OnlineService::class, "setOnline(): ERROR: $error")
|
||||
},
|
||||
success = { response ->
|
||||
Log.d(TAG, "setOnline(): success: $response")
|
||||
logger.debug(this@OnlineService::class, "setOnline(): response: $response")
|
||||
}
|
||||
)
|
||||
}.collect()
|
||||
@@ -96,7 +101,7 @@ class OnlineService : Service() {
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
Log.d(STATE_TAG, "onDestroy")
|
||||
logger.debug(this::class, "onDestroy()")
|
||||
|
||||
timerJob?.cancel("OnlineService destroyed")
|
||||
onlineJob?.cancel("OnlineService destroyed")
|
||||
|
||||
@@ -7,7 +7,6 @@ import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.IBinder
|
||||
import android.provider.Settings
|
||||
import android.util.Log
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.ServiceCompat
|
||||
import com.conena.nanokt.android.app.stopForegroundCompat
|
||||
@@ -22,6 +21,7 @@ import dev.meloda.fast.datastore.AppSettings
|
||||
import dev.meloda.fast.domain.LongPollEventsHandler
|
||||
import dev.meloda.fast.domain.LongPollUpdatesParser
|
||||
import dev.meloda.fast.domain.LongPollUseCase
|
||||
import dev.meloda.fast.logger.FastLogger
|
||||
import dev.meloda.fast.model.api.data.LongPollUpdates
|
||||
import dev.meloda.fast.model.api.data.VkLongPollData
|
||||
import dev.meloda.fast.ui.R
|
||||
@@ -41,6 +41,8 @@ import kotlin.time.Duration.Companion.seconds
|
||||
|
||||
class LongPollingService : Service() {
|
||||
|
||||
private val logger: FastLogger by inject()
|
||||
|
||||
private val longPollController: LongPollController by inject()
|
||||
|
||||
private val job = SupervisorJob()
|
||||
@@ -65,20 +67,21 @@ class LongPollingService : Service() {
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
Log.d(STATE_TAG, "onCreate()")
|
||||
logger.debug(this::class, "STATE: onCreate()")
|
||||
}
|
||||
|
||||
override fun onBind(intent: Intent?): IBinder? {
|
||||
Log.d(STATE_TAG, "onBind: intent: $intent")
|
||||
logger.debug(this::class, "STATE: onBind(): intent: $intent")
|
||||
return null
|
||||
}
|
||||
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
if (startId > 1) return START_STICKY
|
||||
|
||||
Log.d(
|
||||
STATE_TAG,
|
||||
"onStartCommand: asForeground: $inBackground; flags: $flags; startId: $startId;\ninstance: $this"
|
||||
logger.debug(
|
||||
this::class,
|
||||
"STATE: onStartCommand(): asForeground: %s; flags: %s; startId: %s;\ninstance: %s"
|
||||
.format("$inBackground", "$flags", "$startId", "$this")
|
||||
)
|
||||
|
||||
startJob()
|
||||
@@ -133,11 +136,15 @@ class LongPollingService : Service() {
|
||||
|
||||
private fun startPolling(): Job {
|
||||
if (job.isCompleted || job.isCancelled) {
|
||||
Log.d(STATE_TAG, "Job is completed or cancelled")
|
||||
logger.debug(
|
||||
this::class,
|
||||
"startPolling(): Job is already done. isCompleted: %s; isCancelled: %s"
|
||||
.format("${job.isCompleted}", "${job.isCancelled}")
|
||||
)
|
||||
throw Exception("Job is over")
|
||||
}
|
||||
|
||||
Log.d(STATE_TAG, "Starting job...")
|
||||
logger.debug(this::class, "startPolling(): Starting job.")
|
||||
|
||||
return coroutineScope.launch(coroutineContext) {
|
||||
longPollController.updateCurrentState(
|
||||
@@ -213,11 +220,11 @@ class LongPollingService : Service() {
|
||||
).listenValue(coroutineScope) { state ->
|
||||
state.processState(
|
||||
success = { response ->
|
||||
Log.d(TAG, "getServerInfo: serverInfoResponse: $response")
|
||||
logger.debug(this::class, "getServerInfo(): response: $response")
|
||||
it.resume(response)
|
||||
},
|
||||
error = { error ->
|
||||
Log.e(TAG, "getServerInfo: $error")
|
||||
logger.error(this::class, "getServerInfo(): ERROR: $error")
|
||||
it.resume(null)
|
||||
}
|
||||
)
|
||||
@@ -237,11 +244,11 @@ class LongPollingService : Service() {
|
||||
).listenValue(coroutineScope) { state ->
|
||||
state.processState(
|
||||
success = { response ->
|
||||
Log.d(TAG, "lastUpdateResponse: $response")
|
||||
logger.debug(this::class, "getUpdatesResponse(): response: $response")
|
||||
it.resume(response)
|
||||
},
|
||||
error = { error ->
|
||||
Log.d(TAG, "getUpdatesResponse: error: $error")
|
||||
logger.debug(this::class, "getUpdatesResponse(): error: $error")
|
||||
it.resume(null)
|
||||
}
|
||||
)
|
||||
@@ -254,7 +261,7 @@ class LongPollingService : Service() {
|
||||
}
|
||||
|
||||
private fun handleError(throwable: Throwable) {
|
||||
Log.e(TAG, "error: $throwable")
|
||||
logger.error(this::class, "CoroutineException", throwable)
|
||||
|
||||
if (throwable !is NoAccessTokenException) {
|
||||
throwable.printStackTrace()
|
||||
@@ -269,7 +276,7 @@ class LongPollingService : Service() {
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
Log.d(STATE_TAG, "onDestroy")
|
||||
logger.debug(this::class, "STATE: onDestroy()")
|
||||
longPollController.updateCurrentState(LongPollState.Stopped)
|
||||
try {
|
||||
AppSettings.edit { putBoolean(KEY_LONG_POLL_WAS_DESTROYED, true) }
|
||||
@@ -281,7 +288,7 @@ class LongPollingService : Service() {
|
||||
}
|
||||
|
||||
override fun onTrimMemory(level: Int) {
|
||||
Log.d(STATE_TAG, "onTrimMemory. Level: $level")
|
||||
logger.debug(this::class, "STATE: onTrimMemory(): Level: $level")
|
||||
super.onTrimMemory(level)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user