From 183972475bfc6e9f633e44e87c5128f39f5635f3 Mon Sep 17 00:00:00 2001 From: Aria Moradi Date: Thu, 25 Mar 2021 16:53:36 +0430 Subject: [PATCH] better loging --- .../main/kotlin/ir/armor/tachidesk/Main.kt | 10 ++++--- .../ir/armor/tachidesk/util/Extension.kt | 30 ++++++++----------- .../ir/armor/tachidesk/util/ExtensionsList.kt | 7 +++-- .../ir/armor/tachidesk/util/SourceList.kt | 12 ++++---- 4 files changed, 31 insertions(+), 28 deletions(-) diff --git a/server/src/main/kotlin/ir/armor/tachidesk/Main.kt b/server/src/main/kotlin/ir/armor/tachidesk/Main.kt index 40e0c40..64cb9cf 100644 --- a/server/src/main/kotlin/ir/armor/tachidesk/Main.kt +++ b/server/src/main/kotlin/ir/armor/tachidesk/Main.kt @@ -33,11 +33,14 @@ import ir.armor.tachidesk.util.sourceFilters import ir.armor.tachidesk.util.sourceGlobalSearch import ir.armor.tachidesk.util.sourceSearch import ir.armor.tachidesk.util.updateCategory +import mu.KLogging class Main { - companion object { + companion object : KLogging() { + @JvmStatic fun main(args: Array) { + serverSetup() var hasWebUiBundled: Boolean = false @@ -49,7 +52,7 @@ class Main { config.addStaticFiles("/react") config.addSinglePageRoot("/", "/react/index.html") } catch (e: RuntimeException) { - println("Warning: react build files are missing.") + logger.warn("react build files are missing.") hasWebUiBundled = false } config.enableCorsForAllOrigins() @@ -68,7 +71,6 @@ class Main { app.get("/api/v1/extension/install/:apkName") { ctx -> val apkName = ctx.pathParam("apkName") - println("installing $apkName") ctx.status( installAPK(apkName) @@ -77,7 +79,7 @@ class Main { app.get("/api/v1/extension/uninstall/:apkName") { ctx -> val apkName = ctx.pathParam("apkName") - println("uninstalling $apkName") + removeExtension(apkName) ctx.status(200) } diff --git a/server/src/main/kotlin/ir/armor/tachidesk/util/Extension.kt b/server/src/main/kotlin/ir/armor/tachidesk/util/Extension.kt index 48f1f71..a5d6fbe 100644 --- a/server/src/main/kotlin/ir/armor/tachidesk/util/Extension.kt +++ b/server/src/main/kotlin/ir/armor/tachidesk/util/Extension.kt @@ -15,6 +15,7 @@ import ir.armor.tachidesk.applicationDirs import ir.armor.tachidesk.database.table.ExtensionTable import ir.armor.tachidesk.database.table.SourceTable import kotlinx.coroutines.runBlocking +import mu.KotlinLogging import okhttp3.Request import okio.buffer import okio.sink @@ -29,7 +30,10 @@ import java.io.InputStream import java.net.URL import java.net.URLClassLoader +private val logger = KotlinLogging.logger {} + fun installAPK(apkName: String): Int { + logger.info("Installing $apkName") val extensionRecord = getExtensionList(true).first { it.apkName == apkName } val fileNameWithoutType = apkName.substringBefore(".apk") val dirPathWithoutType = "${applicationDirs.extensionsRoot}/$fileNameWithoutType" @@ -49,7 +53,7 @@ fun installAPK(apkName: String): Int { downloadAPKFile(apkToDownload, apkFilePath) val className: String = APKExtractor.extract_dex_and_read_className(apkFilePath, dexFilePath) - println(className) + logger.info(className) // dex -> jar Dex2jarCmd.main(dexFilePath, "-o", jarFilePath, "--force") @@ -69,11 +73,6 @@ fun installAPK(apkName: String): Int { if (instance is HttpSource) { // single source val httpSource = instance as HttpSource transaction { -// SourceEntity.new { -// sourceId = httpSource.id -// name = httpSource.name -// this.extension = ExtensionEntity.find { ExtensionsTable.name eq extension.name }.first().id -// } if (SourceTable.select { SourceTable.id eq httpSource.id }.count() == 0L) { SourceTable.insert { it[this.id] = httpSource.id @@ -82,9 +81,7 @@ fun installAPK(apkName: String): Int { it[extension] = extensionId } } -// println(httpSource.id) -// println(httpSource.name) -// println() + logger.info("Installed source ${httpSource.name} with id:${httpSource.id}") } } else { // multi source val sourceFactory = instance as SourceFactory @@ -101,9 +98,7 @@ fun installAPK(apkName: String): Int { it[positionInFactorySource] = index } } -// println(httpSource.id) -// println(httpSource.name) -// println() + logger.info("Installed source ${httpSource.name} with id:${httpSource.id}") } } } @@ -134,9 +129,11 @@ private fun downloadAPKFile(url: String, apkPath: String) { sink.close() } -fun removeExtension(pkgName: String) { - val extensionRecord = getExtensionList(true).first { it.apkName == pkgName } - val fileNameWithoutType = pkgName.substringBefore(".apk") +fun removeExtension(apkName: String) { + logger.info("Uninstalling $apkName") + + val extensionRecord = getExtensionList(true).first { it.apkName == apkName } + val fileNameWithoutType = apkName.substringBefore(".apk") val jarPath = "${applicationDirs.extensionsRoot}/$fileNameWithoutType.jar" transaction { val extensionId = ExtensionTable.select { ExtensionTable.name eq extensionRecord.name }.first()[ExtensionTable.id] @@ -158,9 +155,8 @@ fun getExtensionIcon(apkName: String): Pair { val iconUrl = transaction { ExtensionTable.select { ExtensionTable.apkName eq apkName }.firstOrNull()!! }[ExtensionTable.iconUrl] val saveDir = "${applicationDirs.extensionsRoot}/icon" - val fileName = apkName - return getCachedResponse(saveDir, fileName) { + return getCachedResponse(saveDir, apkName) { network.client.newCall( GET(iconUrl) ).execute() diff --git a/server/src/main/kotlin/ir/armor/tachidesk/util/ExtensionsList.kt b/server/src/main/kotlin/ir/armor/tachidesk/util/ExtensionsList.kt index 346c761..22dbef5 100644 --- a/server/src/main/kotlin/ir/armor/tachidesk/util/ExtensionsList.kt +++ b/server/src/main/kotlin/ir/armor/tachidesk/util/ExtensionsList.kt @@ -9,12 +9,15 @@ import eu.kanade.tachiyomi.extension.model.Extension import ir.armor.tachidesk.database.dataclass.ExtensionDataClass import ir.armor.tachidesk.database.table.ExtensionTable import kotlinx.coroutines.runBlocking +import mu.KotlinLogging import org.jetbrains.exposed.sql.insert import org.jetbrains.exposed.sql.select import org.jetbrains.exposed.sql.selectAll import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.update +private val logger = KotlinLogging.logger {} + private object Data { var lastExtensionCheck: Long = 0 } @@ -28,7 +31,7 @@ private fun extensionDatabaseIsEmtpy(): Boolean { fun getExtensionList(offline: Boolean = false): List { // update if 60 seconds has passed or requested offline and database is empty if (Data.lastExtensionCheck + 60 * 1000 < System.currentTimeMillis() || (offline && extensionDatabaseIsEmtpy())) { - println("Getting extensions list from the internet") + logger.info("Getting extensions list from the internet") Data.lastExtensionCheck = System.currentTimeMillis() var foundExtensions: List runBlocking { @@ -66,7 +69,7 @@ fun getExtensionList(offline: Boolean = false): List { } } } else { - println("used cached extension list") + logger.info("used cached extension list") } return transaction { diff --git a/server/src/main/kotlin/ir/armor/tachidesk/util/SourceList.kt b/server/src/main/kotlin/ir/armor/tachidesk/util/SourceList.kt index 8986ff7..3aa99e3 100644 --- a/server/src/main/kotlin/ir/armor/tachidesk/util/SourceList.kt +++ b/server/src/main/kotlin/ir/armor/tachidesk/util/SourceList.kt @@ -12,6 +12,7 @@ import ir.armor.tachidesk.database.entity.ExtensionEntity import ir.armor.tachidesk.database.entity.SourceEntity import ir.armor.tachidesk.database.table.ExtensionTable import ir.armor.tachidesk.database.table.SourceTable +import mu.KotlinLogging import org.jetbrains.exposed.sql.select import org.jetbrains.exposed.sql.selectAll import org.jetbrains.exposed.sql.transactions.transaction @@ -19,6 +20,8 @@ import java.lang.NullPointerException import java.net.URL import java.net.URLClassLoader +private val logger = KotlinLogging.logger {} + private val sourceCache = mutableListOf>() private val extensionCache = mutableListOf>() @@ -29,10 +32,11 @@ fun getHttpSource(sourceId: Long): HttpSource { val cachedResult: Pair? = sourceCache.firstOrNull { it.first == sourceId } if (cachedResult != null) { - println("used cached HttpSource: ${cachedResult.second.name}") + logger.info("used cached HttpSource: ${cachedResult.second.name}") return cachedResult.second } + val result: HttpSource = transaction { val extensionId = sourceRecord.extension.id.value val extensionRecord = ExtensionEntity.findById(extensionId)!! @@ -41,17 +45,15 @@ fun getHttpSource(sourceId: Long): HttpSource { val jarName = apkName.substringBefore(".apk") + ".jar" val jarPath = "${applicationDirs.extensionsRoot}/$jarName" - println(jarName) - val cachedExtensionPair = extensionCache.firstOrNull { it.first == jarPath } var usedCached = false val instance = if (cachedExtensionPair != null) { usedCached = true - println("Used cached Extension") + logger.info("Used cached Extension") cachedExtensionPair.second } else { - println("No Extension cache") + logger.info("No Extension cache") val child = URLClassLoader(arrayOf(URL("file:$jarPath")), this::class.java.classLoader) val classToLoad = Class.forName(className, true, child) classToLoad.newInstance()