Sync debug value to Logger

This commit is contained in:
Mygod
2020-07-10 04:33:58 +08:00
parent 78f32cde3c
commit 14beddd0dd
2 changed files with 20 additions and 15 deletions

View File

@@ -12,6 +12,7 @@ import androidx.collection.LongSparseArray
import androidx.collection.set import androidx.collection.set
import androidx.collection.valueIterator import androidx.collection.valueIterator
import eu.chainfire.librootjava.AppProcess import eu.chainfire.librootjava.AppProcess
import eu.chainfire.librootjava.Logger
import eu.chainfire.librootjava.RootJava import eu.chainfire.librootjava.RootJava
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
@@ -152,7 +153,7 @@ class RootServer @JvmOverloads constructor(private val warnLogger: (String) -> U
writer.flush() writer.flush()
val reader = process.inputStream.bufferedReader() val reader = process.inputStream.bufferedReader()
reader.lookForToken(token1) reader.lookForToken(token1)
if (DEBUG) Log.d(TAG, "Root shell initialized") if (isDebugEnabled) Log.d(TAG, "Root shell initialized")
reader to writer reader to writer
} catch (e: Exception) { } catch (e: Exception) {
throw NoShellException(e) throw NoShellException(e)
@@ -189,7 +190,7 @@ class RootServer @JvmOverloads constructor(private val warnLogger: (String) -> U
output = writer output = writer
require(!active) require(!active)
active = true active = true
if (DEBUG) Log.d(TAG, "Root server initialized") if (isDebugEnabled) Log.d(TAG, "Root server initialized")
} }
private fun callbackSpin() { private fun callbackSpin() {
@@ -209,7 +210,7 @@ class RootServer @JvmOverloads constructor(private val warnLogger: (String) -> U
} }
} }
} }
if (DEBUG) Log.d(TAG, "Received callback #$index: $result") if (isDebugEnabled) Log.d(TAG, "Received callback #$index: $result")
callback(input, result) callback(input, result)
} }
} }
@@ -244,7 +245,7 @@ class RootServer @JvmOverloads constructor(private val warnLogger: (String) -> U
process.destroy() process.destroy()
throw e throw e
} finally { } finally {
if (DEBUG) Log.d(TAG, "Waiting for exit") if (isDebugEnabled) Log.d(TAG, "Waiting for exit")
errorReader.await() errorReader.await()
process.waitFor() process.waitFor()
withContext(NonCancellable) { closeInternal(true) } withContext(NonCancellable) { closeInternal(true) }
@@ -267,7 +268,7 @@ class RootServer @JvmOverloads constructor(private val warnLogger: (String) -> U
private fun sendLocked(command: Parcelable) { private fun sendLocked(command: Parcelable) {
output.writeParcelable(command) output.writeParcelable(command)
output.flush() output.flush()
if (DEBUG) Log.d(TAG, "Sent #$counter: $command") if (isDebugEnabled) Log.d(TAG, "Sent #$counter: $command")
counter++ counter++
} }
@@ -327,7 +328,7 @@ class RootServer @JvmOverloads constructor(private val warnLogger: (String) -> U
private suspend fun closeInternal(fromWorker: Boolean = false) = mutex.withLock { private suspend fun closeInternal(fromWorker: Boolean = false) = mutex.withLock {
if (active) { if (active) {
active = false active = false
if (DEBUG) Log.d(TAG, if (fromWorker) "Shutting down from worker" else "Shutting down from client") if (isDebugEnabled) Log.d(TAG, if (fromWorker) "Shutting down from worker" else "Shutting down from client")
try { try {
sendLocked(Shutdown()) sendLocked(Shutdown())
output.close() output.close()
@@ -335,7 +336,7 @@ class RootServer @JvmOverloads constructor(private val warnLogger: (String) -> U
} catch (e: IOException) { } catch (e: IOException) {
Log.i(TAG, "send Shutdown failed", e) Log.i(TAG, "send Shutdown failed", e)
} }
if (DEBUG) Log.d(TAG, "Client closed") if (isDebugEnabled) Log.d(TAG, "Client closed")
} }
if (fromWorker) { if (fromWorker) {
for (callback in callbackLookup.valueIterator()) callback.cancel() for (callback in callbackLookup.valueIterator()) callback.cancel()
@@ -354,8 +355,12 @@ class RootServer @JvmOverloads constructor(private val warnLogger: (String) -> U
/** /**
* If set to true, debug information will be printed to logcat. * If set to true, debug information will be printed to logcat.
*/ */
@JvmField @JvmStatic
var DEBUG = false var isDebugEnabled = false
set(value) {
field = value
Logger.setDebugLogging(value)
}
private const val TAG = "RootServer" private const val TAG = "RootServer"
private const val SUCCESS = 0 private const val SUCCESS = 0
@@ -448,7 +453,7 @@ class RootServer @JvmOverloads constructor(private val warnLogger: (String) -> U
// thread safety: usage of input should be in main thread // thread safety: usage of input should be in main thread
val input = DataInputStream(System.`in`.buffered()) val input = DataInputStream(System.`in`.buffered())
var counter = 0L var counter = 0L
if (DEBUG) Log.d(TAG, "Server entering main loop") if (isDebugEnabled) Log.d(TAG, "Server entering main loop")
loop@ while (true) { loop@ while (true) {
val command = try { val command = try {
input.readParcelable<Parcelable>(RootServer::class.java.classLoader) input.readParcelable<Parcelable>(RootServer::class.java.classLoader)
@@ -456,7 +461,7 @@ class RootServer @JvmOverloads constructor(private val warnLogger: (String) -> U
break break
} }
val callback = counter val callback = counter
if (DEBUG) Log.d(TAG, "Received #$callback: $command") if (isDebugEnabled) Log.d(TAG, "Received #$callback: $command")
when (command) { when (command) {
is CancelCommand -> cancellables[command.index]?.invoke() is CancelCommand -> cancellables[command.index]?.invoke()
is RootCommandOneWay -> defaultWorker.launch { is RootCommandOneWay -> defaultWorker.launch {
@@ -508,10 +513,10 @@ class RootServer @JvmOverloads constructor(private val warnLogger: (String) -> U
counter++ counter++
} }
job.cancel() job.cancel()
if (DEBUG) Log.d(TAG, "Clean up initiated before exit. Jobs: ${job.children.joinToString()}") if (isDebugEnabled) Log.d(TAG, "Clean up initiated before exit. Jobs: ${job.children.joinToString()}")
if (runBlocking { withTimeoutOrNull(5000) { job.join() } } == null) { if (runBlocking { withTimeoutOrNull(5000) { job.join() } } == null) {
Log.w(TAG, "Clean up timeout: ${job.children.joinToString()}") Log.w(TAG, "Clean up timeout: ${job.children.joinToString()}")
} else if (DEBUG) Log.d(TAG, "Clean up finished, exiting") } else if (isDebugEnabled) Log.d(TAG, "Clean up finished, exiting")
} }
} }
} }

View File

@@ -16,7 +16,7 @@ object RootManager : RootSession() {
@Parcelize @Parcelize
class RootInit : RootCommandNoResult { class RootInit : RootCommandNoResult {
override suspend fun execute(): Parcelable? { override suspend fun execute(): Parcelable? {
RootServer.DEBUG = BuildConfig.DEBUG RootServer.isDebugEnabled = BuildConfig.DEBUG
Timber.plant(object : Timber.DebugTree() { Timber.plant(object : Timber.DebugTree() {
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) { override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
if (priority >= Log.WARN) System.err.println("$priority/$tag: $message") if (priority >= Log.WARN) System.err.println("$priority/$tag: $message")
@@ -36,7 +36,7 @@ object RootManager : RootSession() {
override fun createServer() = RootServer { Timber.w(it) } override fun createServer() = RootServer { Timber.w(it) }
override suspend fun initServer(server: RootServer) { override suspend fun initServer(server: RootServer) {
RootServer.DEBUG = BuildConfig.DEBUG RootServer.isDebugEnabled = BuildConfig.DEBUG
try { try {
server.init(app) server.init(app)
} finally { } finally {