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

Commit

Permalink
feat: properly make use of logging facade
Browse files Browse the repository at this point in the history
  • Loading branch information
oSumAtrIX committed Aug 24, 2023
1 parent 45dd15f commit 41898d7
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ import app.revanced.patcher.patch.PatchOption
import picocli.CommandLine.*
import picocli.CommandLine.Help.Visibility.ALWAYS
import java.io.File
import java.util.logging.Logger


@Command(name = "list-patches", description = ["List patches from supplied patch bundles"])
internal object ListPatchesCommand : Runnable {
private val logger = Logger.getLogger(ListPatchesCommand::class.java.name)

@Parameters(
description = ["Paths to patch bundles"],
arity = "1..*"
Expand Down
33 changes: 30 additions & 3 deletions src/main/kotlin/app/revanced/cli/command/MainCommand.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
package app.revanced.cli.command

import app.revanced.cli.logging.impl.DefaultCliLogger
import app.revanced.patcher.patch.PatchClass
import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.IVersionProvider
import java.util.*
import java.util.logging.*


fun main(args: Array<String>) {
System.setProperty("java.util.logging.SimpleFormatter.format", "%4\$s: %5\$s %n")
Logger.getLogger("").apply {
handlers.forEach {
it.close()
removeHandler(it)
}

object : Handler() {
override fun publish(record: LogRecord) = formatter.format(record).let {
if (record.level.intValue() > Level.INFO.intValue()) {
System.err.write(it.toByteArray())
} else {
System.out.write(it.toByteArray())
}
}

override fun flush() {
System.out.flush()
System.err.flush()
}

override fun close() = flush()
}.also {
it.level = Level.ALL
it.formatter = SimpleFormatter()
}.let(::addHandler)
}

CommandLine(MainCommand).execute(*args)
}

internal typealias PatchList = List<PatchClass>

internal val logger = DefaultCliLogger()

object CLIVersionProvider : IVersionProvider {
override fun getVersion(): Array<String> {
Properties().apply {
Expand Down
7 changes: 5 additions & 2 deletions src/main/kotlin/app/revanced/cli/command/OptionsCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import app.revanced.utils.Options.setOptions
import picocli.CommandLine
import picocli.CommandLine.Help.Visibility.ALWAYS
import java.io.File
import java.util.logging.Logger

@CommandLine.Command(
name = "options",
description = ["Generate options file from patches"],
)
internal object OptionsCommand : Runnable {
private val logger = Logger.getLogger(OptionsCommand::class.java.name)

@CommandLine.Parameters(
description = ["Paths to patch bundles"],
arity = "1..*"
Expand Down Expand Up @@ -41,10 +44,10 @@ internal object OptionsCommand : Runnable {

override fun run() = if (!path.exists() || overwrite)
with(PatchBundleLoader.Jar(*patchBundles)) {
if (update) setOptions(path, logger)
if (update) setOptions(path)

Options.serialize(this, prettyPrint = true)
.let(path::writeText)
}
else logger.error("Options file already exists, use --override to override it")
else logger.severe("Options file already exists, use --override to override it")
}
25 changes: 12 additions & 13 deletions src/main/kotlin/app/revanced/cli/command/PatchCommand.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package app.revanced.cli.command

import app.revanced.cli.patcher.logging.impl.PatcherLogger
import app.revanced.patcher.PatchBundleLoader
import app.revanced.patcher.Patcher
import app.revanced.patcher.PatcherOptions
Expand All @@ -20,13 +19,16 @@ import kotlinx.coroutines.runBlocking
import picocli.CommandLine
import picocli.CommandLine.Help.Visibility.ALWAYS
import java.io.File
import java.util.logging.Logger


@CommandLine.Command(
name = "patch",
description = ["Patch the supplied APK file with the supplied patches and integrations"]
)
internal object PatchCommand: Runnable {
private val logger = Logger.getLogger(PatchCommand::class.java.name)

@CommandLine.Parameters(
description = ["APK file to be patched"],
arity = "1..1"
Expand Down Expand Up @@ -144,15 +146,13 @@ internal object PatchCommand: Runnable {
// region Prepare

if (!apk.exists()) {
logger.error("Input file ${apk.name} does not exist")
logger.severe("Input file ${apk.name} does not exist")
return
}

val adbManager = deviceSerial?.let { serial ->
if (mount) AdbManager.RootAdbManager(serial, logger) else AdbManager.UserAdbManager(
serial,
logger
)
if (mount) AdbManager.RootAdbManager(serial)
else AdbManager.UserAdbManager(serial)
}

// endregion
Expand All @@ -167,7 +167,7 @@ internal object PatchCommand: Runnable {
logger.info("Setting patch options")

optionsFile.let {
if (it.exists()) patches.setOptions(it, logger)
if (it.exists()) patches.setOptions(it)
else Options.serialize(patches, prettyPrint = true).let(it::writeText)
}

Expand All @@ -181,7 +181,6 @@ internal object PatchCommand: Runnable {
resourceCachePath,
aaptBinaryPath.path,
resourceCachePath.absolutePath,
PatcherLogger
)
)

Expand All @@ -193,7 +192,7 @@ internal object PatchCommand: Runnable {
runBlocking {
apply(false).collect { patchResult ->
patchResult.exception?.let {
logger.error("${patchResult.patchName} failed:\n${patchResult.exception}")
logger.severe("${patchResult.patchName} failed:\n${patchResult.exception}")
} ?: logger.info("${patchResult.patchName} succeeded")
}
}
Expand Down Expand Up @@ -288,22 +287,22 @@ internal object PatchCommand: Runnable {
it.isEmpty() || it.any { version -> version == packageVersion }
}

if (!matchesVersion) return@patch logger.warn(
if (!matchesVersion) return@patch logger.warning(
"${patch.patchName} is incompatible with version $packageVersion. " +
"This patch is only compatible with version " +
packages.joinToString(";") { pkg ->
"${pkg.name}: ${pkg.versions.joinToString(", ")}"
}
)

} ?: return@patch logger.trace(
} ?: return@patch logger.fine(
"${patch.patchName} is incompatible with $packageName. " +
"This patch is only compatible with " +
packages.joinToString(", ") { `package` -> `package`.name }
)

return@let
} ?: logger.trace("$formattedPatchName: No constraint on packages.")
} ?: logger.fine("$formattedPatchName: No constraint on packages.")

/**
* Check if the patch is explicitly included.
Expand All @@ -329,7 +328,7 @@ internal object PatchCommand: Runnable {
val included = implicitlyIncluded || exclusivelyIncluded
if (!included) return@patch logger.info("${patch.patchName} excluded by default") // Case 1.

logger.trace("Adding $formattedPatchName")
logger.fine("Adding $formattedPatchName")

add(patch)
}
Expand Down
9 changes: 6 additions & 3 deletions src/main/kotlin/app/revanced/cli/command/UninstallCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package app.revanced.cli.command
import app.revanced.utils.adb.AdbManager
import picocli.CommandLine.*
import picocli.CommandLine.Help.Visibility.ALWAYS
import java.util.logging.Logger


@Command(
name = "uninstall",
description = ["Uninstall a patched APK file from the devices with the supplied ADB device serials"]
)
internal object UninstallCommand : Runnable {
private val logger = Logger.getLogger(UninstallCommand::class.java.name)

@Parameters(
description = ["ADB device serials"],
arity = "1..*"
Expand All @@ -33,12 +36,12 @@ internal object UninstallCommand : Runnable {
override fun run() = try {
deviceSerials.forEach {deviceSerial ->
if (unmount) {
AdbManager.RootAdbManager(deviceSerial, logger)
AdbManager.RootAdbManager(deviceSerial)
} else {
AdbManager.UserAdbManager(deviceSerial, logger)
AdbManager.UserAdbManager(deviceSerial)
}.uninstall(packageName)
}
} catch (e: AdbManager.DeviceNotFoundException) {
logger.error(e.toString())
logger.severe(e.toString())
}
}
8 changes: 0 additions & 8 deletions src/main/kotlin/app/revanced/cli/logging/CliLogger.kt

This file was deleted.

30 changes: 0 additions & 30 deletions src/main/kotlin/app/revanced/cli/logging/impl/DefaultCliLogger.kt

This file was deleted.

This file was deleted.

This file was deleted.

14 changes: 7 additions & 7 deletions src/main/kotlin/app/revanced/utils/Options.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package app.revanced.utils

import app.revanced.cli.command.PatchList
import app.revanced.cli.logging.CliLogger
import app.revanced.patcher.extensions.PatchExtensions.options
import app.revanced.patcher.extensions.PatchExtensions.patchName
import app.revanced.patcher.patch.NoSuchOptionException
import app.revanced.utils.Options.PatchOption.Option
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import java.io.File
import java.util.logging.Logger


internal object Options {
private val logger = Logger.getLogger(Options::class.java.name)

private var mapper = jacksonObjectMapper()

/**
Expand Down Expand Up @@ -53,9 +55,8 @@ internal object Options {
* Sets the options for the patches in the list.
*
* @param json The JSON string containing the options.
* @param logger The logger to use for logging.
*/
fun PatchList.setOptions(json: String, logger: CliLogger? = null) {
fun PatchList.setOptions(json: String) {
filter { it.options?.any() == true }.let { patches ->
if (patches.isEmpty()) return

Expand All @@ -66,9 +67,9 @@ internal object Options {
it.options.forEach { option ->
try {
patch.options?.set(option.key, option.value)
?: logger?.warn("${patch.patchName} has no options")
?: logger.warning("${patch.patchName} has no options")
} catch (e: NoSuchOptionException) {
logger?.error(e.message ?: "Unknown error")
logger.info(e.message ?: "Unknown error")
}
}
}
Expand All @@ -80,10 +81,9 @@ internal object Options {
* Sets the options for the patches in the list.
*
* @param file The file containing the JSON string containing the options.
* @param logger The logger to use for logging.
* @see setOptions
*/
fun PatchList.setOptions(file: File, logger: CliLogger? = null) = setOptions(file.readText(), logger)
fun PatchList.setOptions(file: File) = setOptions(file.readText())

/**
* Data class for a patch and its [Option]s.
Expand Down
Loading

0 comments on commit 41898d7

Please sign in to comment.