Skip to content

Commit f1eba16

Browse files
Merge pull request #1667 from session-foundation/feature/debug-logger
Debug logger
2 parents 58bcc58 + 96dfc58 commit f1eba16

File tree

13 files changed

+630
-76
lines changed

13 files changed

+630
-76
lines changed

app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import org.session.libsignal.utilities.HTTP.isConnectedToNetwork
5151
import org.session.libsignal.utilities.Log
5252
import org.thoughtcrime.securesms.AppContext.configureKovenant
5353
import org.thoughtcrime.securesms.debugmenu.DebugActivity
54+
import org.thoughtcrime.securesms.debugmenu.DebugLogger
5455
import org.thoughtcrime.securesms.dependencies.DatabaseComponent
5556
import org.thoughtcrime.securesms.dependencies.DatabaseModule.init
5657
import org.thoughtcrime.securesms.dependencies.OnAppStartupComponents
@@ -89,6 +90,7 @@ class ApplicationContext : Application(), DefaultLifecycleObserver, Configuratio
8990

9091
@Inject lateinit var startupComponents: Lazy<OnAppStartupComponents>
9192
@Inject lateinit var persistentLogger: Lazy<PersistentLogger>
93+
@Inject lateinit var debugLogger: Lazy<DebugLogger>
9294
@Inject lateinit var textSecurePreferences: Lazy<TextSecurePreferences>
9395
@Inject lateinit var migrationManager: Lazy<DatabaseMigrationManager>
9496

@@ -208,7 +210,7 @@ class ApplicationContext : Application(), DefaultLifecycleObserver, Configuratio
208210
}
209211

