|
10 | 10 | import io.sentry.UncaughtExceptionHandlerIntegration; |
11 | 11 | import io.sentry.android.core.AnrV2Integration; |
12 | 12 | import io.sentry.android.core.SentryAndroidOptions; |
| 13 | +import io.sentry.android.core.TombstoneIntegration; |
13 | 14 | import io.sentry.android.core.internal.util.AndroidCurrentDateProvider; |
14 | 15 | import io.sentry.android.core.performance.AppStartMetrics; |
15 | 16 | import io.sentry.android.core.performance.TimeSpan; |
|
31 | 32 | public final class AndroidEnvelopeCache extends EnvelopeCache { |
32 | 33 |
|
33 | 34 | public static final String LAST_ANR_REPORT = "last_anr_report"; |
| 35 | + public static final String LAST_TOMBSTONE_REPORT = "last_tombstone_report"; |
34 | 36 |
|
35 | 37 | private final @NotNull ICurrentDateProvider currentDateProvider; |
36 | 38 |
|
@@ -94,6 +96,22 @@ private boolean storeInternalAndroid(@NotNull SentryEnvelope envelope, @NotNull |
94 | 96 |
|
95 | 97 | writeLastReportedAnrMarker(timestamp); |
96 | 98 | }); |
| 99 | + |
| 100 | + HintUtils.runIfHasType( |
| 101 | + hint, |
| 102 | + TombstoneIntegration.TombstoneHint.class, |
| 103 | + (tombstoneHint) -> { |
| 104 | + final @Nullable Long timestamp = tombstoneHint.timestamp(); |
| 105 | + options |
| 106 | + .getLogger() |
| 107 | + .log( |
| 108 | + SentryLevel.DEBUG, |
| 109 | + "Writing last reported Tombstone marker with timestamp %d", |
| 110 | + timestamp); |
| 111 | + |
| 112 | + writeLastReportedTombstoneMarker(timestamp); |
| 113 | + }); |
| 114 | + |
97 | 115 | return didStore; |
98 | 116 | } |
99 | 117 |
|
@@ -152,42 +170,74 @@ public static boolean hasStartupCrashMarker(final @NotNull SentryOptions options |
152 | 170 | return false; |
153 | 171 | } |
154 | 172 |
|
155 | | - public static @Nullable Long lastReportedAnr(final @NotNull SentryOptions options) { |
| 173 | + private static @Nullable Long lastReportedMarker( |
| 174 | + final @NotNull SentryOptions options, |
| 175 | + @NotNull String reportFilename, |
| 176 | + @NotNull String markerCategory) { |
156 | 177 | final String cacheDirPath = |
157 | 178 | Objects.requireNonNull( |
158 | | - options.getCacheDirPath(), "Cache dir path should be set for getting ANRs reported"); |
| 179 | + options.getCacheDirPath(), |
| 180 | + "Cache dir path should be set for getting " + markerCategory + "s reported"); |
159 | 181 |
|
160 | | - final File lastAnrMarker = new File(cacheDirPath, LAST_ANR_REPORT); |
| 182 | + final File lastMarker = new File(cacheDirPath, reportFilename); |
161 | 183 | try { |
162 | | - final String content = FileUtils.readText(lastAnrMarker); |
| 184 | + final String content = FileUtils.readText(lastMarker); |
163 | 185 | // we wrapped into try-catch already |
164 | 186 | //noinspection ConstantConditions |
165 | 187 | return content.equals("null") ? null : Long.parseLong(content.trim()); |
166 | 188 | } catch (Throwable e) { |
167 | 189 | if (e instanceof FileNotFoundException) { |
168 | 190 | options |
169 | 191 | .getLogger() |
170 | | - .log(DEBUG, "Last ANR marker does not exist. %s.", lastAnrMarker.getAbsolutePath()); |
| 192 | + .log( |
| 193 | + DEBUG, |
| 194 | + "Last " + markerCategory + " marker does not exist. %s.", |
| 195 | + lastMarker.getAbsolutePath()); |
171 | 196 | } else { |
172 | | - options.getLogger().log(ERROR, "Error reading last ANR marker", e); |
| 197 | + options.getLogger().log(ERROR, "Error reading last " + markerCategory + " marker", e); |
173 | 198 | } |
174 | 199 | } |
175 | 200 | return null; |
176 | 201 | } |
177 | 202 |
|
178 | | - private void writeLastReportedAnrMarker(final @Nullable Long timestamp) { |
| 203 | + private void writeLastReportedMarker( |
| 204 | + final @Nullable Long timestamp, |
| 205 | + @NotNull String reportFilename, |
| 206 | + @NotNull String markerCategory) { |
179 | 207 | final String cacheDirPath = options.getCacheDirPath(); |
180 | 208 | if (cacheDirPath == null) { |
181 | | - options.getLogger().log(DEBUG, "Cache dir path is null, the ANR marker will not be written"); |
| 209 | + options |
| 210 | + .getLogger() |
| 211 | + .log( |
| 212 | + DEBUG, |
| 213 | + "Cache dir path is null, the " + markerCategory + " marker will not be written"); |
182 | 214 | return; |
183 | 215 | } |
184 | 216 |
|
185 | | - final File anrMarker = new File(cacheDirPath, LAST_ANR_REPORT); |
| 217 | + final File anrMarker = new File(cacheDirPath, reportFilename); |
186 | 218 | try (final OutputStream outputStream = new FileOutputStream(anrMarker)) { |
187 | 219 | outputStream.write(String.valueOf(timestamp).getBytes(UTF_8)); |
188 | 220 | outputStream.flush(); |
189 | 221 | } catch (Throwable e) { |
190 | | - options.getLogger().log(ERROR, "Error writing the ANR marker to the disk", e); |
| 222 | + options |
| 223 | + .getLogger() |
| 224 | + .log(ERROR, "Error writing the " + markerCategory + " marker to the disk", e); |
191 | 225 | } |
192 | 226 | } |
| 227 | + |
| 228 | + public static @Nullable Long lastReportedAnr(final @NotNull SentryOptions options) { |
| 229 | + return lastReportedMarker(options, LAST_ANR_REPORT, "ANR"); |
| 230 | + } |
| 231 | + |
| 232 | + private void writeLastReportedAnrMarker(final @Nullable Long timestamp) { |
| 233 | + this.writeLastReportedMarker(timestamp, LAST_ANR_REPORT, "ANR"); |
| 234 | + } |
| 235 | + |
| 236 | + public static @Nullable Long lastReportedTombstone(final @NotNull SentryOptions options) { |
| 237 | + return lastReportedMarker(options, LAST_TOMBSTONE_REPORT, "Tombstone"); |
| 238 | + } |
| 239 | + |
| 240 | + private void writeLastReportedTombstoneMarker(final @Nullable Long timestamp) { |
| 241 | + this.writeLastReportedMarker(timestamp, LAST_TOMBSTONE_REPORT, "Tombstone"); |
| 242 | + } |
193 | 243 | } |
0 commit comments