Skip to content
Snippets Groups Projects
Commit 998d8af8 authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Improve crash reporting

parent d93d70c3
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest package="de.kuschku.malheur" />
package="de.kuschku.malheur">
<uses-permission android:name="android.permission.READ_LOGS" />
</manifest>
package de.kuschku.malheur package de.kuschku.malheur
import android.app.Application import android.app.Application
import android.os.Handler
import android.os.HandlerThread
import android.util.Log
import android.widget.Toast
import com.google.gson.GsonBuilder import com.google.gson.GsonBuilder
import de.kuschku.malheur.collectors.ReportCollector import de.kuschku.malheur.collectors.ReportCollector
import de.kuschku.malheur.config.ReportConfig import de.kuschku.malheur.config.ReportConfig
import java.io.File
import java.util.* import java.util.*
object CrashHandler { object CrashHandler {
...@@ -12,16 +17,26 @@ object CrashHandler { ...@@ -12,16 +17,26 @@ object CrashHandler {
private val startTime = Date() private val startTime = Date()
private var originalHandler: Thread.UncaughtExceptionHandler? = null private var originalHandler: Thread.UncaughtExceptionHandler? = null
private lateinit var handler: Handler
fun init(application: Application, config: ReportConfig = ReportConfig(), fun init(application: Application, config: ReportConfig = ReportConfig(),
buildConfig: Class<*>?) { buildConfig: Class<*>?) {
if (myHandler == null) { if (myHandler == null) {
originalHandler = Thread.getDefaultUncaughtExceptionHandler() originalHandler = Thread.getDefaultUncaughtExceptionHandler()
} }
val handlerThread = HandlerThread("Malheur")
handlerThread.start()
handler = Handler(handlerThread.looper)
val reportCollector = ReportCollector(application) val reportCollector = ReportCollector(application)
myHandler = Thread.UncaughtExceptionHandler { activeThread, throwable -> myHandler = Thread.UncaughtExceptionHandler { activeThread, throwable ->
val crashTime = Date() val crashTime = Date()
val stackTraces = Thread.getAllStackTraces() val stackTraces = Thread.getAllStackTraces()
Log.e("Malheur", "Creating crash report")
handler.post {
Toast.makeText(application, "Creating crash report", Toast.LENGTH_LONG).show()
}
Thread { Thread {
try { try {
val json = gson.toJson( val json = gson.toJson(
...@@ -38,7 +53,17 @@ object CrashHandler { ...@@ -38,7 +53,17 @@ object CrashHandler {
), config ), config
) )
) )
// FIXME STOPSHIP Implement crash handling val crashDirectory = File(application.cacheDir, "crashes")
crashDirectory.mkdirs()
val crashFile = File(crashDirectory, "${System.currentTimeMillis()}.json")
crashFile.createNewFile()
crashFile.writeText(json)
Log.e("Malheur", "Crash report saved: $crashFile")
handler.post {
Toast.makeText(application,
"Crash report saved: ${crashFile.name}",
Toast.LENGTH_LONG).show()
}
} catch (e: Throwable) { } catch (e: Throwable) {
e.printStackTrace() e.printStackTrace()
originalHandler?.uncaughtException(activeThread, throwable) originalHandler?.uncaughtException(activeThread, throwable)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment