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.sourceSearch
import ir.armor.tachidesk.util.updateCategory
import mu.KLogging
class Main {
companion object {
companion object : KLogging() {
@JvmStatic
fun main(args: Array<String>) {
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)
}

View File

@ -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<InputStream, String> {
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()

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.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<ExtensionDataClass> {
// 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<Extension.Available>
runBlocking {
@ -66,7 +69,7 @@ fun getExtensionList(offline: Boolean = false): List<ExtensionDataClass> {
}
}
} else {
println("used cached extension list")
logger.info("used cached extension list")
}
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.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<Pair<Long, HttpSource>>()
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 }
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>(URL("file:$jarPath")), this::class.java.classLoader)
val classToLoad = Class.forName(className, true, child)
classToLoad.newInstance()