Skip to content

Commit 6737965

Browse files
committed
refactor: replace logcat DSL with subprocess
1 parent 136e3b7 commit 6737965

File tree

3 files changed

+22
-52
lines changed

3 files changed

+22
-52
lines changed

app/src/main/java/com/osfans/trime/daemon/RimeDaemon.kt

+5-11
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import com.osfans.trime.core.whenReady
2020
import com.osfans.trime.ui.main.LogActivity
2121
import com.osfans.trime.util.appContext
2222
import com.osfans.trime.util.createNotificationChannel
23-
import com.osfans.trime.util.logcat
23+
import com.osfans.trime.util.readText
24+
import com.osfans.trime.util.subprocess
2425
import com.osfans.trime.util.toast
2526
import kotlinx.coroutines.CoroutineScope
2627
import kotlinx.coroutines.Dispatchers
@@ -171,9 +172,7 @@ object RimeDaemon {
171172
if (it is RimeMessage.DeployMessage) {
172173
when (it.data) {
173174
RimeMessage.DeployMessage.State.Start -> {
174-
withContext(Dispatchers.IO) {
175-
logcat { clear() }
176-
}
175+
withContext(Dispatchers.IO) { subprocess("logcat", "--clear") }
177176
}
178177
RimeMessage.DeployMessage.State.Success -> {
179178
ContextCompat.getMainExecutor(appContext).execute {
@@ -185,13 +184,8 @@ object RimeDaemon {
185184
Intent(appContext, LogActivity::class.java).apply {
186185
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
187186
val log =
188-
withContext(Dispatchers.IO) {
189-
logcat {
190-
format("brief")
191-
filterspec("rime.trime", "W")
192-
dump()
193-
}.inputStream.bufferedReader().readText()
194-
}
187+
subprocess("logcat", "-v", "brief", "-s", "rime.trime:W", "-d")
188+
.readText()
195189
putExtra(LogActivity.FROM_DEPLOY, true)
196190
putExtra(LogActivity.DEPLOY_FAILURE_TRACE, log)
197191
}

app/src/main/java/com/osfans/trime/util/Logcat.kt

+4-41
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ class Logcat(
3939
fun getLogAsync(): Deferred<Result<List<String>>> =
4040
async {
4141
runCatching {
42-
logcat {
43-
pid?.let { pid(it) }
44-
dump()
45-
}.inputStream.bufferedReader().readLines()
42+
subprocess("logcat", pid?.let { "--pid=$it" } ?: "", "-d").readLines()
4643
}
4744
}
4845

@@ -51,7 +48,7 @@ class Logcat(
5148
*/
5249
fun clearLog(): Job =
5350
launch {
54-
runCatching { logcat { clear() } }
51+
runCatching { subprocess("logcat", "--clear") }
5552
}
5653

5754
/**
@@ -63,10 +60,8 @@ class Logcat(
6360
} else {
6461
launch {
6562
runCatching {
66-
logcat {
67-
pid?.let { pid(it) }
68-
format("time")
69-
}.also { process = it }
63+
subprocess("logcat", pid?.let { "--pid=$it" } ?: "", "-v", "time")
64+
.also { process = it }
7065
.asFlow()
7166
.collect { flow.emit(it) }
7267
}
@@ -85,35 +80,3 @@ class Logcat(
8580
val default by lazy { Logcat() }
8681
}
8782
}
88-
89-
// DSL
90-
inline fun logcat(builderAction: LogcatCommandBuilder.() -> Unit): java.lang.Process =
91-
LogcatCommandBuilder().apply(builderAction).toProcess()
92-
93-
class LogcatCommandBuilder {
94-
private val cmdList = arrayListOf("logcat")
95-
96-
fun clear() = apply { cmdList.add("--clear") }
97-
98-
fun dump() = apply { cmdList.add("-d") }
99-
100-
fun pid(pid: Int) = apply { cmdList.add("--pid=$pid") }
101-
102-
fun format(format: String) = apply { cmdList.add("--format=$format") }
103-
104-
fun filterspec(
105-
tag: String,
106-
priority: String,
107-
) = apply {
108-
cmdList.add("-s")
109-
cmdList.add("$tag:$priority")
110-
}
111-
112-
fun filterspec(spec: String) =
113-
apply {
114-
cmdList.add("-s")
115-
cmdList.add(spec)
116-
}
117-
118-
fun toProcess(): java.lang.Process = Runtime.getRuntime().exec(cmdList.toTypedArray())
119-
}

app/src/main/java/com/osfans/trime/util/Flow.kt renamed to app/src/main/java/com/osfans/trime/util/Process.kt

+13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import kotlinx.coroutines.flow.Flow
1010
import kotlinx.coroutines.flow.asFlow
1111
import kotlinx.coroutines.flow.cancellable
1212
import kotlinx.coroutines.flow.flowOn
13+
import kotlinx.coroutines.withContext
14+
15+
fun subprocess(vararg commands: String): Process = Runtime.getRuntime().exec(commands)
1316

1417
fun Process.asFlow(): Flow<String> =
1518
inputStream
@@ -18,3 +21,13 @@ fun Process.asFlow(): Flow<String> =
1821
.asFlow()
1922
.flowOn(Dispatchers.IO)
2023
.cancellable()
24+
25+
suspend fun Process.readText() =
26+
withContext(Dispatchers.IO) {
27+
inputStream.bufferedReader().readText()
28+
}
29+
30+
suspend fun Process.readLines() =
31+
withContext(Dispatchers.IO) {
32+
inputStream.bufferedReader().readLines()
33+
}

0 commit comments

Comments
 (0)