From 226510007b94a79528d4d8e119191b9f48b321cc Mon Sep 17 00:00:00 2001
From: Matthias Urhahn <darken@darken.eu>
Date: Mon, 21 Sep 2020 13:51:17 +0200
Subject: [PATCH] Add extra logging for transaction and worker exceptions.
 (#1195) (EXPOSUREAPP-2640)

---
 .../worker/BackgroundWorkHelper.kt            |  2 ++
 .../DiagnosisKeyRetrievalOneTimeWorker.kt     | 20 +++++++++++++++----
 .../DiagnosisKeyRetrievalPeriodicWorker.kt    | 20 +++++++++++++++----
 3 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/worker/BackgroundWorkHelper.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/worker/BackgroundWorkHelper.kt
index c7e797fa3..9e9fb0a70 100644
--- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/worker/BackgroundWorkHelper.kt
+++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/worker/BackgroundWorkHelper.kt
@@ -5,6 +5,7 @@ import androidx.work.Constraints
 import androidx.work.NetworkType
 import de.rki.coronawarnapp.notification.NotificationHelper
 import de.rki.coronawarnapp.storage.LocalData
+import timber.log.Timber
 import kotlin.random.Random
 
 /**
@@ -90,6 +91,7 @@ object BackgroundWorkHelper {
      * @see LocalData.backgroundNotification()
      */
     fun sendDebugNotification(title: String, content: String) {
+        Timber.d("sendDebugNotification(title=%s, content=%s)", title, content)
         if (!LocalData.backgroundNotification()) return
         NotificationHelper.sendNotification(title, content, NotificationCompat.PRIORITY_HIGH, true)
     }
diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/worker/DiagnosisKeyRetrievalOneTimeWorker.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/worker/DiagnosisKeyRetrievalOneTimeWorker.kt
index e45eb28ba..c404b59e4 100644
--- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/worker/DiagnosisKeyRetrievalOneTimeWorker.kt
+++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/worker/DiagnosisKeyRetrievalOneTimeWorker.kt
@@ -27,28 +27,40 @@ class DiagnosisKeyRetrievalOneTimeWorker(val context: Context, workerParams: Wor
      * @see RetrieveDiagnosisKeysTransaction
      */
     override suspend fun doWork(): Result {
-        Timber.d("Background job started. Run attempt: $runAttemptCount ")
+        Timber.tag(TAG).d("$id: doWork() started. Run attempt: $runAttemptCount")
+
         BackgroundWorkHelper.sendDebugNotification(
-            "KeyOneTime Executing: Start", "KeyOneTime started. Run attempt: $runAttemptCount ")
+            "KeyOneTime Executing: Start", "KeyOneTime started. Run attempt: $runAttemptCount "
+        )
 
         var result = Result.success()
         try {
             RetrieveDiagnosisKeysTransaction.startWithConstraints()
         } catch (e: Exception) {
+            Timber.tag(TAG).w(
+                e, "$id: Error during RetrieveDiagnosisKeysTransaction.startWithConstraints()."
+            )
+
             if (runAttemptCount > BackgroundConstants.WORKER_RETRY_COUNT_THRESHOLD) {
+                Timber.tag(TAG).w(e, "$id: Retry attempts exceeded.")
 
                 BackgroundWorkHelper.sendDebugNotification(
-                    "KeyOneTime Executing: Failure", "KeyOneTime failed with $runAttemptCount attempts")
+                    "KeyOneTime Executing: Failure",
+                    "KeyOneTime failed with $runAttemptCount attempts"
+                )
 
                 return Result.failure()
             } else {
+                Timber.tag(TAG).d(e, "$id: Retrying.")
                 result = Result.retry()
             }
         }
 
         BackgroundWorkHelper.sendDebugNotification(
-            "KeyOneTime Executing: End", "KeyOneTime result: $result ")
+            "KeyOneTime Executing: End", "KeyOneTime result: $result "
+        )
 
+        Timber.tag(TAG).d("$id: doWork() finished with %s", result)
         return result
     }
 }
diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/worker/DiagnosisKeyRetrievalPeriodicWorker.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/worker/DiagnosisKeyRetrievalPeriodicWorker.kt
index 79f091610..03417c199 100644
--- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/worker/DiagnosisKeyRetrievalPeriodicWorker.kt
+++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/worker/DiagnosisKeyRetrievalPeriodicWorker.kt
@@ -28,28 +28,40 @@ class DiagnosisKeyRetrievalPeriodicWorker(val context: Context, workerParams: Wo
      * @see BackgroundWorkScheduler.scheduleDiagnosisKeyOneTimeWork()
      */
     override suspend fun doWork(): Result {
-        Timber.d("Background job started. Run attempt: $runAttemptCount")
+        Timber.tag(TAG).d("$id: doWork() started. Run attempt: $runAttemptCount")
+
         BackgroundWorkHelper.sendDebugNotification(
-            "KeyPeriodic Executing: Start", "KeyPeriodic started. Run attempt: $runAttemptCount ")
+            "KeyPeriodic Executing: Start", "KeyPeriodic started. Run attempt: $runAttemptCount"
+        )
 
         var result = Result.success()
         try {
             BackgroundWorkScheduler.scheduleDiagnosisKeyOneTimeWork()
         } catch (e: Exception) {
+            Timber.tag(TAG).w(
+                e, "$id: Error during BackgroundWorkScheduler.scheduleDiagnosisKeyOneTimeWork()."
+            )
+
             if (runAttemptCount > BackgroundConstants.WORKER_RETRY_COUNT_THRESHOLD) {
+                Timber.tag(TAG).w(e, "$id: Retry attempts exceeded.")
 
                 BackgroundWorkHelper.sendDebugNotification(
-                    "KeyPeriodic Executing: Failure", "KeyPeriodic failed with $runAttemptCount attempts")
+                    "KeyPeriodic Executing: Failure",
+                    "KeyPeriodic failed with $runAttemptCount attempts"
+                )
 
                 return Result.failure()
             } else {
+                Timber.tag(TAG).d(e, "$id: Retrying.")
                 result = Result.retry()
             }
         }
 
         BackgroundWorkHelper.sendDebugNotification(
-            "KeyPeriodic Executing: End", "KeyPeriodic result: $result ")
+            "KeyPeriodic Executing: End", "KeyPeriodic result: $result "
+        )
 
+        Timber.tag(TAG).d("$id: doWork() finished with %s", result)
         return result
     }
 }
-- 
GitLab