Fix logcat

This commit is contained in:
2dust
2025-01-07 14:03:26 +08:00
parent 1c7042463d
commit c37f09bfcd

View File

@@ -19,109 +19,57 @@ import kotlinx.coroutines.withContext
import java.io.IOException
class LogcatActivity : BaseActivity() {
private val binding by lazy { ActivityLogcatBinding.inflate(layoutInflater) }
private val throttleManager = ThrottleManager()
private val binding by lazy {
ActivityLogcatBinding.inflate(layoutInflater)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
title = getString(R.string.title_logcat)
logcat(false)
}
class ThrottleManager {
private val throttleMap = mutableMapOf<String, Long>()
companion object {
private const val THROTTLE_DURATION = 1000L
}
@Synchronized
fun shouldProcess(key: String): Boolean {
val currentTime = System.currentTimeMillis()
val lastProcessTime = throttleMap[key] ?: 0L
return if (currentTime - lastProcessTime > THROTTLE_DURATION) {
throttleMap[key] = currentTime
true
} else {
false
}
}
@Synchronized
fun reset(key: String) {
throttleMap.remove(key)
}
}
private fun logcat(shouldFlushLog: Boolean) {
binding.pbWaiting.visibility = View.VISIBLE
lifecycleScope.launch(Dispatchers.Default) {
try {
try {
binding.pbWaiting.visibility = View.VISIBLE
lifecycleScope.launch(Dispatchers.Default) {
if (shouldFlushLog) {
val lst = linkedSetOf("logcat", "-c")
val lst = LinkedHashSet<String>()
lst.add("logcat")
lst.add("-c")
withContext(Dispatchers.IO) {
val process = Runtime.getRuntime().exec(lst.toTypedArray())
process.waitFor()
}
}
val lst = linkedSetOf(
"logcat", "-d", "-v", "time", "-s",
"GoLog,tun2socks,$ANG_PACKAGE,AndroidRuntime,System.err"
)
val lst = LinkedHashSet<String>()
lst.add("logcat")
lst.add("-d")
lst.add("-v")
lst.add("time")
lst.add("-s")
lst.add("GoLog,tun2socks,${ANG_PACKAGE},AndroidRuntime,System.err")
val process = withContext(Dispatchers.IO) {
Runtime.getRuntime().exec(lst.toTypedArray())
}
val allLogs = process.inputStream.bufferedReader().use { it.readLines() }
val filteredLogs = processLogs(allLogs)
withContext(Dispatchers.Main) {
updateLogDisplay(filteredLogs)
}
} catch (e: IOException) {
withContext(Dispatchers.Main) {
// val bufferedReader = BufferedReader(
// InputStreamReader(process.inputStream))
// val allText = bufferedReader.use(BufferedReader::readText)
val allText = process.inputStream.bufferedReader().use { it.readText() }
launch(Dispatchers.Main) {
binding.tvLogcat.text = allText
binding.tvLogcat.movementMethod = ScrollingMovementMethod()
binding.pbWaiting.visibility = View.GONE
toast(R.string.toast_failure)
Handler(Looper.getMainLooper()).post { binding.svLogcat.fullScroll(View.FOCUS_DOWN) }
}
e.printStackTrace()
}
}
}
private fun processLogs(logs: List<String>): List<String> {
val processedLogs = mutableListOf<String>()
var isNotMatch = false
for (line in logs) {
when {
line.contains("zxing.NotFoundException", ignoreCase = true) -> {
if (!isNotMatch) {
if (throttleManager.shouldProcess("NotFoundException")) {
processedLogs.add(line)
isNotMatch = true
}
}
}
else -> processedLogs.add(line)
}
}
return processedLogs.take(500)
}
private fun updateLogDisplay(logs: List<String>) {
binding.tvLogcat.text = logs.joinToString("\n")
binding.tvLogcat.movementMethod = ScrollingMovementMethod()
binding.pbWaiting.visibility = View.GONE
Handler(Looper.getMainLooper()).post {
binding.svLogcat.fullScroll(View.FOCUS_DOWN)
} catch (e: IOException) {
e.printStackTrace()
}
}
@@ -136,11 +84,12 @@ class LogcatActivity : BaseActivity() {
toast(R.string.toast_success)
true
}
R.id.clear_all -> {
throttleManager.reset("zxing.NotFoundException")
logcat(true)
true
}
else -> super.onOptionsItemSelected(item)
}
}