Skip to content

Commit

Permalink
MOB-487 Android: Log error into Crashlytics (#100)
Browse files Browse the repository at this point in the history
* WIP

* Timber

* Use Logging with consistency

* Cleanup

* Clean up

* Clean up

* Lint
  • Loading branch information
ruixhuang authored May 14, 2024
1 parent ff8b753 commit 8a489e4
Show file tree
Hide file tree
Showing 55 changed files with 263 additions and 123 deletions.
19 changes: 14 additions & 5 deletions v4/app/src/main/java/exchange/dydx/trading/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@ import exchange.dydx.trading.common.di.CoroutineScopes
import exchange.dydx.trading.common.theme.DydxTheme
import exchange.dydx.trading.common.theme.DydxThemeImpl
import exchange.dydx.trading.feature.shared.PreferenceKeys
import exchange.dydx.trading.integration.analytics.CompositeTracker
import exchange.dydx.trading.integration.analytics.CompositeTracking
import exchange.dydx.trading.integration.analytics.Tracking
import exchange.dydx.trading.integration.analytics.logging.CompositeLogger
import exchange.dydx.trading.integration.analytics.logging.CompositeLogging
import exchange.dydx.trading.integration.analytics.tracking.CompositeTracker
import exchange.dydx.trading.integration.analytics.tracking.CompositeTracking
import exchange.dydx.trading.integration.analytics.tracking.Tracking
import exchange.dydx.trading.integration.cosmos.CosmosV4ClientProtocol
import exchange.dydx.trading.integration.cosmos.CosmosV4ClientWebview
import exchange.dydx.trading.integration.cosmos.CosmosV4WebviewClientProtocol
import exchange.dydx.utilities.utils.JsonUtils
import exchange.dydx.utilities.utils.Logging
import exchange.dydx.utilities.utils.SharedPreferencesStore
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -75,13 +78,14 @@ interface AppModule {
fun provideThemeSettings(
@ApplicationContext appContext: Context,
preferenceStore: SharedPreferencesStore,
logger: Logging,
): ThemeSettings {
var theme = preferenceStore.read(PreferenceKeys.Theme)
if (theme.isNullOrEmpty()) {
theme = "dark"
}
val themeConfigValue =
ThemeConfig.createFromPreference(appContext, theme) ?: ThemeConfig.dark(appContext)
ThemeConfig.createFromPreference(appContext, theme, logger) ?: ThemeConfig.dark(appContext)
val themeConfig = MutableStateFlow<ThemeConfig?>(themeConfigValue)
val styleConfig =
MutableStateFlow<StyleConfig?>(JsonUtils.loadFromAssets(appContext, "dydxStyle.json"))
Expand All @@ -103,8 +107,9 @@ interface AppModule {
threading: ThreadingProtocol?,
timer: TimerProtocol?,
fileSystem: FileSystemProtocol?,
logger: CompositeLogging?
): IOImplementations =
IOImplementations(rest, webSocket, chain, tracking, threading, timer, fileSystem)
IOImplementations(rest, webSocket, chain, tracking, threading, timer, fileSystem, logger)

@EnvKey @Provides
fun provideEnvKey(): String = PreferenceKeys.Env
Expand Down Expand Up @@ -173,6 +178,10 @@ interface AppModule {

@Binds fun bindTracking(compositeTracking: CompositeTracking): Tracking

@Binds fun bindLogger(compositeLogger: CompositeLogger): Logging

@Binds fun bindCompositeLogging(compositeLogger: CompositeLogger): CompositeLogging

@Binds fun bindThreading(abacusThreadingImp: AbacusThreadingImp): ThreadingProtocol

@Binds fun bindTimer(abacusTimerImp: AbacusTimerImp): TimerProtocol
Expand Down
2 changes: 1 addition & 1 deletion v4/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ ext {
compileSdkVersion = 34

// App dependencies
abacusVersion = '1.7.9'
abacusVersion = '1.7.10'
carteraVersion = '0.1.13'
kollectionsVersion = '2.0.16'

Expand Down
14 changes: 8 additions & 6 deletions v4/core/src/main/java/exchange/dydx/trading/TradingActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import exchange.dydx.trading.core.biometric.DydxBiometricView
import exchange.dydx.trading.feature.shared.PreferenceKeys
import exchange.dydx.utilities.utils.SharedPreferencesStore
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject

private const val TAG = "TradingActivity"
Expand All @@ -57,10 +56,10 @@ class TradingActivity : FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Timber.tag(TAG).i("TradingActivity#onCreate")
viewModel.logger.d(TAG, "TradingActivity#onCreate")

CarteraSetup.run(this)
AnalyticsSetup.run(viewModel.compositeTracking, this)
CarteraSetup.run(this, viewModel.logger)
AnalyticsSetup.run(viewModel.compositeTracking, this, viewModel.logger)

WindowCompat.setDecorFitsSystemWindows(window, false)

Expand Down Expand Up @@ -96,6 +95,7 @@ class TradingActivity : FragmentActivity() {
modifier = Modifier,
isVisible = false,
javascriptRunner = it.runner,
logger = viewModel.logger,
)
}
content()
Expand All @@ -106,6 +106,7 @@ class TradingActivity : FragmentActivity() {
private fun BiometricPrompt() {
DydxBiometricPrompt.Content(
activity = this,
logger = viewModel.logger,
processSuccess = { result, error ->
setContentWithJS {
if (result) {
Expand Down Expand Up @@ -141,6 +142,7 @@ class TradingActivity : FragmentActivity() {
DydxNavGraph(
appRouter = viewModel.router,
modifier = it,
logger = viewModel.logger,
)
}
}
Expand All @@ -158,11 +160,11 @@ class TradingActivity : FragmentActivity() {

when (requestCode) {
DydxLogger.DATABASE_EXPORT_CODE -> {
viewModel.logger.shareDb(this, data)
viewModel.loggerDeprecated.shareDb(this, data)
}

else ->
Timber.tag(TAG).w("onActivityResult: unknown request code: %d", requestCode)
viewModel.logger.e(TAG, "onActivityResult: unknown request code: $requestCode")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import com.amplitude.android.DefaultTrackingOptions
import com.google.firebase.analytics.ktx.analytics
import com.google.firebase.ktx.Firebase
import exchange.dydx.trading.common.R
import exchange.dydx.trading.integration.analytics.AmplitudeTracker
import exchange.dydx.trading.integration.analytics.CompositeTracking
import exchange.dydx.trading.integration.analytics.FirebaseTracker
import timber.log.Timber
import exchange.dydx.trading.integration.analytics.tracking.AmplitudeTracker
import exchange.dydx.trading.integration.analytics.tracking.CompositeTracking
import exchange.dydx.trading.integration.analytics.tracking.FirebaseTracker
import exchange.dydx.utilities.utils.Logging

object AnalyticsSetup {

Expand All @@ -19,6 +19,7 @@ object AnalyticsSetup {
fun run(
compositeTracking: CompositeTracking,
activity: FragmentActivity,
logger: Logging,
) {
try {
val firebaseAnalytics = Firebase.analytics
Expand All @@ -33,7 +34,7 @@ object AnalyticsSetup {
)
compositeTracking.addTracker(AmplitudeTracker(amplitude))
} catch (e: Exception) {
Timber.tag(TAG).e(e, "Failed to set up Analytics")
logger.e(TAG, "Failed to set up Analytics")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ import exchange.dydx.cartera.CarteraConfig
import exchange.dydx.cartera.WalletConnectV2Config
import exchange.dydx.cartera.WalletProvidersConfig
import exchange.dydx.cartera.WalletSegueConfig
import timber.log.Timber
import exchange.dydx.utilities.utils.Logging

object CarteraSetup {

private const val TAG = "CarteraSetup"

fun run(activity: FragmentActivity) {
fun run(
activity: FragmentActivity,
logger: Logging,
) {
try {
setUpCartera(activity)
} catch (e: Exception) {
Timber.tag(TAG).e(e, "Failed to set up cartera")
logger.e(TAG, "Failed to set up cartera")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import exchange.dydx.trading.common.formatter.DydxFormatter
import exchange.dydx.trading.common.logger.DydxLogger
import exchange.dydx.trading.common.navigation.DydxRouter
import exchange.dydx.trading.feature.workers.DydxGlobalWorkers
import exchange.dydx.trading.integration.analytics.CompositeTracking
import exchange.dydx.trading.integration.analytics.Tracking
import exchange.dydx.trading.integration.analytics.logging.CompositeLogging
import exchange.dydx.trading.integration.analytics.tracking.CompositeTracking
import exchange.dydx.trading.integration.analytics.tracking.Tracking
import exchange.dydx.trading.integration.cosmos.CosmosV4WebviewClientProtocol
import exchange.dydx.utilities.utils.CachedFileLoader
import exchange.dydx.utilities.utils.SharedPreferencesStore
Expand All @@ -27,7 +28,7 @@ private const val TAG = "CoreViewModel"
@HiltViewModel
class CoreViewModel @Inject constructor(
val router: DydxRouter,
val logger: DydxLogger,
val loggerDeprecated: DydxLogger,
val cosmosClient: CosmosV4WebviewClientProtocol,
val platformInfo: PlatformInfo,
private val abacusStateManager: AbacusStateManagerProtocol,
Expand All @@ -37,6 +38,7 @@ class CoreViewModel @Inject constructor(
private val formatter: DydxFormatter,
private val parser: ParserProtocol,
private val tracker: Tracking,
val logger: CompositeLogging,
val compositeTracking: CompositeTracking,
private val preferencesStore: SharedPreferencesStore,
) : ViewModel() {
Expand All @@ -55,6 +57,7 @@ class CoreViewModel @Inject constructor(
formatter = formatter,
parser = parser,
tracker = tracker,
logger = logger,
preferencesStore = preferencesStore,
)
}
Expand Down
12 changes: 10 additions & 2 deletions v4/core/src/main/java/exchange/dydx/trading/core/DydxNavGraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import exchange.dydx.trading.feature.portfolio.portfolioGraph
import exchange.dydx.trading.feature.profile.profileGraph
import exchange.dydx.trading.feature.trade.tradeGraph
import exchange.dydx.trading.feature.transfer.transferGraph
import timber.log.Timber
import exchange.dydx.utilities.utils.Logging

private const val TAG = "DydxNavGraph"

Expand All @@ -35,6 +35,7 @@ private const val DEFAULT_START_DESTINATION = PortfolioRoutes.main
fun DydxNavGraph(
appRouter: DydxRouter,
modifier: Modifier = Modifier,
logger: Logging,
) {
val navController: NavHostController = rememberNavController()
appRouter.initialize(navController)
Expand All @@ -48,30 +49,37 @@ fun DydxNavGraph(
) {
loginGraph(
appRouter = appRouter,
logger = logger,
)

marketGraph(
appRouter = appRouter,
logger = logger,
)

tradeGraph(
appRouter = appRouter,
logger = logger,
)

profileGraph(
appRouter = appRouter,
logger = logger,
)

newsAlertsGraph(
appRouter = appRouter,
logger = logger,
)

portfolioGraph(
appRouter = appRouter,
logger = logger,
)

transferGraph(
appRouter = appRouter,
logger = logger,
)
}
}
Expand All @@ -84,7 +92,7 @@ private fun InitializeManagers(
// increment restartCount to cancel all downstream coroutines and
// manually reconnect
LaunchedEffect(coreViewModel.restartCount) {
Timber.tag(TAG).i("Intializing core services")
coreViewModel.logger.d(TAG, "Intializing core services")
coreViewModel.start()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import exchange.dydx.trading.common.AppConfig
import exchange.dydx.trading.common.navigation.DydxRouter
import exchange.dydx.trading.common.navigation.DydxRouter.Destination
import exchange.dydx.trading.common.navigation.MarketRoutes
import exchange.dydx.trading.integration.analytics.Tracking
import exchange.dydx.trading.integration.analytics.tracking.Tracking
import exchange.dydx.utilities.utils.Logging
import kotlinx.coroutines.flow.MutableStateFlow
import timber.log.Timber
import javax.inject.Inject

private const val TAG = "DydxRouterImpl"
Expand All @@ -34,6 +34,7 @@ class DydxRouterImpl @Inject constructor(
private val application: Application,
private val tracker: Tracking,
appConfig: AppConfig,
private val logger: Logging,
) : DydxRouter {

private lateinit var navHostController: NavHostController
Expand Down Expand Up @@ -79,7 +80,7 @@ class DydxRouterImpl @Inject constructor(
val dest = Destination(controller, destination, arguments)
val success = destinationFlow.tryEmit(dest)
if (!success) {
Timber.tag(TAG).w("Failed to emit: %s", dest.toString())
logger.e(TAG, "Failed to emit: $dest")
}

val destinationRoute = destination.route
Expand All @@ -106,12 +107,12 @@ class DydxRouterImpl @Inject constructor(
pendingPresentation = null
}
} else {
Timber.tag(TAG).w("Destination route is null")
logger.e(TAG, "Destination route is null")
}
}

override fun initialize(navHostController: NavHostController) {
Timber.tag(TAG).d("DydxRouter initialized")
logger.d(TAG, "DydxRouter initialized")
this.navHostController = navHostController
navHostController.addOnDestinationChangedListener(destinationChangedListener)
}
Expand All @@ -137,7 +138,7 @@ class DydxRouterImpl @Inject constructor(
restoreState = false
}
} catch (e: Exception) {
Timber.tag(TAG).e(e, "Failed to navigate to route: %s", routePath)
logger.e(TAG, "Failed to navigate to route: $routePath")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import exchange.dydx.abacus.protocols.LocalizerProtocol
import exchange.dydx.dydxstatemanager.localizeWithParams
import exchange.dydx.trading.common.compose.collectAsStateWithLifecycle
import exchange.dydx.trading.common.theme.MockLocalizer
import timber.log.Timber
import exchange.dydx.utilities.utils.Logging

object DydxBiometricPrompt {

Expand All @@ -28,6 +28,7 @@ object DydxBiometricPrompt {
@Composable
fun Content(
activity: FragmentActivity,
logger: Logging,
processSuccess: (Boolean, String?) -> Unit,
) {
val viewModel: DydxBiometricPromptModel = hiltViewModel()
Expand Down Expand Up @@ -58,7 +59,7 @@ object DydxBiometricPrompt {

override fun onAuthenticationError(errCode: Int, errString: CharSequence) {
super.onAuthenticationError(errCode, errString)
Timber.tag(TAG).d("errCode is $errCode and errString is: $errString")
logger.d(TAG, "errCode is $errCode and errString is: $errString")
val error = localizer.localizeWithParams(
path = "ERRORS.GENERAL.SOMETHING_WENT_WRONG_WITH_MESSAGE",
params = mapOf("ERROR_MESSAGE" to errString.toString()),
Expand All @@ -68,13 +69,13 @@ object DydxBiometricPrompt {

override fun onAuthenticationFailed() {
super.onAuthenticationFailed()
Timber.tag(TAG).d("User biometric rejected.")
logger.d(TAG, "User biometric rejected.")
processSuccess(false, localizer.localize("APP.GENERAL.AUTHENTICATE_TO_PROCEED"))
}

override fun onAuthenticationSucceeded(result: androidx.biometric.BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
Timber.d(TAG, "Authentication was successful")
logger.d(TAG, "Authentication was successful")
processSuccess(true, null)
}
}
Expand Down
Loading

0 comments on commit 8a489e4

Please sign in to comment.