Optimization logcat after change quickie-foss (#4171)

After changed quickie-foss, scanning cause lot of log. throttle log com.google.zxing.NotFoundException.
This commit is contained in:
886963226
2024-12-22 10:21:55 +08:00
committed by GitHub
parent eb8562e6b0
commit 41bc064083

View File

@@ -19,19 +19,42 @@ import kotlinx.coroutines.withContext
import java.io.IOException
class LogcatActivity : BaseActivity() {
private val binding by lazy {
ActivityLogcatBinding.inflate(layoutInflater)
}
private val binding by lazy { ActivityLogcatBinding.inflate(layoutInflater) }
private val throttleManager = ThrottleManager()
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
@@ -44,20 +67,23 @@ class LogcatActivity : BaseActivity() {
process.waitFor()
}
}
val lst = linkedSetOf(
"logcat", "-d", "-v", "time", "-s",
"GoLog,tun2socks,$ANG_PACKAGE,AndroidRuntime,System.err"
)
val process = withContext(Dispatchers.IO) {
Runtime.getRuntime().exec(lst.toTypedArray())
}
val allText = process.inputStream.bufferedReader().use { it.readText() }
val allLogs = process.inputStream.bufferedReader().use { it.readLines() }
val filteredLogs = processLogs(allLogs)
withContext(Dispatchers.Main) {
binding.tvLogcat.text = allText
binding.tvLogcat.movementMethod = ScrollingMovementMethod()
binding.pbWaiting.visibility = View.GONE
Handler(Looper.getMainLooper()).post { binding.svLogcat.fullScroll(View.FOCUS_DOWN) }
updateLogDisplay(filteredLogs)
}
} catch (e: IOException) {
withContext(Dispatchers.Main) {
binding.pbWaiting.visibility = View.GONE
@@ -68,6 +94,36 @@ class LogcatActivity : BaseActivity() {
}
}
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)
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_logcat, menu)
@@ -80,12 +136,11 @@ class LogcatActivity : BaseActivity() {
toast(R.string.toast_success)
true
}
R.id.clear_all -> {
throttleManager.reset("zxing.NotFoundException")
logcat(true)
true
}
else -> super.onOptionsItemSelected(item)
}
}