2021-02-06 11:50:57 +01:00
|
|
|
package org.moire.ultrasonic.util
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.os.Build
|
|
|
|
import java.io.File
|
|
|
|
import java.io.PrintWriter
|
2021-02-14 15:55:16 +01:00
|
|
|
import timber.log.Timber
|
2021-02-06 11:50:57 +01:00
|
|
|
|
|
|
|
private const val filename = "ultrasonic-stacktrace.txt"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs the stack trace of uncaught exceptions to a file on the SD card.
|
|
|
|
*/
|
2021-02-14 15:55:16 +01:00
|
|
|
class SubsonicUncaughtExceptionHandler(
|
2021-02-06 11:50:57 +01:00
|
|
|
private val context: Context
|
2021-02-14 15:55:16 +01:00
|
|
|
) : Thread.UncaughtExceptionHandler {
|
|
|
|
private val defaultHandler: Thread.UncaughtExceptionHandler? =
|
|
|
|
Thread.getDefaultUncaughtExceptionHandler()
|
2021-02-06 11:50:57 +01:00
|
|
|
|
|
|
|
override fun uncaughtException(thread: Thread, throwable: Throwable) {
|
|
|
|
var file: File? = null
|
|
|
|
var printWriter: PrintWriter? = null
|
|
|
|
|
|
|
|
try {
|
|
|
|
file = File(FileUtil.getUltrasonicDirectory(context), filename)
|
|
|
|
printWriter = PrintWriter(file)
|
|
|
|
val logMessage = String.format(
|
2021-02-14 15:55:16 +01:00
|
|
|
"Android API level: %s\nUltrasonic version name: %s\n" +
|
|
|
|
"Ultrasonic version code: %s\n\n",
|
|
|
|
Build.VERSION.SDK_INT, Util.getVersionName(context), Util.getVersionCode(context)
|
|
|
|
)
|
2021-02-06 11:50:57 +01:00
|
|
|
printWriter.println(logMessage)
|
|
|
|
throwable.printStackTrace(printWriter)
|
|
|
|
Timber.e(throwable, "Uncaught Exception! %s", logMessage)
|
|
|
|
Timber.i("Stack trace written to %s", file)
|
|
|
|
} catch (x: Throwable) {
|
|
|
|
Timber.e(x, "Failed to write stack trace to %s", file)
|
|
|
|
} finally {
|
|
|
|
Util.close(printWriter)
|
|
|
|
defaultHandler?.uncaughtException(thread, throwable)
|
|
|
|
}
|
|
|
|
}
|
2021-02-14 15:55:16 +01:00
|
|
|
}
|