From 7e7e069244d273801e897e779a7271c7342825df Mon Sep 17 00:00:00 2001 From: Aria Moradi Date: Thu, 20 May 2021 13:48:33 +0430 Subject: [PATCH] - Set log level eairlier - Set AndroidCompat's data root properly --- .../xyz/nulldev/ts/config/ConfigManager.kt | 22 +++++++++++++++---- .../java/xyz/nulldev/ts/config/Logging.kt | 20 +++++++++++++++++ .../ir/armor/tachidesk/server/ServerConfig.kt | 4 +++- .../ir/armor/tachidesk/server/ServerSetup.kt | 8 +------ 4 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 AndroidCompat/Config/src/main/java/xyz/nulldev/ts/config/Logging.kt diff --git a/AndroidCompat/Config/src/main/java/xyz/nulldev/ts/config/ConfigManager.kt b/AndroidCompat/Config/src/main/java/xyz/nulldev/ts/config/ConfigManager.kt index 817083e..43c3cd9 100644 --- a/AndroidCompat/Config/src/main/java/xyz/nulldev/ts/config/ConfigManager.kt +++ b/AndroidCompat/Config/src/main/java/xyz/nulldev/ts/config/ConfigManager.kt @@ -7,6 +7,7 @@ package xyz.nulldev.ts.config * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +import ch.qos.logback.classic.Level import com.typesafe.config.Config import com.typesafe.config.ConfigFactory import com.typesafe.config.ConfigRenderOptions @@ -41,21 +42,34 @@ open class ConfigManager { */ fun loadConfigs(): Config { //Load reference configs - val compatConfig = ConfigFactory.parseResources("compat-reference.conf") + val compatConfig = ConfigFactory.parseResources("compat-reference.conf") val serverConfig = ConfigFactory.parseResources("server-reference.conf") + val baseConfig = + ConfigFactory.parseMap( + mapOf( + "ts.server.rootDir" to ApplicationRootDir + ) + ) //Load user config val userConfig = - File(ApplicationRootDir, "server.conf").let { - ConfigFactory.parseFile(it) - } + File(ApplicationRootDir, "server.conf").let { + ConfigFactory.parseFile(it) + } + val config = ConfigFactory.empty() + .withFallback(baseConfig) .withFallback(userConfig) .withFallback(compatConfig) .withFallback(serverConfig) .resolve() + // set log level early + if (debugLogsEnabled(config)) { + setLogLevel(Level.DEBUG) + } + logger.debug { "Loaded config:\n" + config.root().render(ConfigRenderOptions.concise().setFormatted(true)) } diff --git a/AndroidCompat/Config/src/main/java/xyz/nulldev/ts/config/Logging.kt b/AndroidCompat/Config/src/main/java/xyz/nulldev/ts/config/Logging.kt new file mode 100644 index 0000000..a7ea068 --- /dev/null +++ b/AndroidCompat/Config/src/main/java/xyz/nulldev/ts/config/Logging.kt @@ -0,0 +1,20 @@ +package xyz.nulldev.ts.config + +/* + * Copyright (C) Contributors to the Suwayomi project + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +import ch.qos.logback.classic.Level +import com.typesafe.config.Config +import mu.KotlinLogging +import org.slf4j.Logger + +fun setLogLevel(level: Level) { + (KotlinLogging.logger(Logger.ROOT_LOGGER_NAME).underlyingLogger as ch.qos.logback.classic.Logger).level = level +} + +fun debugLogsEnabled(config: Config) + = System.getProperty("ir.armor.tachidesk.debugLogsEnabled", config.getString("server.debugLogsEnabled")).toBoolean() \ No newline at end of file diff --git a/server/src/main/kotlin/ir/armor/tachidesk/server/ServerConfig.kt b/server/src/main/kotlin/ir/armor/tachidesk/server/ServerConfig.kt index 9750f64..1320ed4 100644 --- a/server/src/main/kotlin/ir/armor/tachidesk/server/ServerConfig.kt +++ b/server/src/main/kotlin/ir/armor/tachidesk/server/ServerConfig.kt @@ -10,6 +10,8 @@ package ir.armor.tachidesk.server import com.typesafe.config.Config import io.github.config4k.getValue import xyz.nulldev.ts.config.ConfigModule +import xyz.nulldev.ts.config.GlobalConfigManager +import xyz.nulldev.ts.config.debugLogsEnabled class ServerConfig(config: Config) : ConfigModule(config) { val ip: String by config @@ -21,7 +23,7 @@ class ServerConfig(config: Config) : ConfigModule(config) { val socksProxyPort: String by config // misc - val debugLogsEnabled: Boolean = System.getProperty("ir.armor.tachidesk.debugLogsEnabled", config.getString("debugLogsEnabled")).toBoolean() + val debugLogsEnabled: Boolean = debugLogsEnabled(GlobalConfigManager.config) val systemTrayEnabled: Boolean by config val initialOpenInBrowserEnabled: Boolean by config diff --git a/server/src/main/kotlin/ir/armor/tachidesk/server/ServerSetup.kt b/server/src/main/kotlin/ir/armor/tachidesk/server/ServerSetup.kt index 09d5939..153e9fc 100644 --- a/server/src/main/kotlin/ir/armor/tachidesk/server/ServerSetup.kt +++ b/server/src/main/kotlin/ir/armor/tachidesk/server/ServerSetup.kt @@ -7,7 +7,6 @@ package ir.armor.tachidesk.server * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -import ch.qos.logback.classic.Level import eu.kanade.tachiyomi.App import ir.armor.tachidesk.model.database.databaseUp import ir.armor.tachidesk.server.util.AppMutex.handleAppMutex @@ -17,7 +16,6 @@ import org.kodein.di.DI import org.kodein.di.bind import org.kodein.di.conf.global import org.kodein.di.singleton -import org.slf4j.Logger import xyz.nulldev.androidcompat.AndroidCompat import xyz.nulldev.androidcompat.AndroidCompatInitializer import xyz.nulldev.ts.config.ApplicationRootDir @@ -67,6 +65,7 @@ fun applicationSetup() { ServerConfig.register(GlobalConfigManager.config) ) + // Make sure only one instance of the app is running handleAppMutex() // Load config API @@ -76,11 +75,6 @@ fun applicationSetup() { // start app androidCompat.startApp(App()) - // set application wide logging level - if (serverConfig.debugLogsEnabled) { - (KotlinLogging.logger(Logger.ROOT_LOGGER_NAME).underlyingLogger as ch.qos.logback.classic.Logger).level = Level.DEBUG - } - // create conf file if doesn't exist try { val dataConfFile = File("${applicationDirs.dataRoot}/server.conf")