From df85fa37ef067681a027e6fe9212c8a065d4981b Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Thu, 23 Jun 2022 02:10:11 +0200 Subject: [PATCH] feat: improve logging --- .../app/revanced/cli/command/MainCommand.kt | 14 ++++----- .../app/revanced/cli/logging/CliLogger.kt | 8 +++++ .../cli/logging/impl/DefaultCliLogger.kt | 20 +++++++++++++ .../app/revanced/cli/patcher/Patcher.kt | 3 ++ .../app/revanced/cli/patcher/PatcherLogger.kt | 23 -------------- .../cli/patcher/logging/impl/PatcherLogger.kt | 13 ++++++++ .../app/revanced/cli/signing/Signing.kt | 6 ++-- src/main/kotlin/app/revanced/utils/adb/Adb.kt | 9 ++++-- .../app/revanced/utils/patcher/Patcher.kt | 30 ++++++++++++------- .../app/revanced/utils/signing/Signer.kt | 3 +- 10 files changed, 83 insertions(+), 46 deletions(-) create mode 100644 src/main/kotlin/app/revanced/cli/logging/CliLogger.kt create mode 100644 src/main/kotlin/app/revanced/cli/logging/impl/DefaultCliLogger.kt delete mode 100644 src/main/kotlin/app/revanced/cli/patcher/PatcherLogger.kt create mode 100644 src/main/kotlin/app/revanced/cli/patcher/logging/impl/PatcherLogger.kt diff --git a/src/main/kotlin/app/revanced/cli/command/MainCommand.kt b/src/main/kotlin/app/revanced/cli/command/MainCommand.kt index 8ef569d2..fe02ab0c 100644 --- a/src/main/kotlin/app/revanced/cli/command/MainCommand.kt +++ b/src/main/kotlin/app/revanced/cli/command/MainCommand.kt @@ -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") } } diff --git a/src/main/kotlin/app/revanced/cli/logging/CliLogger.kt b/src/main/kotlin/app/revanced/cli/logging/CliLogger.kt new file mode 100644 index 00000000..06849b11 --- /dev/null +++ b/src/main/kotlin/app/revanced/cli/logging/CliLogger.kt @@ -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) +} \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/cli/logging/impl/DefaultCliLogger.kt b/src/main/kotlin/app/revanced/cli/logging/impl/DefaultCliLogger.kt new file mode 100644 index 00000000..395922b9 --- /dev/null +++ b/src/main/kotlin/app/revanced/cli/logging/impl/DefaultCliLogger.kt @@ -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) +} \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/cli/patcher/Patcher.kt b/src/main/kotlin/app/revanced/cli/patcher/Patcher.kt index 7962788e..bd85c3b6 100644 --- a/src/main/kotlin/app/revanced/cli/patcher/Patcher.kt +++ b/src/main/kotlin/app/revanced/cli/patcher/Patcher.kt @@ -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()) } diff --git a/src/main/kotlin/app/revanced/cli/patcher/PatcherLogger.kt b/src/main/kotlin/app/revanced/cli/patcher/PatcherLogger.kt deleted file mode 100644 index bf7f664c..00000000 --- a/src/main/kotlin/app/revanced/cli/patcher/PatcherLogger.kt +++ /dev/null @@ -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") - } -} \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/cli/patcher/logging/impl/PatcherLogger.kt b/src/main/kotlin/app/revanced/cli/patcher/logging/impl/PatcherLogger.kt new file mode 100644 index 00000000..172fa9b3 --- /dev/null +++ b/src/main/kotlin/app/revanced/cli/patcher/logging/impl/PatcherLogger.kt @@ -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) +} \ No newline at end of file diff --git a/src/main/kotlin/app/revanced/cli/signing/Signing.kt b/src/main/kotlin/app/revanced/cli/signing/Signing.kt index 80eb2388..5732d712 100644 --- a/src/main/kotlin/app/revanced/cli/signing/Signing.kt +++ b/src/main/kotlin/app/revanced/cli/signing/Signing.kt @@ -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) } } diff --git a/src/main/kotlin/app/revanced/utils/adb/Adb.kt b/src/main/kotlin/app/revanced/utils/adb/Adb.kt index 723c594d..a66e3524 100644 --- a/src/main/kotlin/app/revanced/utils/adb/Adb.kt +++ b/src/main/kotlin/app/revanced/utils/adb/Adb.kt @@ -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() } diff --git a/src/main/kotlin/app/revanced/utils/patcher/Patcher.kt b/src/main/kotlin/app/revanced/utils/patcher/Patcher.kt index f6a0e567..d4b46647 100644 --- a/src/main/kotlin/app/revanced/utils/patcher/Patcher.kt +++ b/src/main/kotlin/app/revanced/utils/patcher/Patcher.kt @@ -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") } } diff --git a/src/main/kotlin/app/revanced/utils/signing/Signer.kt b/src/main/kotlin/app/revanced/utils/signing/Signer.kt index 7f9bfdaa..02a91ae6 100644 --- a/src/main/kotlin/app/revanced/utils/signing/Signer.kt +++ b/src/main/kotlin/app/revanced/utils/signing/Signer.kt @@ -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")