From a759638bcd53826fd218462215adb69ad0c45681 Mon Sep 17 00:00:00 2001 From: Tuomas Airaksinen Date: Thu, 24 Nov 2022 18:39:00 +0200 Subject: [PATCH] Fix #2385 (Logcat should be saved immediately after crash) --- .../net/bible/android/BibleApplication.kt | 1 + .../control/report/ErrorReportControl.kt | 103 ++++++++++-------- 2 files changed, 56 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/net/bible/android/BibleApplication.kt b/app/src/main/java/net/bible/android/BibleApplication.kt index 034f270f5f..a67e014506 100644 --- a/app/src/main/java/net/bible/android/BibleApplication.kt +++ b/app/src/main/java/net/bible/android/BibleApplication.kt @@ -96,6 +96,7 @@ open class BibleApplication : Application() { val defaultExceptionHandler = Thread.getDefaultUncaughtExceptionHandler() Thread.setDefaultUncaughtExceptionHandler { t, e -> BugReport.saveScreenshot() + BugReport.saveLogcat() CommonUtils.realSharedPreferences.edit().putBoolean("app-crashed", true).commit() defaultExceptionHandler.uncaughtException(t, e) } diff --git a/app/src/main/java/net/bible/android/control/report/ErrorReportControl.kt b/app/src/main/java/net/bible/android/control/report/ErrorReportControl.kt index 8d9ad59ea0..52ebd73edd 100644 --- a/app/src/main/java/net/bible/android/control/report/ErrorReportControl.kt +++ b/app/src/main/java/net/bible/android/control/report/ErrorReportControl.kt @@ -39,6 +39,7 @@ import net.bible.android.activity.R import net.bible.android.control.backup.BackupControl import net.bible.android.view.activity.base.ActivityBase import net.bible.android.view.activity.base.CurrentActivityHolder +import net.bible.android.view.activity.page.application import net.bible.android.view.util.Hourglass import net.bible.service.common.CommonUtils import net.bible.service.common.CommonUtils.applicationVersionName @@ -166,11 +167,39 @@ object BugReport { return returnedBitmap } + private val logDir get() = File(application.filesDir, "/log") + + fun saveLogcat() { + Log.i(TAG, "Trying to save logcat") + val f = File(logDir, "logcat.txt.gz") + val log = StringBuilder() + try { + val process = Runtime.getRuntime().exec("logcat -d -v threadtime") + val bufferedReader = BufferedReader( + InputStreamReader(process.inputStream)) + + var line = bufferedReader.readLine() + while (line != null) { + log.append(line + '\n'); + line = bufferedReader.readLine() + } + } catch (_: IOException) {} + + logDir.mkdirs() + + val fOut = FileOutputStream(f) + val osw = GZIPOutputStream(fOut) + + osw.write(log.toString().toByteArray()); + osw.flush() + osw.close() + } + fun saveScreenshot() { + Log.i(TAG, "Trying to save screenshot") val activity = CurrentActivityHolder.currentActivity?: return - val dir = File(activity.filesDir, "/log") - dir.mkdirs() - val screenshotFile = File(dir, SCREENSHOT_FILE) + logDir.mkdirs() + val screenshotFile = File(logDir, SCREENSHOT_FILE) try { val screenShot = getScreenShot(activity) ?: return val screenshotOutputStream = FileOutputStream(screenshotFile) @@ -185,25 +214,24 @@ object BugReport { } } - private fun getBugReportMessage(context: Context, exception: Throwable?): String = - context.run { - val bigHeading = getString(R.string.report_bug_big_heading) - val heading1 = getString(R.string.report_bug_heading1) - val heading2 = getString(R.string.report_bug_heading2) - val heading3 = getString(R.string.report_bug_heading_3) - val heading4 = getString(R.string.report_bug_heading_4) - val instruction1 = getString(R.string.report_bug_instructions1) - val instruction2 = getString(R.string.report_bug_instructions2) - val instruction3 = getString(R.string.report_bug_instructions3) - val line1 = getString(R.string.report_bug_line_1) - val line2 = getString(R.string.report_bug_line_2) - val line3 = getString(R.string.report_bug_line_3) - val line4 = getString(R.string.report_bug_line_4) - val line5 = getString(R.string.bug_report_attachment_line_1) - val logcat = getString(R.string.bug_report_logcat) - val screenShot = getString(R.string.bug_report_screenshot) + private fun getBugReportMessage(context: Context, exception: Throwable?): String = context.run { + val bigHeading = getString(R.string.report_bug_big_heading) + val heading1 = getString(R.string.report_bug_heading1) + val heading2 = getString(R.string.report_bug_heading2) + val heading3 = getString(R.string.report_bug_heading_3) + val heading4 = getString(R.string.report_bug_heading_4) + val instruction1 = getString(R.string.report_bug_instructions1) + val instruction2 = getString(R.string.report_bug_instructions2) + val instruction3 = getString(R.string.report_bug_instructions3) + val line1 = getString(R.string.report_bug_line_1) + val line2 = getString(R.string.report_bug_line_2) + val line3 = getString(R.string.report_bug_line_3) + val line4 = getString(R.string.report_bug_line_4) + val line5 = getString(R.string.bug_report_attachment_line_1) + val logcat = getString(R.string.bug_report_logcat) + val screenShot = getString(R.string.bug_report_screenshot) - "\n\n" + + "\n\n" + """ --- $bigHeading --- @@ -226,41 +254,20 @@ object BugReport { $heading4 """.trimIndent() + - createErrorText(exception) - } + createErrorText(exception) + } suspend fun reportBug(context_: ActivityBase? = null, exception: Throwable? = null, useSaved: Boolean = false, source: String) { val context = context_ ?: CurrentActivityHolder.currentActivity!! - val dir = File(context.filesDir, "/log") - val f = File(dir, "logcat.txt.gz") - val screenshotFile = File(dir, SCREENSHOT_FILE) + val screenshotFile = File(logDir, SCREENSHOT_FILE) + val logcatFile = File(logDir, "logcat.txt.gz") val hourglass = Hourglass(context) hourglass.show() withContext(Dispatchers.IO) { - val log = StringBuilder() - try { - val process = Runtime.getRuntime().exec("logcat -d -v threadtime") - val bufferedReader = BufferedReader( - InputStreamReader(process.inputStream)) - - var line = bufferedReader.readLine() - while (line != null) { - log.append(line + '\n'); - line = bufferedReader.readLine() - } - } catch (_: IOException) {} - - dir.mkdirs() - - val fOut = FileOutputStream(f) - val osw = GZIPOutputStream(fOut) - - osw.write(log.toString().toByteArray()); - osw.flush() - osw.close() if(!useSaved) { delay(1000) + saveLogcat() saveScreenshot() } } @@ -271,7 +278,7 @@ object BugReport { val subject = context.getString(R.string.report_bug_email_subject_3, source, CommonUtils.applicationNameMedium, getSubject(exception)) val message = getBugReportMessage(context, exception) - val uris = ArrayList(listOf(f, screenshotFile).filter { it.canRead() }.map { + val uris = ArrayList(listOf(logcatFile, screenshotFile).filter { it.canRead() }.map { FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", it) }) val email = Intent(Intent.ACTION_SEND_MULTIPLE).apply {