210212
private fun initializeLogging() {
211-
Log.initialize(AndroidLogger(), persistentLogger.get())
213+
Log.initialize(AndroidLogger(), persistentLogger.get(), debugLogger.get())
212214
Logger.addLogger(object : Logger {
213215
private val tag = "LibSession"
214216

app/src/main/java/org/thoughtcrime/securesms/attachments/AvatarReuploadWorker.kt

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.thoughtcrime.securesms.attachments
22

33
import android.content.Context
4-
import android.widget.Toast
54
import androidx.compose.ui.unit.IntSize
65
import androidx.hilt.work.HiltWorker
76
import androidx.work.BackoffPolicy
@@ -17,8 +16,6 @@ import dagger.Lazy
1716
import dagger.assisted.Assisted
1817
import dagger.assisted.AssistedInject
1918
import kotlinx.coroutines.CancellationException
20-
import kotlinx.coroutines.Dispatchers
21-
import kotlinx.coroutines.withContext
2219
import network.loki.messenger.BuildConfig
2320
import okhttp3.HttpUrl.Companion.toHttpUrl
2421
import okio.BufferedSource
@@ -31,8 +28,8 @@ import org.session.libsession.utilities.TextSecurePreferences
3128
import org.session.libsession.utilities.recipients.RemoteFile.Companion.toRemoteFile
3229
import org.session.libsignal.exceptions.NonRetryableException
3330
import org.session.libsignal.utilities.Log
31+
import org.thoughtcrime.securesms.debugmenu.DebugLogGroup
3432
import org.thoughtcrime.securesms.util.BitmapUtil
35-
import org.thoughtcrime.securesms.util.CurrentActivityObserver
3633
import org.thoughtcrime.securesms.util.DateUtils.Companion.secondsToInstant
3734
import org.thoughtcrime.securesms.util.ImageUtils
3835
import java.time.Duration
@@ -54,23 +51,14 @@ class AvatarReuploadWorker @AssistedInject constructor(
5451
private val configFactory: ConfigFactoryProtocol,
5552
private val avatarUploadManager: Lazy<AvatarUploadManager>,
5653
private val localEncryptedFileInputStreamFactory: LocalEncryptedFileInputStream.Factory,
57-
private val fileServerApi: FileServerApi,
58-
private val prefs: TextSecurePreferences,
59-
private val currentActivityObserver: CurrentActivityObserver,
54+
private val fileServerApi: FileServerApi
6055
) : CoroutineWorker(context, params) {
6156

6257
/**
6358
* Log the given message and show a toast if in debug mode
6459
*/
65-
private suspend inline fun logAndToast(message: String, e: Throwable? = null) {
66-
Log.d(TAG, message, e)
67-
68-
val context = currentActivityObserver.currentActivity.value ?: return
69-
if (prefs.debugAvatarReupload || BuildConfig.DEBUG) {
70-
withContext(Dispatchers.Main) {
71-
Toast.makeText(context, "AvatarReupload[debug only]: $message", Toast.LENGTH_SHORT).show()
72-
}
73-
}
60+
private fun log(message: String, e: Throwable? = null) {
61+
Log.d(DebugLogGroup.AVATAR.label, "Avatar Reupload: $message", e)
7462
}
7563

7664
override suspend fun doWork(): Result {
@@ -79,13 +67,13 @@ class AvatarReuploadWorker @AssistedInject constructor(
7967
}
8068

8169
if (profile == null) {
82-
logAndToast("No profile picture set; nothing to do.")
70+
log("No profile picture set; nothing to do.")
8371
return Result.success()
8472
}
8573

8674
val localFile = AvatarDownloadManager.computeFileName(context, profile)
8775
if (!localFile.exists()) {
88-
logAndToast("Avatar file is missing locally; nothing to do.")
76+
log("Avatar file is missing locally; nothing to do.")
8977
return Result.success()
9078
}
9179

@@ -94,7 +82,7 @@ class AvatarReuploadWorker @AssistedInject constructor(
9482
// Check if the file exists and whether we need to do reprocessing, if we do, we reprocess and re-upload
9583
localEncryptedFileInputStreamFactory.create(localFile).use { stream ->
9684
if (stream.meta.hasPermanentDownloadError) {
97-
logAndToast("Permanent download error for current avatar; nothing to do.")
85+
log("Permanent download error for current avatar; nothing to do.")
9886
return Result.success()
9987
}
10088

@@ -103,7 +91,7 @@ class AvatarReuploadWorker @AssistedInject constructor(
10391
val source = stream.source().buffer()
10492

10593
if ((lastUpdated != null && needsReProcessing(source)) || lastUpdated == null) {
106-
logAndToast("About to start reuploading avatar.")
94+
log("About to start reuploading avatar.")
10795
val attachment = attachmentProcessor.processAvatar(
10896
data = source.use { it.readByteArray() },
10997
) ?: return Result.failure()
@@ -118,22 +106,22 @@ class AvatarReuploadWorker @AssistedInject constructor(
118106
} catch (e: CancellationException) {
119107
throw e
120108
} catch (e: NonRetryableException) {
121-
logAndToast("Non-retryable error while reuploading avatar.", e)
109+
log("Non-retryable error while reuploading avatar.", e)
122110
return Result.failure()
123111
} catch (e: Exception) {
124-
logAndToast("Error while reuploading avatar.", e)
112+
log("Error while reuploading avatar.", e)
125113
return Result.retry()
126114
}
127115

128-
logAndToast("Successfully reuploaded avatar.")
116+
log("Successfully reuploaded avatar.")
129117
return Result.success()
130118
}
131119
}
132120

133121
// Otherwise, we only need to renew the same avatar on the server
134122
val parsed = fileServerApi.parseAttachmentUrl(profile.url.toHttpUrl())
135123

136-
logAndToast("Renewing user avatar on ${parsed.fileServer}")
124+
log("Renewing user avatar on ${parsed.fileServer}")
137125
try {
138126
fileServerApi.renew(
139127
fileId = parsed.fileId,
@@ -149,7 +137,7 @@ class AvatarReuploadWorker @AssistedInject constructor(
149137
val now = Instant.now()
150138
if (fileExpiry?.isBefore(now) == true ||
151139
(lastUpdated?.isBefore(now.minus(Duration.ofDays(12)))) == true) {
152-
logAndToast("FileServer renew failed, trying to upload", e)
140+
log("FileServer renew failed, trying to upload", e)
153141
val pictureData =
154142
localEncryptedFileInputStreamFactory.create(localFile).use { stream ->
155143
check(!stream.meta.hasPermanentDownloadError) {
@@ -166,18 +154,18 @@ class AvatarReuploadWorker @AssistedInject constructor(
166154
} catch (e: CancellationException) {
167155
throw e
168156
} catch (e: Exception) {
169-
logAndToast("Error while reuploading avatar after renew failed.", e)
157+
log("Error while reuploading avatar after renew failed.", e)
170158
return Result.failure()
171159
}
172160

173-
logAndToast("Successfully reuploaded avatar after renew failed.")
161+
log("Successfully reuploaded avatar after renew failed.")
174162
} else {
175-
logAndToast( "Not reuploading avatar after renew failed; last updated too recent.")
163+
log( "Not reuploading avatar after renew failed; last updated too recent.")
176164
}
177165

178166
return Result.success()
179167
} else {
180-
logAndToast("Error while renewing avatar. Retrying...", e)
168+
log("Error while renewing avatar. Retrying...", e)
181169
return Result.retry()
182170
}
183171
}

app/src/main/java/org/thoughtcrime/securesms/attachments/AvatarUploadManager.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import org.session.libsession.utilities.Util
2121
import org.session.libsession.utilities.recipients.RemoteFile
2222
import org.session.libsession.utilities.recipients.RemoteFile.Companion.toRemoteFile
2323
import org.session.libsignal.utilities.Log
24+
import org.thoughtcrime.securesms.debugmenu.DebugLogGroup
2425
import org.thoughtcrime.securesms.dependencies.ManagerScope
2526
import org.thoughtcrime.securesms.dependencies.OnAppStartupComponent
2627
import org.thoughtcrime.securesms.util.castAwayType
@@ -41,7 +42,7 @@ class AvatarUploadManager @Inject constructor(
4142
@ManagerScope scope: CoroutineScope,
4243
private val localEncryptedFileOutputStreamFactory: LocalEncryptedFileOutputStream.Factory,
4344
private val fileServerApi: FileServerApi,
44-
private val attachmentProcessor: AttachmentProcessor,
45+
private val attachmentProcessor: AttachmentProcessor
4546
) : OnAppStartupComponent {
4647
init {
4748
// Manage scheduling/cancellation of the AvatarReuploadWorker based on login state
@@ -99,7 +100,7 @@ class AvatarUploadManager @Inject constructor(
99100
customExpiresDuration = DEBUG_AVATAR_TTL.takeIf { prefs.forcedShortTTL() }
100101
)
101102

102-
Log.d(TAG, "Avatar upload finished with $uploadResult")
103+
Log.d(DebugLogGroup.AVATAR.label, "Avatar upload finished with $uploadResult")
103104

104105
val remoteFile = RemoteFile.Encrypted(url = uploadResult.fileUrl, key = Bytes(result.key))
105106

@@ -111,7 +112,7 @@ class AvatarUploadManager @Inject constructor(
111112
it.write(pictureData)
112113
}
113114

114-
Log.d(TAG, "Avatar file written to local storage")
115+
Log.d(DebugLogGroup.AVATAR.label, "Avatar file written to local storage")
115116

116117
// Now that we have the file both locally and remotely, we can update the user profile
117118
val oldPic = configFactory.withMutableUserConfigs {
@@ -134,7 +135,7 @@ class AvatarUploadManager @Inject constructor(
134135
// If we had an old avatar, delete it from local storage
135136
val oldFile = AvatarDownloadManager.computeFileName(application, oldPic)
136137
if (oldFile.exists()) {
137-
Log.d(TAG, "Deleting old avatar file: $oldFile")
138+
Log.d(DebugLogGroup.AVATAR.label, "Deleting old avatar file: $oldFile")
138139
oldFile.delete()
139140
}
140141
}

app/src/main/java/org/thoughtcrime/securesms/debugmenu/DebugActivity.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class DebugActivity : FullComposeActivity() {
99

1010
@Composable
1111
override fun ComposeContent() {
12-
DebugMenuScreen(
13-
onClose = { finish() }
12+
DebugMenuNavHost(
13+
onBack = { finish() }
1414
)
1515
}
1616
}

0 commit comments

Comments
 (0)