Skip to content

Commit 920c415

Browse files
authored
Merge pull request #4860 from element-hq/feature/bma/extractLogs
Small cleanup around log tag.
2 parents 770e16d + 4619f06 commit 920c415

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustClientSessionDelegate.kt

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
package io.element.android.libraries.matrix.impl
99

1010
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
11+
import io.element.android.libraries.core.log.logger.LoggerTag
1112
import io.element.android.libraries.matrix.impl.mapper.toSessionData
1213
import io.element.android.libraries.matrix.impl.paths.getSessionPaths
1314
import io.element.android.libraries.matrix.impl.util.anonymizedTokens
1415
import io.element.android.libraries.sessionstorage.api.SessionStore
1516
import kotlinx.coroutines.CoroutineScope
16-
import kotlinx.coroutines.ExperimentalCoroutinesApi
1717
import kotlinx.coroutines.launch
1818
import org.matrix.rustcomponents.sdk.ClientDelegate
1919
import org.matrix.rustcomponents.sdk.ClientSessionDelegate
@@ -22,21 +22,20 @@ import timber.log.Timber
2222
import java.lang.ref.WeakReference
2323
import java.util.concurrent.atomic.AtomicBoolean
2424

25+
private val loggerTag = LoggerTag("RustClientSessionDelegate")
26+
2527
/**
2628
* This class is responsible for handling the session data for the Rust SDK.
2729
*
2830
* It implements both [ClientSessionDelegate] and [ClientDelegate] to react to session data updates and auth errors.
2931
*
3032
* IMPORTANT: you must set the [client] property as soon as possible so [didReceiveAuthError] can work properly.
3133
*/
32-
@OptIn(ExperimentalCoroutinesApi::class)
3334
class RustClientSessionDelegate(
3435
private val sessionStore: SessionStore,
3536
private val appCoroutineScope: CoroutineScope,
3637
coroutineDispatchers: CoroutineDispatchers,
3738
) : ClientSessionDelegate, ClientDelegate {
38-
private val clientLog = Timber.tag("$this")
39-
4039
// Used to ensure several calls to `didReceiveAuthError` don't trigger multiple logouts
4140
private val isLoggingOut = AtomicBoolean(false)
4241

@@ -64,7 +63,7 @@ class RustClientSessionDelegate(
6463
appCoroutineScope.launch(updateTokensDispatcher) {
6564
val existingData = sessionStore.getSession(session.userId) ?: return@launch
6665
val (anonymizedAccessToken, anonymizedRefreshToken) = session.anonymizedTokens()
67-
clientLog.d(
66+
Timber.tag(loggerTag.value).d(
6867
"Saving new session data with token: access token '$anonymizedAccessToken' and refresh token '$anonymizedRefreshToken'. " +
6968
"Was token valid: ${existingData.isTokenValid}"
7069
)
@@ -75,48 +74,48 @@ class RustClientSessionDelegate(
7574
sessionPaths = existingData.getSessionPaths(),
7675
)
7776
sessionStore.updateData(newData)
78-
clientLog.d("Saved new session data with access token: '$anonymizedAccessToken'.")
77+
Timber.tag(loggerTag.value).d("Saved new session data with access token: '$anonymizedAccessToken'.")
7978
}.invokeOnCompletion {
8079
if (it != null) {
81-
clientLog.e(it, "Failed to save new session data.")
80+
Timber.tag(loggerTag.value).e(it, "Failed to save new session data.")
8281
}
8382
}
8483
}
8584

8685
override fun didReceiveAuthError(isSoftLogout: Boolean) {
87-
clientLog.w("didReceiveAuthError(isSoftLogout=$isSoftLogout)")
86+
Timber.tag(loggerTag.value).w("didReceiveAuthError(isSoftLogout=$isSoftLogout)")
8887
if (isLoggingOut.getAndSet(true).not()) {
89-
clientLog.v("didReceiveAuthError -> do the cleanup")
88+
Timber.tag(loggerTag.value).v("didReceiveAuthError -> do the cleanup")
9089
// TODO handle isSoftLogout parameter.
9190
appCoroutineScope.launch(updateTokensDispatcher) {
9291
val currentClient = client.get()
9392
if (currentClient == null) {
94-
clientLog.w("didReceiveAuthError -> no client, exiting")
93+
Timber.tag(loggerTag.value).w("didReceiveAuthError -> no client, exiting")
9594
isLoggingOut.set(false)
9695
return@launch
9796
}
9897
val existingData = sessionStore.getSession(currentClient.sessionId.value)
9998
val (anonymizedAccessToken, anonymizedRefreshToken) = existingData.anonymizedTokens()
100-
clientLog.d(
99+
Timber.tag(loggerTag.value).d(
101100
"Removing session data with access token '$anonymizedAccessToken' " +
102101
"and refresh token '$anonymizedRefreshToken'."
103102
)
104103
if (existingData != null) {
105104
// Set isTokenValid to false
106105
val newData = existingData.copy(isTokenValid = false)
107106
sessionStore.updateData(newData)
108-
clientLog.d("Invalidated session data with access token: '$anonymizedAccessToken'.")
107+
Timber.tag(loggerTag.value).d("Invalidated session data with access token: '$anonymizedAccessToken'.")
109108
} else {
110-
clientLog.d("No session data found.")
109+
Timber.tag(loggerTag.value).d("No session data found.")
111110
}
112111
currentClient.logout(userInitiated = false, ignoreSdkError = true)
113112
}.invokeOnCompletion {
114113
if (it != null) {
115-
clientLog.e(it, "Failed to remove session data.")
114+
Timber.tag(loggerTag.value).e(it, "Failed to remove session data.")
116115
}
117116
}
118117
} else {
119-
clientLog.v("didReceiveAuthError -> already cleaning up")
118+
Timber.tag(loggerTag.value).v("didReceiveAuthError -> already cleaning up")
120119
}
121120
}
122121

libraries/pushproviders/unifiedpush/src/main/kotlin/io/element/android/libraries/pushproviders/unifiedpush/UnifiedPushGatewayResolver.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package io.element.android.libraries.pushproviders.unifiedpush
1010
import com.squareup.anvil.annotations.ContributesBinding
1111
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
1212
import io.element.android.libraries.core.data.tryOrNull
13+
import io.element.android.libraries.core.log.logger.LoggerTag
1314
import io.element.android.libraries.di.AppScope
1415
import kotlinx.coroutines.withContext
1516
import retrofit2.HttpException
@@ -29,43 +30,45 @@ interface UnifiedPushGatewayResolver {
2930
suspend fun getGateway(endpoint: String): UnifiedPushGatewayResolverResult
3031
}
3132

33+
private val loggerTag = LoggerTag("DefaultUnifiedPushGatewayResolver")
34+
3235
@ContributesBinding(AppScope::class)
3336
class DefaultUnifiedPushGatewayResolver @Inject constructor(
3437
private val unifiedPushApiFactory: UnifiedPushApiFactory,
3538
private val coroutineDispatchers: CoroutineDispatchers,
3639
) : UnifiedPushGatewayResolver {
3740
override suspend fun getGateway(endpoint: String): UnifiedPushGatewayResolverResult {
3841
val url = tryOrNull(
39-
onException = { Timber.tag("DefaultUnifiedPushGatewayResolver").d(it, "Cannot parse endpoint as an URL") }
42+
onException = { Timber.tag(loggerTag.value).d(it, "Cannot parse endpoint as an URL") }
4043
) {
4144
URL(endpoint)
4245
}
4346
return if (url == null) {
44-
Timber.tag("DefaultUnifiedPushGatewayResolver").d("ErrorInvalidUrl")
47+
Timber.tag(loggerTag.value).d("ErrorInvalidUrl")
4548
UnifiedPushGatewayResolverResult.ErrorInvalidUrl
4649
} else {
4750
val port = if (url.port != -1) ":${url.port}" else ""
4851
val customBase = "${url.protocol}://${url.host}$port"
4952
val customUrl = "$customBase/_matrix/push/v1/notify"
50-
Timber.tag("DefaultUnifiedPushGatewayResolver").i("Testing $customUrl")
53+
Timber.tag(loggerTag.value).i("Testing $customUrl")
5154
return withContext(coroutineDispatchers.io) {
5255
val api = unifiedPushApiFactory.create(customBase)
5356
try {
5457
val discoveryResponse = api.discover()
5558
if (discoveryResponse.unifiedpush.gateway == "matrix") {
56-
Timber.tag("DefaultUnifiedPushGatewayResolver").d("The endpoint seems to be a valid UnifiedPush gateway")
59+
Timber.tag(loggerTag.value).d("The endpoint seems to be a valid UnifiedPush gateway")
5760
UnifiedPushGatewayResolverResult.Success(customUrl)
5861
} else {
5962
// The endpoint returned a 200 OK but didn't promote an actual matrix gateway, which means it doesn't have any
60-
Timber.tag("DefaultUnifiedPushGatewayResolver").w("The endpoint does not seem to be a valid UnifiedPush gateway, using fallback")
63+
Timber.tag(loggerTag.value).w("The endpoint does not seem to be a valid UnifiedPush gateway, using fallback")
6164
UnifiedPushGatewayResolverResult.NoMatrixGateway
6265
}
6366
} catch (throwable: Throwable) {
6467
if ((throwable as? HttpException)?.code() == HttpURLConnection.HTTP_NOT_FOUND) {
65-
Timber.tag("DefaultUnifiedPushGatewayResolver").i("Checking for UnifiedPush endpoint yielded 404, using fallback")
68+
Timber.tag(loggerTag.value).i("Checking for UnifiedPush endpoint yielded 404, using fallback")
6669
UnifiedPushGatewayResolverResult.NoMatrixGateway
6770
} else {
68-
Timber.tag("DefaultUnifiedPushGatewayResolver").e(throwable, "Error checking for UnifiedPush endpoint")
71+
Timber.tag(loggerTag.value).e(throwable, "Error checking for UnifiedPush endpoint")
6972
UnifiedPushGatewayResolverResult.Error(customUrl)
7073
}
7174
}

0 commit comments

Comments
 (0)