From ec0ae8f9c7f4eb74ec58a4797bdfe48e2eaafd4e Mon Sep 17 00:00:00 2001 From: Darshan Parajuli Date: Sun, 7 Mar 2021 11:51:35 -0800 Subject: [PATCH] Fix the way log buffer names are parsed. --- .../dp/logcatapp/services/LogcatService.kt | 14 ++- .../main/java/com/dp/logcat/CommandUtils.kt | 4 +- logcat/src/main/java/com/dp/logcat/Logcat.kt | 95 +++++++++---------- 3 files changed, 59 insertions(+), 54 deletions(-) diff --git a/app/src/main/java/com/dp/logcatapp/services/LogcatService.kt b/app/src/main/java/com/dp/logcatapp/services/LogcatService.kt index 2ceefac..549cdf3 100644 --- a/app/src/main/java/com/dp/logcatapp/services/LogcatService.kt +++ b/app/src/main/java/com/dp/logcatapp/services/LogcatService.kt @@ -19,6 +19,7 @@ import com.dp.logcatapp.activities.MainActivity import com.dp.logcatapp.util.PreferenceKeys import com.dp.logcatapp.util.getDefaultSharedPreferences import com.dp.logcatapp.util.showToast +import java.util.Locale class LogcatService : BaseService() { @@ -221,7 +222,18 @@ class LogcatService : BaseService() { logcat.setPollInterval(pollInterval) val buffers = Logcat.AVAILABLE_BUFFERS - logcat.logcatBuffers = bufferValues.map { e -> buffers[e.toInt()].toLowerCase() }.toSet() + logcat.logcatBuffers = bufferValues.mapNotNull { e -> + buffers.getOrNull(e.toInt()) + ?.toLowerCase(Locale.getDefault()) + }.toSet().ifEmpty { + sharedPreferences.edit { + putStringSet( + PreferenceKeys.Logcat.KEY_BUFFERS, + PreferenceKeys.Logcat.Default.BUFFERS + ) + } + Logcat.DEFAULT_BUFFERS + } logcat.start() } } \ No newline at end of file diff --git a/logcat/src/main/java/com/dp/logcat/CommandUtils.kt b/logcat/src/main/java/com/dp/logcat/CommandUtils.kt index 8ca2995..c18ee7a 100644 --- a/logcat/src/main/java/com/dp/logcat/CommandUtils.kt +++ b/logcat/src/main/java/com/dp/logcat/CommandUtils.kt @@ -7,12 +7,12 @@ import kotlin.concurrent.thread object CommandUtils { fun runCmd( - vararg cmd: String, + cmd: List, stdoutList: MutableList? = null, stderrList: MutableList? = null, redirectStderr: Boolean = false ): Int { - val processBuilder = ProcessBuilder(*cmd) + val processBuilder = ProcessBuilder(cmd) .redirectErrorStream(redirectStderr) var process: Process? = null try { diff --git a/logcat/src/main/java/com/dp/logcat/Logcat.kt b/logcat/src/main/java/com/dp/logcat/Logcat.kt index e669705..b6be0a4 100644 --- a/logcat/src/main/java/com/dp/logcat/Logcat.kt +++ b/logcat/src/main/java/com/dp/logcat/Logcat.kt @@ -547,7 +547,7 @@ class Logcat(initialCapacity: Int = INITIAL_LOG_CAPACITY) : Closeable { val result = mutableSetOf() val stdoutList = mutableListOf() - CommandUtils.runCmd(cmd = arrayOf("logcat", "-g"), stdoutList = stdoutList) + CommandUtils.runCmd(cmd = listOf("logcat", "-g"), stdoutList = stdoutList) for (s in stdoutList) { val colonIndex = s.indexOf(":") @@ -567,68 +567,61 @@ class Logcat(initialCapacity: Int = INITIAL_LOG_CAPACITY) : Closeable { return result } - private fun parseBufferNames( - s: String, - names: MutableSet - ): Boolean { - var startIndex = s.indexOf("'") - if (startIndex == -1) { - return true - } - - var nextIndex = s.indexOf("'", startIndex + 1) - if (nextIndex == -1) { - return true - } + private fun getAvailableBuffers(): Array { + val stdoutList = mutableListOf() + CommandUtils.runCmd( + cmd = listOf("logcat", "-h"), + stdoutList = stdoutList, redirectStderr = true + ) - val periodIndex = s.indexOf(".") - while (periodIndex == -1 || (startIndex < periodIndex && nextIndex < periodIndex)) { - val name = s.substring(startIndex + 1, nextIndex) - if (name != "all" && name != "default") { - names += name - } + val helpText = getBufferHelpText(stdoutList) - startIndex = s.indexOf("'", nextIndex + 1) - if (startIndex == -1) { - break + val buffers = mutableListOf() + if (helpText.firstOrNull()?.run { + contains("request alternate ring buffer", ignoreCase = true) && + endsWith(":") + } == true + ) { + if (helpText.size >= 2) { + buffers += helpText[1].split(" ") } + } - nextIndex = s.indexOf("'", startIndex + 1) - if (nextIndex == -1) { - break + val pattern = "'[a-z]+'".toRegex() + for (s in helpText) { + pattern.findAll(s).forEach { match -> + match.value.let { + buffers += it.substring(1, it.length - 1) + } } } - return periodIndex != -1 - } + buffers -= "default" + buffers -= "all" - private fun getAvailableBuffers(): Array { - val result = mutableSetOf() + return buffers.toTypedArray().sortedArray() + } - val stdoutList = mutableListOf() - CommandUtils.runCmd( - cmd = arrayOf("logcat", "-h"), - stdoutList = stdoutList, redirectStderr = true - ) + private fun getBufferHelpText(stdout: List): List { + val startPattern = "^\\s+-b,?.*\\s+".toRegex() + val start = stdout.indexOfFirst { + startPattern.find(it)?.range?.start == 0 + } + if (start == -1) { + return emptyList() + } - var bFound = false - for (s in stdoutList) { - val trimmed = s.trim() - if (bFound) { - if (parseBufferNames(trimmed, result)) { - break - } - } else { - if (trimmed.startsWith("-b")) { - bFound = true - if (parseBufferNames(trimmed, result)) { - break - } - } - } + val endPattern = "^\\s+-[a-zA-Z],?\\s+".toRegex() + var end = stdout.subList(start + 1, stdout.size).indexOfFirst { + endPattern.find(it)?.range?.start == 0 + } + if (end == -1) { + end = stdout.size + } else { + end += start + 1 } - return result.toTypedArray().sortedArray() + return stdout.subList(start, end).map { it.trim() }.toList() } } }