Skip to content
This repository has been archived by the owner on Apr 15, 2024. It is now read-only.

Commit

Permalink
feat: improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Jun 23, 2022
1 parent 04805e4 commit df85fa3
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 46 deletions.
14 changes: 7 additions & 7 deletions src/main/kotlin/app/revanced/cli/command/MainCommand.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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")
}
}
8 changes: 8 additions & 0 deletions src/main/kotlin/app/revanced/cli/logging/CliLogger.kt
Original file line number Diff line number Diff line change
@@ -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)
}
20 changes: 20 additions & 0 deletions src/main/kotlin/app/revanced/cli/logging/impl/DefaultCliLogger.kt
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 3 additions & 0 deletions src/main/kotlin/app/revanced/cli/patcher/Patcher.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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())
}
Expand Down
23 changes: 0 additions & 23 deletions src/main/kotlin/app/revanced/cli/patcher/PatcherLogger.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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)
}
6 changes: 4 additions & 2 deletions src/main/kotlin/app/revanced/cli/signing/Signing.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
}
}
9 changes: 7 additions & 2 deletions src/main/kotlin/app/revanced/utils/adb/Adb.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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()
}
Expand Down
30 changes: 19 additions & 11 deletions src/main/kotlin/app/revanced/utils/patcher/Patcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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")
}
}
3 changes: 2 additions & 1 deletion src/main/kotlin/app/revanced/utils/signing/Signer.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit df85fa3

Please sign in to comment.