Skip to content

Commit

Permalink
live logs!
Browse files Browse the repository at this point in the history
  • Loading branch information
SaeedDev94 committed Feb 23, 2024
1 parent 47b7d9d commit 1399840
Showing 1 changed file with 51 additions and 23 deletions.
74 changes: 51 additions & 23 deletions app/src/main/java/io/github/saeeddev94/xray/LogsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.text.method.ScrollingMovementMethod
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.ScrollView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import io.github.saeeddev94.xray.databinding.ActivityLogsBinding
import java.io.BufferedReader
import java.io.InputStreamReader

class LogsActivity : AppCompatActivity() {

private lateinit var binding: ActivityLogsBinding
private var loggingThread: Thread? = null
private var loggingProcess: Process? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -25,7 +26,16 @@ class LogsActivity : AppCompatActivity() {
setContentView(binding.root)
setSupportActionBar(binding.toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
resolve()
}

override fun onStart() {
super.onStart()
startLoop()
}

override fun onStop() {
super.onStop()
stopLoop()
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
Expand All @@ -35,36 +45,54 @@ class LogsActivity : AppCompatActivity() {

override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.deleteLogs -> resolve(true)
R.id.deleteLogs -> {
stopLoop()
flush()
startLoop()
}
R.id.copyLogs -> copyToClipboard(binding.logsTextView.text.toString())
else -> finish()
}
return true
}

private fun resolve(flush: Boolean = false) {
Thread {
if (flush) flush()
val logs = logs()
runOnUiThread {
binding.logsTextView.text = logs
binding.logsTextView.movementMethod = ScrollingMovementMethod()
Handler(Looper.getMainLooper()).post { binding.logsScrollView.fullScroll(View.FOCUS_DOWN) }
private fun startLoop() {
loggingThread = Thread {
try {
loggingProcess = ProcessBuilder("logcat", "-v", "raw", "-s", "GoLog,${BuildConfig.APPLICATION_ID}").start()
val reader = BufferedReader(InputStreamReader(loggingProcess!!.inputStream))
var line = ""
val logStringBuilder = StringBuilder()
while (!Thread.currentThread().isInterrupted && reader.readLine().also { line = it } != null) {
logStringBuilder.append(line).append("\n")
runOnUiThread {
binding.logsTextView.text = logStringBuilder.toString()
binding.logsScrollView.post {
binding.logsScrollView.fullScroll(ScrollView.FOCUS_DOWN)
}
}
}
} catch (error: Exception) {
error.printStackTrace()
} finally {
loggingProcess?.destroy()
}
}.start()
}
loggingThread?.start()
}

private fun flush() {
val command = listOf("logcat", "-c")
val process = ProcessBuilder(command).start()
process.waitFor()
private fun stopLoop() {
loggingThread?.interrupt()
loggingProcess?.destroy()
loggingThread = null
loggingProcess = null
}

private fun logs(): String {
val command = listOf("logcat", "-d", "-s", "GoLog,${BuildConfig.APPLICATION_ID}")
private fun flush() {
val command = listOf("logcat", "-c")
val process = ProcessBuilder(command).start()
process.waitFor()
return process.inputStream.bufferedReader().use { it.readText() }
binding.logsTextView.text = ""
}

private fun copyToClipboard(text: String) {
Expand All @@ -74,7 +102,7 @@ class LogsActivity : AppCompatActivity() {
clipboardManager.setPrimaryClip(clipData)
Toast.makeText(applicationContext, "Logs copied", Toast.LENGTH_SHORT).show()
} catch (error: Exception) {
// ignore
error.printStackTrace()
}
}

Expand Down

0 comments on commit 1399840

Please sign in to comment.