feat: improve logging

This commit is contained in:
oSumAtrIX 2022-06-23 02:10:11 +02:00
parent 04805e45fe
commit df85fa37ef
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
10 changed files with 83 additions and 46 deletions

View File

@ -1,7 +1,8 @@
package app.revanced.cli.command
import app.revanced.cli.logging.impl.DefaultCliLogger
import app.revanced.cli.patcher.Patcher
import app.revanced.cli.patcher.PatcherLogger
import app.revanced.cli.patcher.logging.impl.PatcherLogger
import app.revanced.cli.signing.Signing
import app.revanced.cli.signing.SigningOptions
import app.revanced.patcher.PatcherOptions
@ -25,6 +26,8 @@ private class CLIVersionProvider : IVersionProvider {
versionProvider = CLIVersionProvider::class
)
internal object MainCommand : Runnable {
val logger = DefaultCliLogger()
@ArgGroup(exclusive = false, multiplicity = "1")
lateinit var args: Args
@ -91,7 +94,7 @@ internal object MainCommand : Runnable {
override fun run() {
if (args.lArgs?.listOnly == true) {
for (patchBundlePath in args.patchBundles) for (patch in JarPatchBundle(patchBundlePath).loadPatches()) {
println("[available] ${patch.patchName}: ${patch.description}")
logger.info("${patch.patchName}: ${patch.description}")
}
return
}
@ -134,13 +137,10 @@ internal object MainCommand : Runnable {
if (args.clean) File(args.cacheDirectory).deleteRecursively()
adb?.let {
println("[deploying]")
it.deploy()
}
adb?.deploy()
if (args.clean && args.deploy != null) Files.delete(outputFile.toPath())
println("[done]")
logger.info("Finished")
}
}

View File

@ -0,0 +1,8 @@
package app.revanced.cli.logging
internal interface CliLogger {
fun error(msg: String)
fun info(msg: String)
fun trace(msg: String)
fun warn(msg: String)
}

View File

@ -0,0 +1,20 @@
package app.revanced.cli.logging.impl
import app.revanced.cli.command.MainCommand
import app.revanced.cli.logging.CliLogger
import java.util.logging.Logger
internal class DefaultCliLogger(
private val logger: Logger = Logger.getLogger(MainCommand::javaClass.name)
) : CliLogger {
companion object {
init {
System.setProperty("java.util.logging.SimpleFormatter.format", "%4\$s: %5\$s %n")
}
}
override fun error(msg: String) = logger.severe(msg)
override fun info(msg: String) = logger.info(msg)
override fun trace(msg: String) = logger.finest(msg)
override fun warn(msg: String) = logger.warning(msg)
}

View File

@ -1,6 +1,7 @@
package app.revanced.cli.patcher
import app.revanced.cli.command.MainCommand.args
import app.revanced.cli.command.MainCommand.logger
import app.revanced.utils.filesystem.ZipFileSystemUtils
import app.revanced.utils.patcher.addPatchesFiltered
import app.revanced.utils.patcher.applyPatchesVerbose
@ -30,12 +31,14 @@ internal object Patcher {
ZipFileSystemUtils(inputFile, output).use { fileSystem ->
// replace all dex files
result.dexFiles.forEach {
logger.info("Writing dex file ${it.name}")
fileSystem.write(it.name, it.dexFileInputStream.readAllBytes())
}
// inputFile being null implies resource patching being disabled
if (inputFile != null) {
// write resources
logger.info("Writing resources")
fileSystem.writeInput()
fileSystem.uncompress(*result.doNotCompress!!.toTypedArray())
}

View File

@ -1,23 +0,0 @@
package app.revanced.cli.patcher
import app.revanced.patcher.PatchLogger
object PatcherLogger : PatchLogger {
private const val prefix = "[patcher]"
override fun error(msg: String) {
println("error: $prefix: $msg")
}
override fun info(msg: String) {
println("info: $prefix: $msg")
}
override fun trace(msg: String) {
println("trace: $prefix: $msg")
}
override fun warn(msg: String) {
println("warn: $prefix: $msg")
}
}

View File

@ -0,0 +1,13 @@
package app.revanced.cli.patcher.logging.impl
import app.revanced.cli.logging.impl.DefaultCliLogger
import java.util.logging.Logger
internal object PatcherLogger : app.revanced.patcher.logging.Logger{
private val logger = DefaultCliLogger(Logger.getLogger(app.revanced.patcher.Patcher::javaClass.name))
override fun error(msg: String) = logger.error(msg)
override fun info(msg: String) = logger.info(msg)
override fun warn(msg: String)= logger.warn(msg)
override fun trace(msg: String)= logger.trace(msg)
}

View File

@ -1,6 +1,7 @@
package app.revanced.cli.signing
import app.revanced.cli.command.MainCommand.args
import app.revanced.cli.command.MainCommand.logger
import app.revanced.utils.signing.Signer
import app.revanced.utils.signing.align.ZipAligner
import java.io.File
@ -12,15 +13,16 @@ object Signing {
val signedOutput = cacheDirectory.resolve("${outputFile.nameWithoutExtension}_signed.apk")
// align the inputFile and write to alignedOutput
println("[aligning]")
logger.info("Aligning ${inputFile.name}")
ZipAligner.align(inputFile, alignedOutput)
// sign the alignedOutput and write to signedOutput
// the reason is, in case the signer fails
// it does not damage the output file
println("[signing]")
logger.info("Signing ${alignedOutput.name}")
Signer(signingOptions).signApk(alignedOutput, signedOutput)
// afterwards copy over the file to the output
logger.info("Copying ${signedOutput.name} to ${outputFile.name}")
signedOutput.copyTo(outputFile, true)
}
}

View File

@ -1,5 +1,6 @@
package app.revanced.utils.adb
import app.revanced.cli.command.MainCommand.logger
import se.vidstige.jadb.JadbConnection
import se.vidstige.jadb.JadbDevice
import se.vidstige.jadb.managers.PackageManager
@ -29,8 +30,12 @@ internal class Adb(
internal fun deploy() {
if (modeInstall) {
logger.info("Installing without mounting")
PackageManager(device).install(file)
} else {
logger.info("Installing by mounting")
// push patched file
device.copy(Constants.PATH_INIT_PUSH, file)
@ -91,10 +96,10 @@ internal class Adb(
}
break
} catch (e: Exception) {
throw RuntimeException("An error occurred while monitoring state of app", e)
throw RuntimeException("An error occurred while monitoring the state of app", e)
}
}
println("App closed, continuing.")
logger.info("Stopped logging because the app was closed")
process.destroy()
executor.shutdown()
}

View File

@ -2,6 +2,7 @@ package app.revanced.utils.patcher
import app.revanced.cli.command.MainCommand
import app.revanced.cli.command.MainCommand.args
import app.revanced.cli.command.MainCommand.logger
import app.revanced.patcher.Patcher
import app.revanced.patcher.data.base.Data
import app.revanced.patcher.extensions.PatchExtensions.compatiblePackages
@ -22,33 +23,40 @@ fun Patcher.addPatchesFiltered(
val compatiblePackages = patch.compatiblePackages
val patchName = patch.patchName
val prefix = "[skipped] $patchName"
val prefix = "Skipping $patchName"
val args = MainCommand.args.pArgs!!
if (excludePatches && args.excludedPatches.contains(patchName)) {
println("$prefix: Explicitly excluded.")
logger.info("$prefix: Explicitly excluded")
return@patch
} else if (!patch.include) {
println("$prefix: Implicitly excluded.")
logger.info("$prefix: Explicitly excluded")
return@patch
}
if (compatiblePackages == null) println("$prefix: Missing compatibility annotation. Continuing.")
if (compatiblePackages == null) logger.warn("$prefix: Missing compatibility annotation. Continuing.")
else {
if (!compatiblePackages.any { it.name == packageName }) {
println("$prefix: Incompatible package.")
logger.warn("$prefix: Incompatible with $packageName. This patch is only compatible with ${
compatiblePackages.joinToString(
", "
) { it.name }
}")
return@patch
}
if (!(args.experimental || compatiblePackages.any { it.versions.isEmpty() || it.versions.any { version -> version == packageVersion } })) {
println("$prefix: The package version is $packageVersion and is incompatible.")
val compatibleWith = compatiblePackages.map { _package ->
"${_package.name}: ${_package.versions.joinToString(", ")}"
}.joinToString(";")
logger.warn("$prefix: Incompatible with version $packageVersion. This patch is only compatible with version $compatibleWith")
return@patch
}
}
logger.trace("Adding $patchName")
includedPatches.add(patch)
println("[added] $patchName")
}
this.addPatches(includedPatches)
}
@ -57,16 +65,16 @@ fun Patcher.addPatchesFiltered(
fun Patcher.applyPatchesVerbose() {
this.applyPatches().forEach { (patch, result) ->
if (result.isSuccess) {
println("[success] $patch")
logger.info("$patch succeeded")
return@forEach
}
println("[error] $patch:")
logger.error("$patch failed:")
result.exceptionOrNull()!!.printStackTrace()
}
}
fun Patcher.mergeFiles() {
this.addFiles(args.pArgs!!.mergeFiles) {
println("[merged] ${it.name}")
this.addFiles(args.pArgs!!.mergeFiles) { file ->
logger.info("Merging $file")
}
}

View File

@ -1,5 +1,6 @@
package app.revanced.utils.signing
import app.revanced.cli.command.MainCommand.logger
import app.revanced.cli.signing.SigningOptions
import com.android.apksig.ApkSigner
import org.bouncycastle.asn1.x500.X500Name
@ -55,7 +56,7 @@ internal class Signer(
// TODO: keystore should be saved securely
val ks = File(signingOptions.keyStoreFilePath)
if (!ks.exists()) newKeystore(ks) else {
println("[found] existing keystore: ${ks.nameWithoutExtension}")
logger.info("Found existing keystore: ${ks.nameWithoutExtension}")
}
val keyStore = KeyStore.getInstance("BKS", "BC")