better loging

This commit is contained in:
Aria Moradi 2021-03-25 16:53:36 +04:30
parent 1c0c09f2f2
commit 183972475b
4 changed files with 31 additions and 28 deletions

View File

@ -33,11 +33,14 @@ import ir.armor.tachidesk.util.sourceFilters
import ir.armor.tachidesk.util.sourceGlobalSearch import ir.armor.tachidesk.util.sourceGlobalSearch
import ir.armor.tachidesk.util.sourceSearch import ir.armor.tachidesk.util.sourceSearch
import ir.armor.tachidesk.util.updateCategory import ir.armor.tachidesk.util.updateCategory
import mu.KLogging
class Main { class Main {
companion object { companion object : KLogging() {
@JvmStatic @JvmStatic
fun main(args: Array<String>) { fun main(args: Array<String>) {
serverSetup() serverSetup()
var hasWebUiBundled: Boolean = false var hasWebUiBundled: Boolean = false
@ -49,7 +52,7 @@ class Main {
config.addStaticFiles("/react") config.addStaticFiles("/react")
config.addSinglePageRoot("/", "/react/index.html") config.addSinglePageRoot("/", "/react/index.html")
} catch (e: RuntimeException) { } catch (e: RuntimeException) {
println("Warning: react build files are missing.") logger.warn("react build files are missing.")
hasWebUiBundled = false hasWebUiBundled = false
} }
config.enableCorsForAllOrigins() config.enableCorsForAllOrigins()
@ -68,7 +71,6 @@ class Main {
app.get("/api/v1/extension/install/:apkName") { ctx -> app.get("/api/v1/extension/install/:apkName") { ctx ->
val apkName = ctx.pathParam("apkName") val apkName = ctx.pathParam("apkName")
println("installing $apkName")
ctx.status( ctx.status(
installAPK(apkName) installAPK(apkName)
@ -77,7 +79,7 @@ class Main {
app.get("/api/v1/extension/uninstall/:apkName") { ctx -> app.get("/api/v1/extension/uninstall/:apkName") { ctx ->
val apkName = ctx.pathParam("apkName") val apkName = ctx.pathParam("apkName")
println("uninstalling $apkName")
removeExtension(apkName) removeExtension(apkName)
ctx.status(200) ctx.status(200)
} }

View File

@ -15,6 +15,7 @@ import ir.armor.tachidesk.applicationDirs
import ir.armor.tachidesk.database.table.ExtensionTable import ir.armor.tachidesk.database.table.ExtensionTable
import ir.armor.tachidesk.database.table.SourceTable import ir.armor.tachidesk.database.table.SourceTable
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import mu.KotlinLogging
import okhttp3.Request import okhttp3.Request
import okio.buffer import okio.buffer
import okio.sink import okio.sink
@ -29,7 +30,10 @@ import java.io.InputStream
import java.net.URL import java.net.URL
import java.net.URLClassLoader import java.net.URLClassLoader
private val logger = KotlinLogging.logger {}
fun installAPK(apkName: String): Int { fun installAPK(apkName: String): Int {
logger.info("Installing $apkName")
val extensionRecord = getExtensionList(true).first { it.apkName == apkName } val extensionRecord = getExtensionList(true).first { it.apkName == apkName }
val fileNameWithoutType = apkName.substringBefore(".apk") val fileNameWithoutType = apkName.substringBefore(".apk")
val dirPathWithoutType = "${applicationDirs.extensionsRoot}/$fileNameWithoutType" val dirPathWithoutType = "${applicationDirs.extensionsRoot}/$fileNameWithoutType"
@ -49,7 +53,7 @@ fun installAPK(apkName: String): Int {
downloadAPKFile(apkToDownload, apkFilePath) downloadAPKFile(apkToDownload, apkFilePath)
val className: String = APKExtractor.extract_dex_and_read_className(apkFilePath, dexFilePath) val className: String = APKExtractor.extract_dex_and_read_className(apkFilePath, dexFilePath)
println(className) logger.info(className)
// dex -> jar // dex -> jar
Dex2jarCmd.main(dexFilePath, "-o", jarFilePath, "--force") Dex2jarCmd.main(dexFilePath, "-o", jarFilePath, "--force")
@ -69,11 +73,6 @@ fun installAPK(apkName: String): Int {
if (instance is HttpSource) { // single source if (instance is HttpSource) { // single source
val httpSource = instance as HttpSource val httpSource = instance as HttpSource
transaction { 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) { if (SourceTable.select { SourceTable.id eq httpSource.id }.count() == 0L) {
SourceTable.insert { SourceTable.insert {
it[this.id] = httpSource.id it[this.id] = httpSource.id
@ -82,9 +81,7 @@ fun installAPK(apkName: String): Int {
it[extension] = extensionId it[extension] = extensionId
} }
} }
// println(httpSource.id) logger.info("Installed source ${httpSource.name} with id:${httpSource.id}")
// println(httpSource.name)
// println()
} }
} else { // multi source } else { // multi source
val sourceFactory = instance as SourceFactory val sourceFactory = instance as SourceFactory
@ -101,9 +98,7 @@ fun installAPK(apkName: String): Int {
it[positionInFactorySource] = index it[positionInFactorySource] = index
} }
} }
// println(httpSource.id) logger.info("Installed source ${httpSource.name} with id:${httpSource.id}")
// println(httpSource.name)
// println()
} }
} }
} }
@ -134,9 +129,11 @@ private fun downloadAPKFile(url: String, apkPath: String) {
sink.close() sink.close()
} }
fun removeExtension(pkgName: String) { fun removeExtension(apkName: String) {
val extensionRecord = getExtensionList(true).first { it.apkName == pkgName } logger.info("Uninstalling $apkName")
val fileNameWithoutType = pkgName.substringBefore(".apk")
val extensionRecord = getExtensionList(true).first { it.apkName == apkName }
val fileNameWithoutType = apkName.substringBefore(".apk")
val jarPath = "${applicationDirs.extensionsRoot}/$fileNameWithoutType.jar" val jarPath = "${applicationDirs.extensionsRoot}/$fileNameWithoutType.jar"
transaction { transaction {
val extensionId = ExtensionTable.select { ExtensionTable.name eq extensionRecord.name }.first()[ExtensionTable.id] val extensionId = ExtensionTable.select { ExtensionTable.name eq extensionRecord.name }.first()[ExtensionTable.id]
@ -158,9 +155,8 @@ fun getExtensionIcon(apkName: String): Pair<InputStream, String> {
val iconUrl = transaction { ExtensionTable.select { ExtensionTable.apkName eq apkName }.firstOrNull()!! }[ExtensionTable.iconUrl] val iconUrl = transaction { ExtensionTable.select { ExtensionTable.apkName eq apkName }.firstOrNull()!! }[ExtensionTable.iconUrl]
val saveDir = "${applicationDirs.extensionsRoot}/icon" val saveDir = "${applicationDirs.extensionsRoot}/icon"
val fileName = apkName
return getCachedResponse(saveDir, fileName) { return getCachedResponse(saveDir, apkName) {
network.client.newCall( network.client.newCall(
GET(iconUrl) GET(iconUrl)
).execute() ).execute()

View File

@ -9,12 +9,15 @@ import eu.kanade.tachiyomi.extension.model.Extension
import ir.armor.tachidesk.database.dataclass.ExtensionDataClass import ir.armor.tachidesk.database.dataclass.ExtensionDataClass
import ir.armor.tachidesk.database.table.ExtensionTable import ir.armor.tachidesk.database.table.ExtensionTable
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import mu.KotlinLogging
import org.jetbrains.exposed.sql.insert import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.select import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.update import org.jetbrains.exposed.sql.update
private val logger = KotlinLogging.logger {}
private object Data { private object Data {
var lastExtensionCheck: Long = 0 var lastExtensionCheck: Long = 0
} }
@ -28,7 +31,7 @@ private fun extensionDatabaseIsEmtpy(): Boolean {
fun getExtensionList(offline: Boolean = false): List<ExtensionDataClass> { fun getExtensionList(offline: Boolean = false): List<ExtensionDataClass> {
// update if 60 seconds has passed or requested offline and database is empty // update if 60 seconds has passed or requested offline and database is empty
if (Data.lastExtensionCheck + 60 * 1000 < System.currentTimeMillis() || (offline && extensionDatabaseIsEmtpy())) { 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() Data.lastExtensionCheck = System.currentTimeMillis()
var foundExtensions: List<Extension.Available> var foundExtensions: List<Extension.Available>
runBlocking { runBlocking {
@ -66,7 +69,7 @@ fun getExtensionList(offline: Boolean = false): List<ExtensionDataClass> {
} }
} }
} else { } else {
println("used cached extension list") logger.info("used cached extension list")
} }
return transaction { return transaction {

View File

@ -12,6 +12,7 @@ import ir.armor.tachidesk.database.entity.ExtensionEntity
import ir.armor.tachidesk.database.entity.SourceEntity import ir.armor.tachidesk.database.entity.SourceEntity
import ir.armor.tachidesk.database.table.ExtensionTable import ir.armor.tachidesk.database.table.ExtensionTable
import ir.armor.tachidesk.database.table.SourceTable import ir.armor.tachidesk.database.table.SourceTable
import mu.KotlinLogging
import org.jetbrains.exposed.sql.select import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
@ -19,6 +20,8 @@ import java.lang.NullPointerException
import java.net.URL import java.net.URL
import java.net.URLClassLoader import java.net.URLClassLoader
private val logger = KotlinLogging.logger {}
private val sourceCache = mutableListOf<Pair<Long, HttpSource>>() private val sourceCache = mutableListOf<Pair<Long, HttpSource>>()
private val extensionCache = mutableListOf<Pair<String, Any>>() private val extensionCache = mutableListOf<Pair<String, Any>>()
@ -29,10 +32,11 @@ fun getHttpSource(sourceId: Long): HttpSource {
val cachedResult: Pair<Long, HttpSource>? = sourceCache.firstOrNull { it.first == sourceId } val cachedResult: Pair<Long, HttpSource>? = sourceCache.firstOrNull { it.first == sourceId }
if (cachedResult != null) { if (cachedResult != null) {
println("used cached HttpSource: ${cachedResult.second.name}") logger.info("used cached HttpSource: ${cachedResult.second.name}")
return cachedResult.second return cachedResult.second
} }
val result: HttpSource = transaction { val result: HttpSource = transaction {
val extensionId = sourceRecord.extension.id.value val extensionId = sourceRecord.extension.id.value
val extensionRecord = ExtensionEntity.findById(extensionId)!! val extensionRecord = ExtensionEntity.findById(extensionId)!!
@ -41,17 +45,15 @@ fun getHttpSource(sourceId: Long): HttpSource {
val jarName = apkName.substringBefore(".apk") + ".jar" val jarName = apkName.substringBefore(".apk") + ".jar"
val jarPath = "${applicationDirs.extensionsRoot}/$jarName" val jarPath = "${applicationDirs.extensionsRoot}/$jarName"
println(jarName)
val cachedExtensionPair = extensionCache.firstOrNull { it.first == jarPath } val cachedExtensionPair = extensionCache.firstOrNull { it.first == jarPath }
var usedCached = false var usedCached = false
val instance = val instance =
if (cachedExtensionPair != null) { if (cachedExtensionPair != null) {
usedCached = true usedCached = true
println("Used cached Extension") logger.info("Used cached Extension")
cachedExtensionPair.second cachedExtensionPair.second
} else { } else {
println("No Extension cache") logger.info("No Extension cache")
val child = URLClassLoader(arrayOf<URL>(URL("file:$jarPath")), this::class.java.classLoader) val child = URLClassLoader(arrayOf<URL>(URL("file:$jarPath")), this::class.java.classLoader)
val classToLoad = Class.forName(className, true, child) val classToLoad = Class.forName(className, true, child)
classToLoad.newInstance() classToLoad.newInstance()