Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions app/src/main/java/com/weenas/castbay/util/LogReport.kt
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,30 @@ object LogReport {

/** This process's lines in logcat, oldest first; empty if logcat can't be read. */
private fun logcat(): List<String> = runCatching {
val process = ProcessBuilder(
"logcat", "-d", "-v", "threadtime", "-t", LOGCAT_LINES.toString(), "--pid", Process.myPid().toString()
).redirectErrorStream(true).start()
val pid = Process.myPid()
// logcat's --pid is Android 7+: Android 6's printed its usage instead. There, every
// process's recent lines are read and this one's kept.
val byPid = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N
val command = mutableListOf("logcat", "-d", "-v", "threadtime", "-t", (if (byPid) LOGCAT_LINES else LOGCAT_LINES * 5).toString())
if (byPid) command += listOf("--pid", pid.toString())
val process = ProcessBuilder(command).redirectErrorStream(true).start()
val lines = process.inputStream.bufferedReader().use { it.readLines() }
process.destroy()
lines
processLines(lines, pid)
}.getOrDefault(emptyList())

/**
* The lines of [pid] in threadtime format ("10-04 15:21:53.785 6213 6256 I Tag: …"); anything
* else, such as logcat's usage or another process's lines, is left out.
*/
internal fun processLines(lines: List<String>, pid: Int): List<String> {
val own = pid.toString()
return lines.filter { line ->
val fields = line.trim().split(Regex("\\s+"), limit = 4)
fields.size == 4 && fields[0].length == 5 && fields[0][2] == '-' && fields[2] == own
}
}

// UxPlay logs the PIN a sender must enter, and the like.
private val secret = Regex("""(?i)\b(pin|password|passwd)(\s*[=:]\s*)"[^"]*"""")
private val personal = Regex("${Log.PERSONAL_START}[^${Log.PERSONAL_END}\\n]*${Log.PERSONAL_END}")
Expand Down
17 changes: 17 additions & 0 deletions app/src/test/java/com/weenas/castbay/util/LogReportTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,21 @@ class LogReportTest {
assertEquals("too many reports", LogReport.reason(LogReport.TooManyReports()))
assertEquals("address not found (DNS)", LogReport.reason(java.net.UnknownHostException("castbay.weenas.com")))
}

@Test
fun keepsOnlyThisProcessesLogcatLines() {
val lines = listOf(
"--------- beginning of main",
"10-04 15:21:53.785 6213 6256 I CastBay: mine",
"10-04 15:21:53.790 2705 3117 D MDnsDS : someone else's",
"10-04 15:21:54.000 6213 6213 W AirPlay: mine too",
"Unrecognized Option -",
"Usage: logcat [options] [filterspecs]",
)
assertEquals(
listOf("10-04 15:21:53.785 6213 6256 I CastBay: mine", "10-04 15:21:54.000 6213 6213 W AirPlay: mine too"),
LogReport.processLines(lines, 6213)
)
assertEquals(emptyList<String>(), LogReport.processLines(listOf("Unrecognized Option -", "Usage: logcat"), 6213))
}
}
Loading