Skip to content

Commit

Permalink
Unlink saving crash stacktrace from Sentry (#4895)
Browse files Browse the repository at this point in the history
  • Loading branch information
jpelgrom authored Dec 10, 2024
1 parent 0493fa0 commit af6bc04
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
package io.homeassistant.companion.android

import android.content.Context
import android.util.Log
import io.sentry.SentryOptions
import io.sentry.android.core.SentryAndroid
import java.io.File
import java.io.PrintWriter
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.net.UnknownHostException
import java.text.SimpleDateFormat
import java.util.Locale
import java.util.concurrent.TimeUnit
import javax.net.ssl.SSLException
import javax.net.ssl.SSLHandshakeException
import javax.net.ssl.SSLPeerUnverifiedException
import javax.net.ssl.SSLProtocolException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

private const val FATAL_CRASH_FILE = "/fatalcrash/last_crash"

fun initCrashReporting(context: Context, enabled: Boolean) {
// Don't init on debug builds or when disabled
Expand All @@ -31,27 +20,6 @@ fun initCrashReporting(context: Context, enabled: Boolean) {
options.isEnableAutoSessionTracking = true
options.isEnableNdk = false

options.beforeSend = SentryOptions.BeforeSendCallback { event, _ ->
if (event.isCrashed && event.throwable != null) {
try {
val crashFile = File(context.applicationContext.cacheDir.absolutePath + FATAL_CRASH_FILE)
if (!crashFile.exists()) {
crashFile.parentFile?.mkdirs()
crashFile.createNewFile()
}
val stacktraceWriter = PrintWriter(crashFile)
val timestamp = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault()).format(event.timestamp)
stacktraceWriter.print("$timestamp: ")
event.throwable?.printStackTrace(stacktraceWriter)
stacktraceWriter.close()
} catch (e: Exception) {
Log.i("CrashHandling", "Tried saving fatal crash but encountered exception", e)
}
}

return@BeforeSendCallback event
}

val ignoredEvents = arrayOf(
ConnectException::class.java,
SocketTimeoutException::class.java,
Expand All @@ -66,23 +34,4 @@ fun initCrashReporting(context: Context, enabled: Boolean) {
}
}

suspend fun getLatestFatalCrash(context: Context, enabled: Boolean): String? = withContext(Dispatchers.IO) {
if (!shouldEnableCrashHandling(enabled)) {
return@withContext null
}

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("CrashHandling", "Encountered exception while reading crash log file", e)
}
return@withContext toReturn
}

private fun shouldEnableCrashHandling(enabled: Boolean) = !BuildConfig.DEBUG && enabled
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import io.homeassistant.companion.android.database.settings.SensorUpdateFrequenc
import io.homeassistant.companion.android.sensors.SensorReceiver
import io.homeassistant.companion.android.settings.language.LanguagesManager
import io.homeassistant.companion.android.util.LifecycleHandler
import io.homeassistant.companion.android.util.initCrashSaving
import io.homeassistant.companion.android.websocket.WebsocketBroadcastReceiver
import io.homeassistant.companion.android.widgets.button.ButtonWidget
import io.homeassistant.companion.android.widgets.entity.EntityWidget
Expand Down Expand Up @@ -60,6 +61,7 @@ open class HomeAssistantApplication : Application() {
applicationContext,
prefsRepository.isCrashReporting()
)
initCrashSaving(applicationContext)
}

languagesManager.applyCurrentLang()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.R
import io.homeassistant.companion.android.common.R as commonR
import io.homeassistant.companion.android.common.data.prefs.PrefsRepository
import io.homeassistant.companion.android.getLatestFatalCrash
import io.homeassistant.companion.android.util.LogcatReader
import io.homeassistant.companion.android.util.getLatestFatalCrash
import java.io.File
import java.util.Calendar
import javax.inject.Inject
Expand Down Expand Up @@ -115,7 +115,7 @@ class LogFragment : Fragment() {

// Runs with Dispatcher IO
processLog = LogcatReader.readLog()
crashLog = getLatestFatalCrash(requireContext(), prefsRepository.isCrashReporting())
crashLog = getLatestFatalCrash(requireContext())

showLog()
showHideLogLoader(false)
Expand Down
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
}
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
}

0 comments on commit af6bc04

Please sign in to comment.