From c3b8ed223d22f0865104dec7adfae72048c097b9 Mon Sep 17 00:00:00 2001 From: Valere Date: Mon, 2 Nov 2020 16:39:06 +0100 Subject: [PATCH 1/6] Increase log level of file logger --- .../java/im/vector/app/features/rageshake/VectorFileLogger.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/rageshake/VectorFileLogger.kt b/vector/src/main/java/im/vector/app/features/rageshake/VectorFileLogger.kt index f26ffdd700..a279c16cb1 100644 --- a/vector/src/main/java/im/vector/app/features/rageshake/VectorFileLogger.kt +++ b/vector/src/main/java/im/vector/app/features/rageshake/VectorFileLogger.kt @@ -102,8 +102,8 @@ class VectorFileLogger @Inject constructor(val context: Context, private val vec return if (vectorPreferences.labAllowedExtendedLogging()) { false } else { - // Exclude debug and verbose logs - priority <= Log.DEBUG + // Exclude verbose logs + priority < Log.DEBUG } } From 3c7f61e45c43389e9068fe631509cd51d7ff3b51 Mon Sep 17 00:00:00 2001 From: Valere Date: Mon, 2 Nov 2020 16:41:30 +0100 Subject: [PATCH 2/6] Clean oldest gossiping entries on open Add a dump of DB size on bug report --- .../matrix/android/sdk/api/session/Session.kt | 5 ++ .../sdk/api/session/crypto/CryptoService.kt | 2 + .../internal/crypto/DefaultCryptoService.kt | 9 +++ .../internal/crypto/store/IMXCryptoStore.kt | 2 + .../crypto/store/db/RealmCryptoStore.kt | 58 +++++++++++++++++++ .../sdk/internal/session/DefaultSession.kt | 22 ++++++- .../app/features/rageshake/BugReporter.kt | 2 + 7 files changed, 99 insertions(+), 1 deletion(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt index f8dc906502..4cf46cbbc0 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt @@ -238,4 +238,9 @@ interface Session : } val sharedSecretStorageService: SharedSecretStorageService + + /** + * Maintenance API, allows to print outs info on DB size to logcat + */ + fun dbgTraceDbInfo() } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/crypto/CryptoService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/crypto/CryptoService.kt index 34be1b8d05..0eefca1b4c 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/crypto/CryptoService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/crypto/CryptoService.kt @@ -155,4 +155,6 @@ interface CryptoService { // For testing shared session fun getSharedWithInfo(roomId: String?, sessionId: String): MXUsersDevicesMap fun getWithHeldMegolmSession(roomId: String, sessionId: String): RoomKeyWithHeldContent? + + fun logDbUsageInfo() } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt index 326eac8f91..d3a3fd9fbd 100755 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/DefaultCryptoService.kt @@ -314,6 +314,10 @@ internal class DefaultCryptoService @Inject constructor( } // Just update fetchDevicesList(NoOpMatrixCallback()) + + cryptoCoroutineScope.launch(coroutineDispatchers.crypto) { + cryptoStore.tidyUpDataBase() + } } fun ensureDevice() { @@ -1291,6 +1295,11 @@ internal class DefaultCryptoService @Inject constructor( override fun getWithHeldMegolmSession(roomId: String, sessionId: String): RoomKeyWithHeldContent? { return cryptoStore.getWithHeldMegolmSession(roomId, sessionId) } + + override fun logDbUsageInfo() { + cryptoStore.logDbUsageInfo() + } + /* ========================================================================================== * For test only * ========================================================================================== */ diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/IMXCryptoStore.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/IMXCryptoStore.kt index 72d541d4df..9a9f645b49 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/IMXCryptoStore.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/IMXCryptoStore.kt @@ -456,4 +456,6 @@ internal interface IMXCryptoStore { fun setDeviceKeysUploaded(uploaded: Boolean) fun getDeviceKeysUploaded(): Boolean + fun tidyUpDataBase() + fun logDbUsageInfo() } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt index 5d19e6d607..ec1a5d73a1 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt @@ -95,6 +95,7 @@ import org.matrix.android.sdk.internal.session.SessionScope import org.matrix.olm.OlmAccount import org.matrix.olm.OlmException import timber.log.Timber +import java.lang.StringBuilder import javax.inject.Inject import kotlin.collections.set @@ -1666,4 +1667,61 @@ internal class RealmCryptoStore @Inject constructor( result } } + + /** + * Some entries in the DB can get a bit out of control with time + * So we need to tidy up a bit + */ + override fun tidyUpDataBase() { + val prevWeekTs = System.currentTimeMillis() - 7 * 24 * 60 * 60 * 1_000 + doRealmTransaction(realmConfiguration) { realm -> + + // Only keep one week history + realm.where() + .lessThan(IncomingGossipingRequestEntityFields.LOCAL_CREATION_TIMESTAMP, prevWeekTs) + .findAll().let { + Timber.i("## Crypto Clean up ${it.size} IncomingGossipingRequestEntity") + it.deleteAllFromRealm() + } + + // Clean the cancelled ones? + realm.where() + .equalTo(OutgoingGossipingRequestEntityFields.REQUEST_STATE_STR, OutgoingGossipingRequestState.CANCELLED.name) + .equalTo(OutgoingGossipingRequestEntityFields.TYPE_STR, GossipRequestType.KEY.name) + .findAll().let { + Timber.i("## Crypto Clean up ${it.size} OutgoingGossipingRequestEntity") + it.deleteAllFromRealm() + } + + // Only keep one week history + realm.where() + .lessThan(GossipingEventEntityFields.AGE_LOCAL_TS, prevWeekTs) + .findAll().let { + Timber.i("## Crypto Clean up ${it.size} GossipingEventEntityFields") + it.deleteAllFromRealm() + } + + // Can we do something for WithHeldSessionEntity? + } + } + + /** + * Prints out database info + */ + override fun logDbUsageInfo() { + Realm.getInstance(realmConfiguration).use { realm -> + val info = StringBuilder() + // Check if we have data + info.append("\n==============================================") + info.append("\n==============================================") + info.append("\nCrypto Realm is empty: ${realm.isEmpty}") + realmConfiguration.realmObjectClasses.forEach { modelClazz -> + val count = realm.where(modelClazz).count() + info.append("\nCrypto Realm - count ${modelClazz.simpleName}: $count") + } + info.append("\n==============================================") + info.append("\n==============================================") + Timber.i(info.toString()) + } + } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt index 7e182525a9..3b1fc73bc9 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt @@ -18,7 +18,9 @@ package org.matrix.android.sdk.internal.session import androidx.annotation.MainThread import dagger.Lazy +import io.realm.Realm import io.realm.RealmConfiguration +import io.realm.kotlin.where import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import okhttp3.OkHttpClient @@ -197,7 +199,7 @@ internal class DefaultSession @Inject constructor( override fun close() { assert(isOpen) stopSync() - // timelineEventDecryptor.destroy() + // timelineEventDecryptor.destroy() uiHandler.post { lifecycleObservers.forEach { it.onStop() } } @@ -284,4 +286,22 @@ internal class DefaultSession @Inject constructor( override fun toString(): String { return "$myUserId - ${sessionParams.deviceId}" } + + override fun dbgTraceDbInfo() { + Realm.getInstance(realmConfiguration).use { realm -> + val info = StringBuilder() + + // Check if we have data + info.append("\n==============================================") + info.append("\n==============================================") + info.append("\nSession Realm is empty: ${realm.isEmpty}") + realmConfiguration.realmObjectClasses.forEach { modelClazz -> + val count = realm.where(modelClazz).count() + info.append("\nSession Realm - count ${modelClazz.simpleName}: $count") + } + info.append("\n==============================================") + info.append("\n==============================================") + Timber.i(info.toString()) + } + } } diff --git a/vector/src/main/java/im/vector/app/features/rageshake/BugReporter.kt b/vector/src/main/java/im/vector/app/features/rageshake/BugReporter.kt index 95fe16ea51..4514c9ef80 100755 --- a/vector/src/main/java/im/vector/app/features/rageshake/BugReporter.kt +++ b/vector/src/main/java/im/vector/app/features/rageshake/BugReporter.kt @@ -446,6 +446,8 @@ class BugReporter @Inject constructor( */ fun openBugReportScreen(activity: FragmentActivity, forSuggestion: Boolean = false) { screenshot = takeScreenshot(activity) + activeSessionHolder.getSafeActiveSession()?.dbgTraceDbInfo() + activeSessionHolder.getSafeActiveSession()?.cryptoService()?.logDbUsageInfo() val intent = Intent(activity, BugReportActivity::class.java) intent.putExtra("FOR_SUGGESTION", forSuggestion) From d4963dfb31212b1e9354b0f6fbab7f098d6d3278 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 3 Nov 2020 09:17:07 +0100 Subject: [PATCH 3/6] Use the same name for the 2 db log methods --- .../matrix/android/sdk/api/session/Session.kt | 2 +- .../sdk/internal/session/DefaultSession.kt | 2 +- .../app/features/rageshake/BugReporter.kt | 32 ++++++++++--------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt index 4cf46cbbc0..56609610f1 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/Session.kt @@ -242,5 +242,5 @@ interface Session : /** * Maintenance API, allows to print outs info on DB size to logcat */ - fun dbgTraceDbInfo() + fun logDbUsageInfo() } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt index 3b1fc73bc9..2a6df81e72 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt @@ -287,7 +287,7 @@ internal class DefaultSession @Inject constructor( return "$myUserId - ${sessionParams.deviceId}" } - override fun dbgTraceDbInfo() { + override fun logDbUsageInfo() { Realm.getInstance(realmConfiguration).use { realm -> val info = StringBuilder() diff --git a/vector/src/main/java/im/vector/app/features/rageshake/BugReporter.kt b/vector/src/main/java/im/vector/app/features/rageshake/BugReporter.kt index 4514c9ef80..96248187aa 100755 --- a/vector/src/main/java/im/vector/app/features/rageshake/BugReporter.kt +++ b/vector/src/main/java/im/vector/app/features/rageshake/BugReporter.kt @@ -212,20 +212,20 @@ class BugReporter @Inject constructor( } activeSessionHolder.getSafeActiveSession() - ?.takeIf { !mIsCancelled && withKeyRequestHistory } - ?.cryptoService() - ?.getGossipingEvents() - ?.let { GossipingEventsSerializer().serialize(it) } - ?.toByteArray() - ?.let { rawByteArray -> - File(context.cacheDir.absolutePath, KEY_REQUESTS_FILENAME) - .also { - it.outputStream() + ?.takeIf { !mIsCancelled && withKeyRequestHistory } + ?.cryptoService() + ?.getGossipingEvents() + ?.let { GossipingEventsSerializer().serialize(it) } + ?.toByteArray() + ?.let { rawByteArray -> + File(context.cacheDir.absolutePath, KEY_REQUESTS_FILENAME) + .also { + it.outputStream() .use { os -> os.write(rawByteArray) } - } - } - ?.let { compressFile(it) } - ?.let { gzippedFiles.add(it) } + } + } + ?.let { compressFile(it) } + ?.let { gzippedFiles.add(it) } var deviceId = "undefined" var userId = "undefined" @@ -446,8 +446,10 @@ class BugReporter @Inject constructor( */ fun openBugReportScreen(activity: FragmentActivity, forSuggestion: Boolean = false) { screenshot = takeScreenshot(activity) - activeSessionHolder.getSafeActiveSession()?.dbgTraceDbInfo() - activeSessionHolder.getSafeActiveSession()?.cryptoService()?.logDbUsageInfo() + activeSessionHolder.getSafeActiveSession()?.let { + it.logDbUsageInfo() + it.cryptoService().logDbUsageInfo() + } val intent = Intent(activity, BugReportActivity::class.java) intent.putExtra("FOR_SUGGESTION", forSuggestion) From 5eb2f143750233eb6377e3f37358ee3d34b13fa1 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 3 Nov 2020 09:20:24 +0100 Subject: [PATCH 4/6] Move RealmDebugTools and make it an internal class --- .../database/tools}/RealmDebugTools.kt | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) rename matrix-sdk-android/src/{debug/java/org/matrix/android/sdk/internal/database => main/java/org/matrix/android/sdk/internal/database/tools}/RealmDebugTools.kt (88%) diff --git a/matrix-sdk-android/src/debug/java/org/matrix/android/sdk/internal/database/RealmDebugTools.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/tools/RealmDebugTools.kt similarity index 88% rename from matrix-sdk-android/src/debug/java/org/matrix/android/sdk/internal/database/RealmDebugTools.kt rename to matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/tools/RealmDebugTools.kt index e5f4af2377..4e37ab14a8 100644 --- a/matrix-sdk-android/src/debug/java/org/matrix/android/sdk/internal/database/RealmDebugTools.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/tools/RealmDebugTools.kt @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.matrix.android.sdk.internal.database +package org.matrix.android.sdk.internal.database.tools import org.matrix.android.sdk.internal.crypto.store.db.model.CrossSigningInfoEntity import org.matrix.android.sdk.internal.crypto.store.db.model.CryptoMetadataEntity @@ -35,17 +35,19 @@ import io.realm.RealmConfiguration import io.realm.kotlin.where import timber.log.Timber -object RealmDebugTools { +internal class RealmDebugTools( + private val realmConfiguration: RealmConfiguration +) { /** - * Log info about the crypto DB + * Log info about the DB */ - fun dumpCryptoDb(realmConfiguration: RealmConfiguration) { + fun logInfo() { + Timber.d("Realm located at : ${realmConfiguration.realmDirectory}/${realmConfiguration.realmFileName}") + + val key = realmConfiguration.encryptionKey.joinToString("") { byte -> "%02x".format(byte) } + Timber.d("Realm encryption key : $key") + Realm.getInstance(realmConfiguration).use { - Timber.d("Realm located at : ${realmConfiguration.realmDirectory}/${realmConfiguration.realmFileName}") - - val key = realmConfiguration.encryptionKey.joinToString("") { byte -> "%02x".format(byte) } - Timber.d("Realm encryption key : $key") - // Check if we have data Timber.e("Realm is empty: ${it.isEmpty}") From a216f82b3584dfc390e137d96143204502d0a500 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 3 Nov 2020 09:30:26 +0100 Subject: [PATCH 5/6] Avoid code duplication and log total count --- .../crypto/store/db/RealmCryptoStore.kt | 17 +---- .../database/tools/RealmDebugTools.kt | 64 ++++++++----------- .../sdk/internal/session/DefaultSession.kt | 19 +----- 3 files changed, 31 insertions(+), 69 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt index ec1a5d73a1..72274aa70a 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStore.kt @@ -87,6 +87,7 @@ import org.matrix.android.sdk.internal.crypto.store.db.query.get import org.matrix.android.sdk.internal.crypto.store.db.query.getById import org.matrix.android.sdk.internal.crypto.store.db.query.getOrCreate import org.matrix.android.sdk.internal.database.mapper.ContentMapper +import org.matrix.android.sdk.internal.database.tools.RealmDebugTools import org.matrix.android.sdk.internal.di.CryptoDatabase import org.matrix.android.sdk.internal.di.DeviceId import org.matrix.android.sdk.internal.di.MoshiProvider @@ -95,7 +96,6 @@ import org.matrix.android.sdk.internal.session.SessionScope import org.matrix.olm.OlmAccount import org.matrix.olm.OlmException import timber.log.Timber -import java.lang.StringBuilder import javax.inject.Inject import kotlin.collections.set @@ -1709,19 +1709,6 @@ internal class RealmCryptoStore @Inject constructor( * Prints out database info */ override fun logDbUsageInfo() { - Realm.getInstance(realmConfiguration).use { realm -> - val info = StringBuilder() - // Check if we have data - info.append("\n==============================================") - info.append("\n==============================================") - info.append("\nCrypto Realm is empty: ${realm.isEmpty}") - realmConfiguration.realmObjectClasses.forEach { modelClazz -> - val count = realm.where(modelClazz).count() - info.append("\nCrypto Realm - count ${modelClazz.simpleName}: $count") - } - info.append("\n==============================================") - info.append("\n==============================================") - Timber.i(info.toString()) - } + RealmDebugTools(realmConfiguration).logInfo("Crypto") } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/tools/RealmDebugTools.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/tools/RealmDebugTools.kt index 4e37ab14a8..ac60f88146 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/tools/RealmDebugTools.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/tools/RealmDebugTools.kt @@ -16,23 +16,9 @@ package org.matrix.android.sdk.internal.database.tools -import org.matrix.android.sdk.internal.crypto.store.db.model.CrossSigningInfoEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.CryptoMetadataEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.CryptoRoomEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.DeviceInfoEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.GossipingEventEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.IncomingGossipingRequestEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.KeyInfoEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.KeysBackupDataEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.MyDeviceLastSeenInfoEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.OlmInboundGroupSessionEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.OlmSessionEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.OutgoingGossipingRequestEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.TrustLevelEntity -import org.matrix.android.sdk.internal.crypto.store.db.model.UserEntity import io.realm.Realm import io.realm.RealmConfiguration -import io.realm.kotlin.where +import org.matrix.android.sdk.BuildConfig import timber.log.Timber internal class RealmDebugTools( @@ -41,30 +27,34 @@ internal class RealmDebugTools( /** * Log info about the DB */ - fun logInfo() { - Timber.d("Realm located at : ${realmConfiguration.realmDirectory}/${realmConfiguration.realmFileName}") + fun logInfo(baseName: String) { + buildString { + append("\n$baseName Realm located at : ${realmConfiguration.realmDirectory}/${realmConfiguration.realmFileName}") - val key = realmConfiguration.encryptionKey.joinToString("") { byte -> "%02x".format(byte) } - Timber.d("Realm encryption key : $key") + if (BuildConfig.LOG_PRIVATE_DATA) { + val key = realmConfiguration.encryptionKey.joinToString("") { byte -> "%02x".format(byte) } + append("\n$baseName Realm encryption key : $key") + } - Realm.getInstance(realmConfiguration).use { - // Check if we have data - Timber.e("Realm is empty: ${it.isEmpty}") - - Timber.d("Realm has CryptoMetadataEntity: ${it.where().count()}") - Timber.d("Realm has CryptoRoomEntity: ${it.where().count()}") - Timber.d("Realm has DeviceInfoEntity: ${it.where().count()}") - Timber.d("Realm has KeysBackupDataEntity: ${it.where().count()}") - Timber.d("Realm has OlmInboundGroupSessionEntity: ${it.where().count()}") - Timber.d("Realm has OlmSessionEntity: ${it.where().count()}") - Timber.d("Realm has UserEntity: ${it.where().count()}") - Timber.d("Realm has KeyInfoEntity: ${it.where().count()}") - Timber.d("Realm has CrossSigningInfoEntity: ${it.where().count()}") - Timber.d("Realm has TrustLevelEntity: ${it.where().count()}") - Timber.d("Realm has GossipingEventEntity: ${it.where().count()}") - Timber.d("Realm has IncomingGossipingRequestEntity: ${it.where().count()}") - Timber.d("Realm has OutgoingGossipingRequestEntity: ${it.where().count()}") - Timber.d("Realm has MyDeviceLastSeenInfoEntity: ${it.where().count()}") + Realm.getInstance(realmConfiguration).use { realm -> + // Check if we have data + separator() + separator() + append("\n$baseName Realm is empty: ${realm.isEmpty}") + var total = 0L + realmConfiguration.realmObjectClasses.forEach { modelClazz -> + val count = realm.where(modelClazz).count() + total += count + append("\n$baseName Realm - count ${modelClazz.simpleName}: $count") + } + separator() + append("\n$baseName Realm - total count: $total") + separator() + separator() + } } + .let { Timber.i(it) } } + + internal fun StringBuilder.separator() = append("\n==============================================") } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt index 2a6df81e72..25345e953c 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/DefaultSession.kt @@ -18,9 +18,7 @@ package org.matrix.android.sdk.internal.session import androidx.annotation.MainThread import dagger.Lazy -import io.realm.Realm import io.realm.RealmConfiguration -import io.realm.kotlin.where import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import okhttp3.OkHttpClient @@ -61,6 +59,7 @@ import org.matrix.android.sdk.api.session.user.UserService import org.matrix.android.sdk.api.session.widgets.WidgetService import org.matrix.android.sdk.internal.auth.SessionParamsStore import org.matrix.android.sdk.internal.crypto.DefaultCryptoService +import org.matrix.android.sdk.internal.database.tools.RealmDebugTools import org.matrix.android.sdk.internal.di.SessionDatabase import org.matrix.android.sdk.internal.di.SessionId import org.matrix.android.sdk.internal.di.UnauthenticatedWithCertificate @@ -288,20 +287,6 @@ internal class DefaultSession @Inject constructor( } override fun logDbUsageInfo() { - Realm.getInstance(realmConfiguration).use { realm -> - val info = StringBuilder() - - // Check if we have data - info.append("\n==============================================") - info.append("\n==============================================") - info.append("\nSession Realm is empty: ${realm.isEmpty}") - realmConfiguration.realmObjectClasses.forEach { modelClazz -> - val count = realm.where(modelClazz).count() - info.append("\nSession Realm - count ${modelClazz.simpleName}: $count") - } - info.append("\n==============================================") - info.append("\n==============================================") - Timber.i(info.toString()) - } + RealmDebugTools(realmConfiguration).logInfo("Session") } } From 997cd6834429022447601dbe17eae0b529f96664 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 3 Nov 2020 09:57:34 +0100 Subject: [PATCH 6/6] Add some padding for easier readability --- .../android/sdk/internal/database/tools/RealmDebugTools.kt | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/tools/RealmDebugTools.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/tools/RealmDebugTools.kt index ac60f88146..103e84dea6 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/tools/RealmDebugTools.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/tools/RealmDebugTools.kt @@ -42,10 +42,11 @@ internal class RealmDebugTools( separator() append("\n$baseName Realm is empty: ${realm.isEmpty}") var total = 0L + val maxNameLength = realmConfiguration.realmObjectClasses.maxOf { it.simpleName.length } realmConfiguration.realmObjectClasses.forEach { modelClazz -> val count = realm.where(modelClazz).count() total += count - append("\n$baseName Realm - count ${modelClazz.simpleName}: $count") + append("\n$baseName Realm - count ${modelClazz.simpleName.padEnd(maxNameLength)} : $count") } separator() append("\n$baseName Realm - total count: $total") @@ -56,5 +57,5 @@ internal class RealmDebugTools( .let { Timber.i(it) } } - internal fun StringBuilder.separator() = append("\n==============================================") + private fun StringBuilder.separator() = append("\n==============================================") }