From 41bc06408361c6d28566e07bb911635911205b39 Mon Sep 17 00:00:00 2001 From: 886963226 Date: Sun, 22 Dec 2024 10:21:55 +0800 Subject: [PATCH] Optimization logcat after change quickie-foss (#4171) After changed quickie-foss, scanning cause lot of log. throttle log com.google.zxing.NotFoundException. --- .../java/com/v2ray/ang/ui/LogcatActivity.kt | 79 ++++++++++++++++--- 1 file changed, 67 insertions(+), 12 deletions(-) diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/LogcatActivity.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/LogcatActivity.kt index 7e424efc..83f45d77 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/LogcatActivity.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/LogcatActivity.kt @@ -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() + + 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): List { + val processedLogs = mutableListOf() + 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) { + 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) } }