Store log files in subfolder based on the homeserver domain.
This commit is contained in:
parent
37786352ba
commit
18c325560b
9 changed files with 90 additions and 22 deletions
|
|
@ -16,5 +16,6 @@ dependencies {
|
|||
implementation(projects.libraries.architecture)
|
||||
implementation(projects.libraries.designsystem)
|
||||
implementation(projects.libraries.androidutils)
|
||||
implementation(projects.libraries.matrix.api)
|
||||
implementation(projects.libraries.uiStrings)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
* 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.features.rageshake.api.logs
|
||||
|
||||
import io.element.android.features.rageshake.api.reporter.BugReporter
|
||||
import io.element.android.libraries.matrix.api.tracing.WriteToFilesConfiguration
|
||||
|
||||
fun BugReporter.createWriteToFilesConfiguration(): WriteToFilesConfiguration {
|
||||
return WriteToFilesConfiguration.Enabled(
|
||||
directory = logDirectory().absolutePath,
|
||||
filenamePrefix = "logs",
|
||||
// Keep a maximum of 1 week of log files.
|
||||
numberOfFiles = 7 * 24,
|
||||
)
|
||||
}
|
||||
|
|
@ -34,6 +34,14 @@ interface BugReporter {
|
|||
*/
|
||||
fun logDirectory(): File
|
||||
|
||||
/**
|
||||
* Set the subfolder name for the log directory.
|
||||
* This will create a subfolder in the log directory with the given name.
|
||||
* It will also configure the Rust SDK to use this subfolder for its logs.
|
||||
* If the name is null, the log files will be stored in the base folder for the logs.
|
||||
*/
|
||||
fun setLogDirectorySubfolder(subfolderName: String?)
|
||||
|
||||
/**
|
||||
* Set the current tracing log level.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ import androidx.core.net.toFile
|
|||
import androidx.core.net.toUri
|
||||
import com.squareup.anvil.annotations.ContributesBinding
|
||||
import io.element.android.appconfig.RageshakeConfig
|
||||
import io.element.android.features.rageshake.api.logs.createWriteToFilesConfiguration
|
||||
import io.element.android.features.rageshake.api.reporter.BugReporter
|
||||
import io.element.android.features.rageshake.api.reporter.BugReporterListener
|
||||
import io.element.android.features.rageshake.impl.crash.CrashDataStore
|
||||
|
|
@ -28,11 +29,14 @@ import io.element.android.libraries.di.ApplicationContext
|
|||
import io.element.android.libraries.di.SingleIn
|
||||
import io.element.android.libraries.matrix.api.MatrixClientProvider
|
||||
import io.element.android.libraries.matrix.api.SdkMetadata
|
||||
import io.element.android.libraries.matrix.api.auth.MatrixAuthenticationService
|
||||
import io.element.android.libraries.matrix.api.core.UserId
|
||||
import io.element.android.libraries.matrix.api.tracing.TracingService
|
||||
import io.element.android.libraries.network.useragent.UserAgentProvider
|
||||
import io.element.android.libraries.sessionstorage.api.SessionStore
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import kotlinx.coroutines.withContext
|
||||
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
||||
import okhttp3.OkHttpClient
|
||||
|
|
@ -71,6 +75,8 @@ class DefaultBugReporter @Inject constructor(
|
|||
private val bugReporterUrlProvider: BugReporterUrlProvider,
|
||||
private val sdkMetadata: SdkMetadata,
|
||||
private val matrixClientProvider: MatrixClientProvider,
|
||||
private val tracingService: TracingService,
|
||||
matrixAuthenticationService: MatrixAuthenticationService,
|
||||
) : BugReporter {
|
||||
companion object {
|
||||
// filenames
|
||||
|
|
@ -81,7 +87,21 @@ class DefaultBugReporter @Inject constructor(
|
|||
private val logcatCommandDebug = arrayOf("logcat", "-d", "-v", "threadtime", "*:*")
|
||||
private var currentTracingLogLevel: String? = null
|
||||
|
||||
private val logCatErrFile = File(logDirectory().absolutePath, LOG_CAT_FILENAME)
|
||||
private val logCatErrFile: File
|
||||
get() = File(logDirectory(), LOG_CAT_FILENAME)
|
||||
private val baseLogDirectory = File(context.cacheDir, LOG_DIRECTORY_NAME)
|
||||
private var currentLogDirectory: File = baseLogDirectory
|
||||
|
||||
init {
|
||||
val logSubfolder = runBlocking {
|
||||
sessionStore.getLatestSession()
|
||||
}?.userId?.substringAfter(":")
|
||||
setCurrentLogDirectory(logSubfolder)
|
||||
matrixAuthenticationService.listenToNewMatrixClients {
|
||||
// When a new Matrix client is created, we update the tracing configuration to write to files
|
||||
setLogDirectorySubfolder(it.userIdServerName())
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun sendBugReport(
|
||||
withDevicesLogs: Boolean,
|
||||
|
|
@ -286,11 +306,24 @@ class DefaultBugReporter @Inject constructor(
|
|||
}
|
||||
|
||||
override fun logDirectory(): File {
|
||||
return File(context.cacheDir, LOG_DIRECTORY_NAME).apply {
|
||||
return currentLogDirectory.apply {
|
||||
mkdirs()
|
||||
}
|
||||
}
|
||||
|
||||
override fun setLogDirectorySubfolder(subfolderName: String?) {
|
||||
setCurrentLogDirectory(subfolderName)
|
||||
tracingService.updateWriteToFilesConfiguration(createWriteToFilesConfiguration())
|
||||
}
|
||||
|
||||
private fun setCurrentLogDirectory(subfolderName: String?) {
|
||||
currentLogDirectory = if (subfolderName == null) {
|
||||
baseLogDirectory
|
||||
} else {
|
||||
File(baseLogDirectory, subfolderName)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun deleteAllFiles(predicate: (File) -> Boolean) {
|
||||
withContext(coroutineDispatchers.io) {
|
||||
getLogFiles()
|
||||
|
|
@ -325,11 +358,12 @@ class DefaultBugReporter @Inject constructor(
|
|||
* @return the file if the operation succeeds
|
||||
*/
|
||||
override fun saveLogCat() {
|
||||
if (logCatErrFile.exists()) {
|
||||
logCatErrFile.safeDelete()
|
||||
val file = logCatErrFile
|
||||
if (file.exists()) {
|
||||
file.safeDelete()
|
||||
}
|
||||
try {
|
||||
logCatErrFile.writer().use {
|
||||
file.writer().use {
|
||||
getLogCatError(it)
|
||||
}
|
||||
} catch (error: OutOfMemoryError) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue