Skip to content

chore: convert startup log to trace #400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions api/src/main/java/com/getcode/db/AppDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.room.migration.AutoMigrationSpec
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.getcode.model.*
import com.getcode.network.repository.decodeBase64
import com.getcode.utils.startupLog
import com.getcode.utils.trace
import com.getcode.vendor.Base58
import io.reactivex.rxjava3.core.BackpressureStrategy
import io.reactivex.rxjava3.subjects.BehaviorSubject
Expand Down Expand Up @@ -73,7 +71,7 @@ object Database {
fun requireInstance() = instance!!

fun init(context: Context, entropyB64: String) {
startupLog("database init start")
trace("database init start")
instance?.close()
val dbUniqueName = Base58.encode(entropyB64.toByteArray().subByteArray(0, 3))
dbName = "$dbNamePrefix-$dbUniqueName$dbNameSuffix"
Expand All @@ -89,7 +87,7 @@ object Database {
instance?.conversationMessageRemoteKeyDao()?.clearRemoteKeys()

isInitSubject.onNext(true)
startupLog("database init end")
trace("database init end")
}

fun close() {
Expand Down
4 changes: 2 additions & 2 deletions api/src/main/java/com/getcode/network/BalanceController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.getcode.solana.organizer.Organizer
import com.getcode.solana.organizer.Tray
import com.getcode.utils.FormatUtils
import com.getcode.utils.network.NetworkConnectivityListener
import com.getcode.utils.startupLog
import com.getcode.utils.trace
import dagger.hilt.android.qualifiers.ApplicationContext
import io.reactivex.rxjava3.core.Completable
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -125,7 +125,7 @@ open class BalanceController @Inject constructor(
}

fun fetchBalance(): Completable {
startupLog("fetchBalance")
trace("fetchBalance")
if (SessionManager.isAuthenticated() != true) {
Timber.d("FetchBalance - Not authenticated")
return Completable.complete()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.getcode.network.client

import android.content.Context
import com.getcode.model.Kin
import com.getcode.model.intents.IntentDeposit
import com.getcode.model.intents.IntentPublicTransfer
import com.getcode.model.intents.IntentReceive
import com.getcode.model.intents.IntentRemoteReceive
Expand All @@ -12,7 +11,7 @@ import com.getcode.solana.organizer.GiftCardAccount
import com.getcode.solana.organizer.Organizer
import com.getcode.solana.organizer.Tray
import com.getcode.utils.ErrorUtils
import com.getcode.utils.startupLog
import com.getcode.utils.trace
import dagger.hilt.android.qualifiers.ApplicationContext
import io.reactivex.rxjava3.core.Completable
import timber.log.Timber
Expand Down Expand Up @@ -122,7 +121,7 @@ class TransactionReceiver @Inject constructor(
}

fun receiveFromIncoming(amount: Kin, organizer: Organizer): Completable {
startupLog("receiveFromIncoming $amount")
trace("receiveFromIncoming $amount")
return transactionRepository.receiveFromIncoming(
context, amount, organizer
).map {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
package com.getcode.network.repository

import android.content.Context
import com.codeinc.gen.account.v1.AccountService
import com.codeinc.gen.chat.v1.ChatService
import com.codeinc.gen.common.v1.Model
import com.getcode.ed25519.Ed25519
import com.getcode.ed25519.Ed25519.KeyPair
import com.getcode.model.*
import com.getcode.network.api.AccountApi
import com.getcode.solana.keys.PublicKey
import com.getcode.utils.startupLog
import com.google.firebase.messaging.Constants.ScionAnalytics.MessageType
import com.google.protobuf.GeneratedMessageLite
import dagger.hilt.android.qualifiers.ApplicationContext
import io.reactivex.rxjava3.core.Single
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import timber.log.Timber
import java.io.ByteArrayOutputStream
import javax.inject.Inject

private const val TAG = "AccountRepository"
Expand Down
80 changes: 71 additions & 9 deletions api/src/main/java/com/getcode/utils/Logging.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,84 @@
package com.getcode.utils

import android.annotation.SuppressLint
import com.bugsnag.android.BreadcrumbType
import com.bugsnag.android.Bugsnag
import timber.log.Timber

sealed interface TraceType {
/**
* An error was sent to Bugsnag (internal use only)
*/

fun startupLog(message: String, error: Throwable? = null) {
val tag = "app-startup"
data object Error : TraceType

Timber.tag(tag).let {
if (error != null) {
it.e(error, message)
} else {
it.d(message)
}
/**
* A log message
*/
data object Log : TraceType

/**
* A navigation event, such as a window opening or closing
*/
data object Navigation : TraceType

/**
* A background process such as a database query
*/
data object Process : TraceType

/**
* A network request
*/
data object Network : TraceType

/**
* A change in application state, such as launch or memory warning
*/
data object StateChange : TraceType

/**
* A user action, such as tapping a button
*/
data object User : TraceType
}

private fun TraceType.toBugsnagBreadcrumbType(): BreadcrumbType {
return when (this) {
TraceType.Error -> BreadcrumbType.ERROR
TraceType.Log -> BreadcrumbType.LOG
TraceType.Navigation -> BreadcrumbType.NAVIGATION
TraceType.Network -> BreadcrumbType.REQUEST
TraceType.Process -> BreadcrumbType.PROCESS
TraceType.StateChange -> BreadcrumbType.STATE
TraceType.User -> BreadcrumbType.USER
}
}

@SuppressLint("TimberExceptionLogging")
fun trace(
message: String,
tag: String? = null,
type: TraceType = TraceType.Log,
error: Throwable? = null
) {
val tree = if (tag == null) Timber else Timber.tag(tag)
val traceMessage = if (tag == null) message else "trace : $message"

tree.d(traceMessage)

if (Bugsnag.isStarted()) {
Bugsnag.leaveBreadcrumb("$tag | $message")
val breadcrumb = if (tag != null) {
"$tag | $traceMessage"
} else {
traceMessage
}

Bugsnag.leaveBreadcrumb(
breadcrumb,
emptyMap(),
type.toBugsnagBreadcrumbType()
)
}

error?.let(ErrorUtils::handleError)
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/getcode/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import com.bugsnag.android.Bugsnag
import com.getcode.manager.AuthManager
import com.getcode.network.integrity.DeviceCheck
import com.getcode.utils.ErrorUtils
import com.getcode.utils.startupLog
import com.getcode.utils.trace
import com.getcode.view.main.bill.CashBillAssets
import com.google.firebase.Firebase
import com.google.firebase.initialize
Expand Down Expand Up @@ -62,6 +62,6 @@ class App : Application() {
} else {
Bugsnag.start(this)
}
startupLog("app onCreate end")
trace("app onCreate end")
}
}
10 changes: 5 additions & 5 deletions app/src/main/java/com/getcode/manager/AuthManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import com.getcode.network.repository.isMock
import com.getcode.util.AccountUtils
import com.getcode.utils.ErrorUtils
import com.getcode.utils.installationId
import com.getcode.utils.startupLog
import com.getcode.utils.trace
import com.getcode.utils.token
import com.google.firebase.Firebase
import com.google.firebase.installations.installations
Expand Down Expand Up @@ -83,7 +83,7 @@ class AuthManager @Inject constructor(
isSoftLogin: Boolean = false,
rollbackOnError: Boolean = false
): Completable {
startupLog("Login: isSoftLogin: $isSoftLogin, rollbackOnError: $rollbackOnError")
trace("Login: isSoftLogin: $isSoftLogin, rollbackOnError: $rollbackOnError")

if (entropyB64.isEmpty()) {
sessionManager.clear()
Expand Down Expand Up @@ -194,7 +194,7 @@ class AuthManager @Inject constructor(
private fun fetchData(context: Context, entropyB64: String):
Single<Pair<PhoneRepository.GetAssociatedPhoneNumberResponse, IdentityRepository.GetUserResponse>> {

startupLog("fetching account data")
trace("fetching account data")

var owner = SessionManager.authState.value.keyPair
if (owner == null || SessionManager.authState.value.entropyB64 != entropyB64) {
Expand Down Expand Up @@ -226,7 +226,7 @@ class AuthManager @Inject constructor(
.toSingleDefault(Pair(phone!!, user!!))
}
.doOnSuccess {
startupLog("account data fetched successfully")
trace("account data fetched successfully")
launch { savePrefs(phone!!, user!!) }
launch { exchange.fetchRatesIfNeeded() }
launch { historyController.fetchChats() }
Expand All @@ -240,7 +240,7 @@ class AuthManager @Inject constructor(

private fun loginAnalytics(entropyB64: String) {
val owner = mnemonicManager.getKeyPair(entropyB64)
startupLog("analytics login event")
trace("analytics login event")
analytics.login(
ownerPublicKey = owner.getPublicKeyBase58(),
autoCompleteCount = 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.getcode.model.KinAmount
import com.getcode.navigation.core.LocalCodeNavigator
import com.getcode.ui.utils.RepeatOnLifecycle
import com.getcode.ui.utils.getActivityScopedViewModel
import com.getcode.utils.startupLog
import com.getcode.utils.trace
import com.getcode.view.main.account.AccountHome
import com.getcode.view.main.account.AccountSheetViewModel
import com.getcode.view.main.giveKin.GiveKinScreen
Expand Down Expand Up @@ -48,7 +48,7 @@ data class HomeScreen(
override fun Content() {
val vm = getViewModel<HomeViewModel>()

startupLog("home rendered")
trace("home rendered")
HomeScreen(vm, cashLink, requestPayload)

OnScreenResult<HomeResult> { result ->
Expand Down
26 changes: 12 additions & 14 deletions app/src/main/java/com/getcode/ui/components/AuthCheck.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.platform.LocalContext
import cafe.adriel.voyager.core.screen.Screen
import com.bugsnag.android.Bugsnag
import com.getcode.LocalDeeplinks
import com.getcode.R
import com.getcode.manager.BottomBarManager
Expand All @@ -23,7 +22,7 @@ import com.getcode.navigation.screens.LoginScreen
import com.getcode.util.DeeplinkHandler
import com.getcode.util.DeeplinkResult
import com.getcode.ui.utils.getActivity
import com.getcode.utils.startupLog
import com.getcode.utils.trace
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
Expand All @@ -36,7 +35,6 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import timber.log.Timber

private typealias DeeplinkFlowState = Pair<DeeplinkResult, SessionManager.SessionState>

Expand All @@ -57,23 +55,23 @@ fun AuthCheck(
}

LaunchedEffect(isAuthenticated) {
startupLog("isauth=$isAuthenticated")
trace("isauth=$isAuthenticated")
isAuthenticated?.let { authenticated ->
if (!deeplinkRouted) {
// Allow the seed input screen to complete and avoid
// premature navigation
if (currentRoute is AccessKeyLoginScreen) {
startupLog("No navigation within seed input")
trace("No navigation within seed input")
return@LaunchedEffect
}
if (currentRoute is LoginGraph) {
startupLog("No navigation within account creation and onboarding")
trace("No navigation within account creation and onboarding")
} else {
if (authenticated) {
startupLog("Navigating to home")
trace("Navigating to home")
onNavigate(listOf(HomeScreen()))
} else {
startupLog("Navigating to login")
trace("Navigating to login")
onNavigate(listOf(LoginScreen()))
}
}
Expand All @@ -93,9 +91,9 @@ fun AuthCheck(
.filter { (result, authState) ->
if (result == null) return@filter false
// wait for authentication
startupLog("checking auth state=${authState.isAuthenticated}")
trace("checking auth state=${authState.isAuthenticated}")
if( authState.isAuthenticated == null) {
startupLog("awaiting auth state confirmation")
trace("awaiting auth state confirmation")
return@filter false
}
return@filter true
Expand Down Expand Up @@ -123,7 +121,7 @@ fun AuthCheck(
.map { it.first }
.onEach { (_, screens) ->
deeplinkRouted = true
startupLog("navigating from deep link")
trace("navigating from deep link")
onNavigate(screens)
deeplinkHandler.debounceIntent = null
context.getActivity()?.intent = null
Expand All @@ -145,10 +143,10 @@ fun AuthCheck(

private fun Flow<DeeplinkFlowState>.mapSeedToHome(): Flow<DeeplinkFlowState> =
map { (data, auth) ->
startupLog("checking type")
trace("checking type")
val (type, screens) = data
if (type is DeeplinkHandler.Type.Login && auth.isAuthenticated == true) {
startupLog("mapping entropy to home screen")
trace("mapping entropy to home screen")
// send the user to home screen
val entropy = (screens.first() as? LoginScreen)?.seed
val updatedData = data.copy(stack = listOf(HomeScreen(seed = entropy)))
Expand All @@ -168,7 +166,7 @@ private fun Flow<DeeplinkResult>.showLogoutConfirmationIfNeeded(
if (type is DeeplinkHandler.Type.Login) {
val entropy = (screens.first() as? HomeScreen)?.seed
if (entropy != null) {
startupLog("showing logout confirm")
trace("showing logout confirm")
showLogoutMessage(
context = context,
entropyB64 = entropy,
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/getcode/ui/components/BottomBarView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import com.getcode.theme.BrandLight
import com.getcode.theme.CodeTheme
import com.getcode.theme.White
import com.getcode.ui.utils.rememberedClickable
import com.getcode.utils.startupLog
import com.getcode.utils.trace

@Composable
fun BottomBarView(
Expand All @@ -33,7 +33,7 @@ fun BottomBarView(
bottomBarMessage ?: return

LaunchedEffect(bottomBarMessage) {
startupLog("bottom bar message shown=${bottomBarMessage.title}")
trace("bottom bar message shown=${bottomBarMessage.title}")
}
BackHandler {
onBackPressed()
Expand Down
Loading
Loading