Enable Rust trace log packs (#4514)

* Enable Rust trace log packs

This is a way to forcefully enable trace logs only for a few Rust crates in a safe way
This commit is contained in:
Jorge Martin Espinosa 2025-04-02 13:21:53 +02:00 committed by GitHub
parent b2791d5aad
commit c3ff9c9bda
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 131 additions and 4 deletions

View file

@ -0,0 +1,22 @@
/*
* Copyright 2025 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.libraries.matrix.api.tracing
enum class TraceLogPack(val key: String) {
EVENT_CACHE("event_cache") {
override val title: String = "Event Cache"
},
SEND_QUEUE("send_queue") {
override val title: String = "Send Queue"
},
TIMELINE("timeline") {
override val title: String = "Timeline"
};
abstract val title: String
}

View file

@ -10,6 +10,7 @@ package io.element.android.libraries.matrix.api.tracing
data class TracingConfiguration(
val logLevel: LogLevel,
val extraTargets: List<String>,
val traceLogPacks: Set<TraceLogPack>,
val writesToLogcat: Boolean,
val writesToFilesConfiguration: WriteToFilesConfiguration,
)

View file

@ -51,7 +51,6 @@ fun TracingConfiguration.map(): org.matrix.rustcomponents.sdk.TracingConfigurati
writeToStdoutOrSystem = writesToLogcat,
logLevel = logLevel.toRustLogLevel(),
extraTargets = extraTargets,
// WARNING: this should be used only to debug issues, changes to this value should *never* be published
traceLogPacks = emptyList(),
traceLogPacks = traceLogPacks.map(),
writeToFiles = writesToFilesConfiguration.toTracingFileConfiguration(),
)

View file

@ -0,0 +1,21 @@
/*
* Copyright 2025 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.libraries.matrix.impl.tracing
import io.element.android.libraries.matrix.api.tracing.TraceLogPack
import org.matrix.rustcomponents.sdk.TraceLogPacks as RustTraceLogPack
fun TraceLogPack.map(): RustTraceLogPack = when (this) {
TraceLogPack.SEND_QUEUE -> RustTraceLogPack.SEND_QUEUE
TraceLogPack.EVENT_CACHE -> RustTraceLogPack.EVENT_CACHE
TraceLogPack.TIMELINE -> RustTraceLogPack.TIMELINE
}
fun Collection<TraceLogPack>.map(): List<RustTraceLogPack> {
return map { it.map() }
}

View file

@ -8,6 +8,7 @@
package io.element.android.libraries.preferences.api.store
import io.element.android.libraries.matrix.api.tracing.LogLevel
import io.element.android.libraries.matrix.api.tracing.TraceLogPack
import kotlinx.coroutines.flow.Flow
interface AppPreferencesStore {
@ -26,5 +27,8 @@ interface AppPreferencesStore {
suspend fun setTracingLogLevel(logLevel: LogLevel)
fun getTracingLogLevelFlow(): Flow<LogLevel>
suspend fun setTracingLogPacks(targets: Set<TraceLogPack>)
fun getTracingLogPacksFlow(): Flow<Set<TraceLogPack>>
suspend fun reset()
}

View file

@ -20,6 +20,7 @@ import io.element.android.libraries.core.meta.BuildType
import io.element.android.libraries.di.AppScope
import io.element.android.libraries.di.ApplicationContext
import io.element.android.libraries.matrix.api.tracing.LogLevel
import io.element.android.libraries.matrix.api.tracing.TraceLogPack
import io.element.android.libraries.preferences.api.store.AppPreferencesStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
@ -32,6 +33,7 @@ private val customElementCallBaseUrlKey = stringPreferencesKey("elementCallBaseU
private val themeKey = stringPreferencesKey("theme")
private val hideImagesAndVideosKey = booleanPreferencesKey("hideImagesAndVideos")
private val logLevelKey = stringPreferencesKey("logLevel")
private val traceLogPacksKey = stringPreferencesKey("traceLogPacks")
@ContributesBinding(AppScope::class)
class DefaultAppPreferencesStore @Inject constructor(
@ -105,6 +107,23 @@ class DefaultAppPreferencesStore @Inject constructor(
}
}
override suspend fun setTracingLogPacks(targets: Set<TraceLogPack>) {
val value = targets.joinToString(",") { it.key }
store.edit { prefs ->
prefs[traceLogPacksKey] = value
}
}
override fun getTracingLogPacksFlow(): Flow<Set<TraceLogPack>> {
return store.data.map { prefs ->
prefs[traceLogPacksKey]
?.split(",")
?.mapNotNull { value -> TraceLogPack.entries.find { it.key == value } }
?.toSet()
?: emptySet()
}
}
override suspend fun reset() {
store.edit { it.clear() }
}

View file

@ -8,6 +8,7 @@
package io.element.android.libraries.preferences.test
import io.element.android.libraries.matrix.api.tracing.LogLevel
import io.element.android.libraries.matrix.api.tracing.TraceLogPack
import io.element.android.libraries.preferences.api.store.AppPreferencesStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
@ -19,6 +20,7 @@ class InMemoryAppPreferencesStore(
theme: String? = null,
simplifiedSlidingSyncEnabled: Boolean = false,
logLevel: LogLevel = LogLevel.INFO,
traceLockPacks: Set<TraceLogPack> = emptySet(),
) : AppPreferencesStore {
private val isDeveloperModeEnabled = MutableStateFlow(isDeveloperModeEnabled)
private val hideImagesAndVideos = MutableStateFlow(hideImagesAndVideos)
@ -26,6 +28,7 @@ class InMemoryAppPreferencesStore(
private val theme = MutableStateFlow(theme)
private val simplifiedSlidingSyncEnabled = MutableStateFlow(simplifiedSlidingSyncEnabled)
private val logLevel = MutableStateFlow(logLevel)
private val tracingLogPacks = MutableStateFlow(traceLockPacks)
override suspend fun setDeveloperModeEnabled(enabled: Boolean) {
isDeveloperModeEnabled.value = enabled
@ -67,6 +70,14 @@ class InMemoryAppPreferencesStore(
return logLevel
}
override suspend fun setTracingLogPacks(logPacks: Set<TraceLogPack>) {
tracingLogPacks.value = logPacks
}
override fun getTracingLogPacksFlow(): Flow<Set<TraceLogPack>> {
return tracingLogPacks
}
override suspend fun reset() {
// No op
}