|
| 1 | +package com.bugsnag.android |
| 2 | + |
| 3 | +import java.io.File |
| 4 | +import java.util.Locale |
| 5 | +import java.util.UUID |
| 6 | + |
| 7 | +/** |
| 8 | + * Represents important information about an event which is encoded/decoded from a filename. |
| 9 | + * Currently the following information is encoded: |
| 10 | + * |
| 11 | + * apiKey - as a user can decide to override the value on an Event |
| 12 | + * uuid - to disambiguate stored error reports |
| 13 | + * timestamp - to sort error reports by time of capture |
| 14 | + * suffix - used to encode whether the app crashed on launch, or the report is not a JVM error |
| 15 | + * errorTypes - a comma delimited string which contains the stackframe types in the error |
| 16 | + */ |
| 17 | +internal data class EventFilenameInfo( |
| 18 | + val apiKey: String, |
| 19 | + val uuid: String, |
| 20 | + val timestamp: Long, |
| 21 | + val suffix: String, |
| 22 | + val errorTypes: Set<ErrorType> |
| 23 | +) { |
| 24 | + |
| 25 | + /** |
| 26 | + * Generates a filename for the Event in the format |
| 27 | + * "[timestamp]_[apiKey]_[errorTypes]_[UUID]_[startupcrash|not-jvm].json" |
| 28 | + */ |
| 29 | + fun encode(): String { |
| 30 | + return String.format( |
| 31 | + Locale.US, |
| 32 | + "%d_%s_%s_%s_%s.json", |
| 33 | + timestamp, |
| 34 | + apiKey, |
| 35 | + serializeErrorTypeHeader(errorTypes), |
| 36 | + uuid, |
| 37 | + suffix |
| 38 | + ) |
| 39 | + } |
| 40 | + |
| 41 | + fun isLaunchCrashReport(): Boolean = suffix == STARTUP_CRASH |
| 42 | + |
| 43 | + internal companion object { |
| 44 | + private const val STARTUP_CRASH = "startupcrash" |
| 45 | + private const val NON_JVM_CRASH = "not-jvm" |
| 46 | + |
| 47 | + @JvmOverloads |
| 48 | + fun fromEvent( |
| 49 | + obj: Any, |
| 50 | + uuid: String = UUID.randomUUID().toString(), |
| 51 | + apiKey: String?, |
| 52 | + timestamp: Long = System.currentTimeMillis(), |
| 53 | + config: ImmutableConfig |
| 54 | + ): EventFilenameInfo { |
| 55 | + val sanitizedApiKey = when { |
| 56 | + obj is Event -> obj.apiKey |
| 57 | + apiKey.isNullOrEmpty() -> config.apiKey |
| 58 | + else -> apiKey |
| 59 | + } |
| 60 | + |
| 61 | + return EventFilenameInfo( |
| 62 | + sanitizedApiKey, |
| 63 | + uuid, |
| 64 | + timestamp, |
| 65 | + findSuffixForEvent(obj, config), |
| 66 | + findErrorTypesForEvent(obj) |
| 67 | + ) |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Reads event information from a filename. |
| 72 | + */ |
| 73 | + fun fromFile(file: File, config: ImmutableConfig): EventFilenameInfo { |
| 74 | + return EventFilenameInfo( |
| 75 | + findApiKeyInFilename(file, config), |
| 76 | + "", // ignore UUID field when reading from file as unused |
| 77 | + -1, // ignore timestamp when reading from file as unused |
| 78 | + findSuffixInFilename(file), |
| 79 | + findErrorTypesInFilename(file) |
| 80 | + ) |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Retrieves the api key encoded in the filename, or an empty string if this information |
| 85 | + * is not encoded for the given event |
| 86 | + */ |
| 87 | + private fun findApiKeyInFilename(file: File, config: ImmutableConfig): String { |
| 88 | + val name = file.name.replace("_$STARTUP_CRASH.json".toRegex(), "") |
| 89 | + val start = name.indexOf("_") + 1 |
| 90 | + val end = name.indexOf("_", start) |
| 91 | + val apiKey = if (start == 0 || end == -1 || end <= start) { |
| 92 | + null |
| 93 | + } else { |
| 94 | + name.substring(start, end) |
| 95 | + } |
| 96 | + return apiKey ?: config.apiKey |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Retrieves the error types encoded in the filename, or an empty string if this |
| 101 | + * information is not encoded for the given event |
| 102 | + */ |
| 103 | + private fun findErrorTypesInFilename(eventFile: File): Set<ErrorType> { |
| 104 | + val name = eventFile.name |
| 105 | + val end = name.lastIndexOf("_", name.lastIndexOf("_") - 1) |
| 106 | + val start = name.lastIndexOf("_", end - 1) + 1 |
| 107 | + |
| 108 | + if (start < end) { |
| 109 | + val encodedValues: List<String> = name.substring(start, end).split(",") |
| 110 | + return ErrorType.values().filter { |
| 111 | + encodedValues.contains(it.desc) |
| 112 | + }.toSet() |
| 113 | + } |
| 114 | + return emptySet() |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Retrieves the error types encoded in the filename, or an empty string if this |
| 119 | + * information is not encoded for the given event |
| 120 | + */ |
| 121 | + private fun findSuffixInFilename(eventFile: File): String { |
| 122 | + val name = eventFile.nameWithoutExtension |
| 123 | + val suffix = name.substring(name.lastIndexOf("_") + 1) |
| 124 | + return when (suffix) { |
| 125 | + STARTUP_CRASH, NON_JVM_CRASH -> suffix |
| 126 | + else -> "" |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Retrieves the error types for the given event |
| 132 | + */ |
| 133 | + private fun findErrorTypesForEvent(obj: Any): Set<ErrorType> { |
| 134 | + return when (obj) { |
| 135 | + is Event -> obj.impl.getErrorTypesFromStackframes() |
| 136 | + else -> setOf(ErrorType.C) |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Calculates the suffix for the given event |
| 142 | + */ |
| 143 | + private fun findSuffixForEvent(obj: Any, config: ImmutableConfig): String { |
| 144 | + return when (obj) { |
| 145 | + is Event -> { |
| 146 | + val duration = obj.app.duration |
| 147 | + if (duration != null && isStartupCrash(duration.toLong(), config)) { |
| 148 | + STARTUP_CRASH |
| 149 | + } else { |
| 150 | + "" |
| 151 | + } |
| 152 | + } |
| 153 | + else -> { |
| 154 | + NON_JVM_CRASH |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private fun isStartupCrash(durationMs: Long, config: ImmutableConfig): Boolean { |
| 160 | + return durationMs < config.launchCrashThresholdMs |
| 161 | + } |
| 162 | + } |
| 163 | +} |
| 164 | + |
0 commit comments