Skip to content

Commit

Permalink
Fix the way log buffer names are parsed.
Browse files Browse the repository at this point in the history
  • Loading branch information
darshanparajuli committed Mar 7, 2021
1 parent 4cada79 commit ec0ae8f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 54 deletions.
14 changes: 13 additions & 1 deletion app/src/main/java/com/dp/logcatapp/services/LogcatService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down Expand Up @@ -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()
}
}
4 changes: 2 additions & 2 deletions logcat/src/main/java/com/dp/logcat/CommandUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import kotlin.concurrent.thread
object CommandUtils {

fun runCmd(
vararg cmd: String,
cmd: List<String>,
stdoutList: MutableList<String>? = null,
stderrList: MutableList<String>? = null,
redirectStderr: Boolean = false
): Int {
val processBuilder = ProcessBuilder(*cmd)
val processBuilder = ProcessBuilder(cmd)
.redirectErrorStream(redirectStderr)
var process: Process? = null
try {
Expand Down
95 changes: 44 additions & 51 deletions logcat/src/main/java/com/dp/logcat/Logcat.kt
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ class Logcat(initialCapacity: Int = INITIAL_LOG_CAPACITY) : Closeable {
val result = mutableSetOf<String>()

val stdoutList = mutableListOf<String>()
CommandUtils.runCmd(cmd = arrayOf("logcat", "-g"), stdoutList = stdoutList)
CommandUtils.runCmd(cmd = listOf("logcat", "-g"), stdoutList = stdoutList)

for (s in stdoutList) {
val colonIndex = s.indexOf(":")
Expand All @@ -567,68 +567,61 @@ class Logcat(initialCapacity: Int = INITIAL_LOG_CAPACITY) : Closeable {
return result
}

private fun parseBufferNames(
s: String,
names: MutableSet<String>
): 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<String> {
val stdoutList = mutableListOf<String>()
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<String>()
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<String> {
val result = mutableSetOf<String>()
return buffers.toTypedArray().sortedArray()
}

val stdoutList = mutableListOf<String>()
CommandUtils.runCmd(
cmd = arrayOf("logcat", "-h"),
stdoutList = stdoutList, redirectStderr = true
)
private fun getBufferHelpText(stdout: List<String>): List<String> {
val startPattern = "^\\s+-b,?.*<buffer>\\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()
}
}
}

0 comments on commit ec0ae8f

Please sign in to comment.