Skip to content

Commit

Permalink
Fix Shizuku/Root logs not being included in debug logs
Browse files Browse the repository at this point in the history
See #1390
  • Loading branch information
d4rken committed Sep 14, 2024
1 parent e0dfcd6 commit 8bb74c2
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import javax.inject.Inject
*/
@Keep
@SuppressLint("UnsafeDynamicallyLoadedCode")
class RootHost constructor(_args: List<String>) : HasSharedResource<Any>, BaseRootHost("$TAG#${hashCode()}", _args) {
class RootHost(_args: List<String>) : HasSharedResource<Any>, BaseRootHost("$TAG#${hashCode()}", _args) {

override val sharedResource = SharedResource.createKeepAlive(iTag, hostScope)

Expand Down Expand Up @@ -58,6 +58,7 @@ class RootHost constructor(_args: List<String>) : HasSharedResource<Any>, BaseRo

ipc.hostOptions
.onEach { options ->
log(TAG) { "New options: $options" }
if (options.isDebug && Logging.loggers.none { it == logCatLogger }) {
Logging.install(logCatLogger)
log(TAG) { "Logger installed!" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import eu.darken.sdmse.common.debug.logging.asLog
import eu.darken.sdmse.common.debug.logging.log
import eu.darken.sdmse.common.debug.logging.logTag
import eu.darken.sdmse.common.files.local.ipc.FileOpsClient
import eu.darken.sdmse.common.flow.setupCommonEventHandlers
import eu.darken.sdmse.common.ipc.IpcClientModule
import eu.darken.sdmse.common.pkgs.pkgops.ipc.PkgOpsClient
import eu.darken.sdmse.common.root.RootSettings
Expand All @@ -19,9 +20,11 @@ import eu.darken.sdmse.common.sharedresource.SharedResource
import eu.darken.sdmse.common.shell.ipc.ShellOpsClient
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onCompletion
Expand Down Expand Up @@ -53,11 +56,11 @@ class RootServiceClient @Inject constructor(
recorderPath = debugSettings.recorderPath.value(),
)

var lastInternal: RootConnection? = null
val lastInternal = MutableStateFlow<RootConnection?>(null)
rootHostLauncher
.createHostConnection(options = initialOptions)
.onEach { wrapper ->
lastInternal = wrapper.host
lastInternal.value = wrapper.host
send(wrapper.service)
}
.launchIn(this)
Expand All @@ -66,19 +69,20 @@ class RootServiceClient @Inject constructor(
debugSettings.isDebugMode.flow,
debugSettings.isTraceMode.flow,
debugSettings.isDryRunMode.flow,
debugSettings.recorderPath.flow
) { isDebug, isTrace, isDryRun, recorderPath ->
lastInternal?.let {
val dynamicOptions = RootHostOptions(
isDebug = isDebug,
isTrace = isTrace,
isDryRun = isDryRun,
recorderPath = recorderPath,
)
log(TAG) { "Updating debug settings: $dynamicOptions" }
it.updateHostOptions(dynamicOptions)
}
}.launchIn(this)
debugSettings.recorderPath.flow,
lastInternal.filterNotNull(),
) { isDebug, isTrace, isDryRun, recorderPath, lastConnection ->
val dynamicOptions = RootHostOptions(
isDebug = isDebug,
isTrace = isTrace,
isDryRun = isDryRun,
recorderPath = recorderPath,
)
log(TAG) { "Updating debug settings: $dynamicOptions" }
lastConnection.updateHostOptions(dynamicOptions)
}
.setupCommonEventHandlers(TAG) { "dynamic-debug-settings" }
.launchIn(this)

log(TAG) { "awaitClose()..." }
awaitClose {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class ShizukuHost(

currentOptions
.onEach { options ->
log(TAG) { "New options: $options" }
if (options.isDebug && Logging.loggers.none { it == logCatLogger }) {
Logging.install(logCatLogger)
log(TAG) { "Logger installed!" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import eu.darken.sdmse.common.debug.logging.asLog
import eu.darken.sdmse.common.debug.logging.log
import eu.darken.sdmse.common.debug.logging.logTag
import eu.darken.sdmse.common.files.local.ipc.FileOpsClient
import eu.darken.sdmse.common.flow.setupCommonEventHandlers
import eu.darken.sdmse.common.ipc.IpcClientModule
import eu.darken.sdmse.common.pkgs.pkgops.ipc.PkgOpsClient
import eu.darken.sdmse.common.sharedresource.SharedResource
Expand All @@ -20,9 +21,11 @@ import eu.darken.sdmse.common.shizuku.service.internal.ShizukuHostLauncher
import eu.darken.sdmse.common.shizuku.service.internal.ShizukuHostOptions
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onCompletion
Expand Down Expand Up @@ -55,11 +58,11 @@ class ShizukuServiceClient @Inject constructor(
recorderPath = debugSettings.recorderPath.value(),
)

var lastInternal: ShizukuConnection? = null
val lastInternal = MutableStateFlow<ShizukuConnection?>(null)
serviceLauncher
.createServiceHostConnection(optionsInitial)
.onEach { wrapper ->
lastInternal = wrapper.host
lastInternal.value = wrapper.host
send(wrapper.service)
}
.launchIn(this)
Expand All @@ -69,18 +72,19 @@ class ShizukuServiceClient @Inject constructor(
debugSettings.isTraceMode.flow,
debugSettings.isDryRunMode.flow,
debugSettings.recorderPath.flow,
) { isDebug, isTrace, isDryRun, recorderPath ->
lastInternal?.let {
val optionsDynamic = ShizukuHostOptions(
isDebug = isDebug,
isTrace = isTrace,
isDryRun = isDryRun,
recorderPath = recorderPath,
)
log(TAG) { "Updating debug settings: $optionsDynamic" }
it.updateHostOptions(optionsDynamic)
}
}.launchIn(this)
lastInternal.filterNotNull(),
) { isDebug, isTrace, isDryRun, recorderPath, lastConnection ->
val optionsDynamic = ShizukuHostOptions(
isDebug = isDebug,
isTrace = isTrace,
isDryRun = isDryRun,
recorderPath = recorderPath,
)
log(TAG) { "Updating debug settings: $optionsDynamic" }
lastConnection.updateHostOptions(optionsDynamic)
}
.setupCommonEventHandlers(TAG) { "dynamic-debug-settings" }
.launchIn(this)

log(TAG) { "awaitClose()..." }
awaitClose {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,21 @@ class RecorderViewModel @Inject constructor(
private val logObsDefault = pathCache
.map { File(it) }
.map { LogData(it, it.length()) }
.onEach { log(TAG) { "Base log: $it" } }
.catch { log(TAG, ERROR) { "Failed to get default log size: ${it.asLog()}" } }
.replayingShare(vmScope)

private val logObsShizuku = pathCache
.map { File(it.replace(".log", "_shizuku.log")) }
.map { if (it.exists()) LogData(it, it.length()) else null }
.onEach { log(TAG) { "Shizuku log: $it" } }
.catch { log(TAG, ERROR) { "Failed to get Shizuku log size: ${it.asLog()}" } }
.replayingShare(vmScope)

private val logObsRoot = pathCache
.map { File(it.replace(".log", "_root.log")) }
.map { if (it.exists()) LogData(it, it.length()) else null }
.onEach { log(TAG) { "Root log: $it" } }
.catch { log(TAG, ERROR) { "Failed to get root log size: ${it.asLog()}" } }
.replayingShare(vmScope)

Expand All @@ -76,6 +79,7 @@ class RecorderViewModel @Inject constructor(
shizuku?.file?.path,
root?.file?.path
)
log(TAG) { "Compressing files: $zipContent" }
val zipFile = File("${default.file.path.dropLast(4)}.zip")
Zipper().zip(zipContent, zipFile.path)
zipFile to zipFile.length()
Expand Down

0 comments on commit 8bb74c2

Please sign in to comment.