Last active
November 1, 2020 13:14
-
-
Save xvadsan/de1c4b67c3c20f396968b7c8ecff7e6a to your computer and use it in GitHub Desktop.
Error exception handler. Writing logs to a file.
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
import android.os.Build | |
import android.os.Environment | |
import android.util.Log | |
import org.threeten.bp.LocalDateTime | |
import tech.dcloud.erfid.core.base.extension.getDateTimeOfPattern | |
import tech.dcloud.erfid.ugrokit.BuildConfig | |
import tech.dcloud.erfid.ugrokit.ui.util.LoggerHandler.Companion.TXT_NAME | |
import java.io.File | |
import java.io.FileOutputStream | |
import java.io.IOException | |
import java.util.* | |
internal class ExceptionHandler : Thread.UncaughtExceptionHandler { | |
private val formatter = LocalDateTime.now().getDateTimeOfPattern("dd_LL_YYYY_HH_mm_ss") | |
private var versionName = "0" | |
private var versionCode = 0 | |
private val previousHandler: Thread.UncaughtExceptionHandler? | |
init { | |
versionName = BuildConfig.VERSION_NAME | |
versionCode = BuildConfig.VERSION_CODE | |
previousHandler = Thread.getDefaultUncaughtExceptionHandler() | |
} | |
override fun uncaughtException(thread: Thread, exception: Throwable) { | |
val state = Environment.getExternalStorageState() | |
val dumpDate = Date(System.currentTimeMillis()) | |
try { | |
if (Environment.MEDIA_MOUNTED == state) { | |
val reportBuilder = StringBuilder() | |
reportBuilder | |
.append("\n\n\n") | |
.append("CrashException").append("\n") | |
.append(formatter.format(dumpDate)).append("\n") | |
.append(String.format("Version: %s (%d)\n", versionName, versionCode)) | |
.append("Device: ${Build.BRAND} ${Build.MODEL}\n") | |
.append("Android ${Build.VERSION.SDK_INT}(API)\n") | |
.append(thread.toString()).append("\n") | |
processThrowable(exception, reportBuilder) | |
val path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) | |
if (!path.exists()) path.mkdirs() | |
val file = File(path, TXT_NAME) | |
if (file.exists().not()) file.createNewFile() | |
val fos = FileOutputStream(file, true) | |
fos.use { it.write(reportBuilder.toString().toByteArray(Charsets.UTF_8)) } | |
fos.use { it.flush() } | |
fos.use { it.close() } | |
} | |
} catch (e: IOException) { | |
Log.println(Log.ERROR, "FileLogTree", "Error while logging into file: $e") | |
} | |
previousHandler?.uncaughtException(thread, exception) | |
} | |
private fun processThrowable(exception: Throwable?, builder: StringBuilder) { | |
if (exception == null) | |
return | |
val stackTraceElements = exception.stackTrace | |
builder | |
.append("Exception: ").append(exception.javaClass.name).append("\n") | |
.append("Message: ").append(exception.message).append("\nStacktrace:\n") | |
for (element in stackTraceElements) { | |
builder.append("\t").append(element.toString()).append("\n") | |
} | |
processThrowable(exception.cause, builder) | |
} | |
companion object { | |
fun reportLogAndHandler(): ExceptionHandler = ExceptionHandler() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
in App in onCreate:
Thread.setDefaultUncaughtExceptionHandler(ExceptionHandler.reportLogAndHandler())