Skip to content

Commit

Permalink
Fix #2385 (Logcat should be saved immediately after crash)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuomas2 committed Nov 24, 2022
1 parent 66e7cb6 commit a759638
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 48 deletions.
1 change: 1 addition & 0 deletions app/src/main/java/net/bible/android/BibleApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 ---
Expand All @@ -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()
}
}
Expand All @@ -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 {
Expand Down

0 comments on commit a759638

Please sign in to comment.