-
-
Notifications
You must be signed in to change notification settings - Fork 664
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unlink saving crash stacktrace from Sentry (#4895)
- Loading branch information
Showing
5 changed files
with
58 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
app/src/main/java/io/homeassistant/companion/android/util/CrashSaving.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.homeassistant.companion.android.util | ||
|
||
import android.content.Context | ||
import android.util.Log | ||
import java.io.File | ||
import java.text.SimpleDateFormat | ||
import java.util.Date | ||
import java.util.Locale | ||
import java.util.concurrent.TimeUnit | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
private const val FATAL_CRASH_FILE = "/fatalcrash/last_crash" | ||
|
||
fun initCrashSaving(context: Context) { | ||
val handler = Thread.getDefaultUncaughtExceptionHandler() | ||
Thread.setDefaultUncaughtExceptionHandler { thread, exception -> | ||
// Try saving the crash in a file | ||
try { | ||
val crashFile = File(context.applicationContext.cacheDir.absolutePath + FATAL_CRASH_FILE) | ||
if (!crashFile.exists()) { | ||
crashFile.parentFile?.mkdirs() | ||
crashFile.createNewFile() | ||
} | ||
|
||
crashFile.writeText( | ||
"""|Timestamp: ${SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault()).format(Date())} | ||
|Thread: ${thread.name} | ||
|Exception: ${exception.stackTraceToString()} | ||
""".trimMargin() | ||
) | ||
} catch (e: Exception) { | ||
Log.i("CrashSaving", "Tried saving fatal crash but encountered exception", e) | ||
} | ||
|
||
// Send to crash handling and/or system (and crash) | ||
handler?.uncaughtException(thread, exception) | ||
} | ||
} | ||
|
||
suspend fun getLatestFatalCrash(context: Context): String? = withContext(Dispatchers.IO) { | ||
var toReturn: String? = null | ||
try { | ||
val crashFile = File(context.applicationContext.cacheDir.absolutePath + FATAL_CRASH_FILE) | ||
if (crashFile.exists() && | ||
crashFile.lastModified() >= (System.currentTimeMillis() - TimeUnit.HOURS.toMillis(12)) | ||
) { // Existing, recent file | ||
toReturn = crashFile.readText().trim().ifBlank { null } | ||
} | ||
} catch (e: Exception) { | ||
Log.e("CrashSaving", "Encountered exception while reading crash log file", e) | ||
} | ||
return@withContext toReturn | ||
} |
7 changes: 0 additions & 7 deletions
7
app/src/minimal/java/io/homeassistant/companion/android/CrashHandling.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,7 @@ | ||
package io.homeassistant.companion.android | ||
|
||
import android.content.Context | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
|
||
fun initCrashReporting(context: Context, enabled: Boolean) { | ||
// Noop | ||
} | ||
|
||
suspend fun getLatestFatalCrash(context: Context, enabled: Boolean): String? = withContext(Dispatchers.IO) { | ||
// Noop | ||
return@withContext null | ||
} |