From 219c0361393e9f973d3242e9b1a114403fddb878 Mon Sep 17 00:00:00 2001 From: Yiqun Zhang Date: Tue, 30 Jan 2024 15:57:08 +0800 Subject: [PATCH] :loud_sound: Set the log level for development debugging to info (#267) --- composeApp/build.gradle.kts | 2 + .../kotlin/com/clipevery/log/Logger.kt | 2 +- .../DesktopClipeveryKoinApplication.kt | 4 +- .../com/clipevery/log/Logger.desktop.kt | 15 ++- .../desktopMain/kotlin/com/clipevery/main.kt | 113 ++++++++++-------- .../clipevery/presist/DesktopFilePersist.kt | 4 + 6 files changed, 84 insertions(+), 56 deletions(-) diff --git a/composeApp/build.gradle.kts b/composeApp/build.gradle.kts index bf29d2554..83fed9312 100644 --- a/composeApp/build.gradle.kts +++ b/composeApp/build.gradle.kts @@ -66,6 +66,8 @@ compose.desktop { application { mainClass = "com.clipevery.MainKt" + args("info") + nativeDistributions { targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb) modules("jdk.charsets") diff --git a/composeApp/src/commonMain/kotlin/com/clipevery/log/Logger.kt b/composeApp/src/commonMain/kotlin/com/clipevery/log/Logger.kt index 8a3bc3008..3bcb8b9c0 100644 --- a/composeApp/src/commonMain/kotlin/com/clipevery/log/Logger.kt +++ b/composeApp/src/commonMain/kotlin/com/clipevery/log/Logger.kt @@ -1,3 +1,3 @@ package com.clipevery.log -expect fun initLogger(logPath: String) \ No newline at end of file +expect fun initLogger(logPath: String, logLevel: String) \ No newline at end of file diff --git a/composeApp/src/desktopMain/kotlin/com/clipevery/DesktopClipeveryKoinApplication.kt b/composeApp/src/desktopMain/kotlin/com/clipevery/DesktopClipeveryKoinApplication.kt index 678a51005..21832eac2 100644 --- a/composeApp/src/desktopMain/kotlin/com/clipevery/DesktopClipeveryKoinApplication.kt +++ b/composeApp/src/desktopMain/kotlin/com/clipevery/DesktopClipeveryKoinApplication.kt @@ -26,8 +26,8 @@ import com.clipevery.net.DesktopClipBonjourService import com.clipevery.net.DesktopClipClient import com.clipevery.net.DesktopClipServer import com.clipevery.path.getPathProvider -import com.clipevery.presist.DesktopFilePersist import com.clipevery.presist.FilePersist +import com.clipevery.presist.getFilePersist import com.clipevery.realm.RealmManager import com.clipevery.signal.DesktopPreKeyStore import com.clipevery.signal.DesktopSessionStore @@ -58,7 +58,7 @@ object Dependencies { // simple component single { DesktopAppInfoFactory(get()).createAppInfo() } single { DesktopEndpointInfoFactory( lazy { get() }) } - single { DesktopFilePersist() } + single { getFilePersist() } single { DefaultConfigManager(get().getPersist("appConfig.json", AppFileType.USER)) } single { DesktopQRCodeGenerator(get(), get()) } diff --git a/composeApp/src/desktopMain/kotlin/com/clipevery/log/Logger.desktop.kt b/composeApp/src/desktopMain/kotlin/com/clipevery/log/Logger.desktop.kt index a27439c00..0b82c59f8 100644 --- a/composeApp/src/desktopMain/kotlin/com/clipevery/log/Logger.desktop.kt +++ b/composeApp/src/desktopMain/kotlin/com/clipevery/log/Logger.desktop.kt @@ -9,7 +9,7 @@ import ch.qos.logback.core.rolling.TimeBasedRollingPolicy import org.slf4j.Logger import org.slf4j.LoggerFactory -actual fun initLogger(logPath: String) { +actual fun initLogger(logPath: String, logLevel: String) { val context = LoggerFactory.getILoggerFactory() as LoggerContext val encoder = PatternLayoutEncoder() @@ -33,9 +33,20 @@ actual fun initLogger(logPath: String) { rollingFileAppender.start() val rootLogger = context.getLogger(Logger.ROOT_LOGGER_NAME) as ch.qos.logback.classic.Logger - rootLogger.level = Level.DEBUG + rootLogger.level = getLevel(logLevel) rootLogger.addAppender(rollingFileAppender) val jThemeLogger = context.getLogger("com.jthemedetecor") as ch.qos.logback.classic.Logger jThemeLogger.level = Level.OFF +} + +fun getLevel(logLevel: String): Level { + return when (logLevel) { + "trace" -> Level.TRACE + "debug" -> Level.DEBUG + "info" -> Level.INFO + "warn" -> Level.WARN + "error" -> Level.ERROR + else -> Level.INFO + } } \ No newline at end of file diff --git a/composeApp/src/desktopMain/kotlin/com/clipevery/main.kt b/composeApp/src/desktopMain/kotlin/com/clipevery/main.kt index fa461335e..ed1af4e73 100644 --- a/composeApp/src/desktopMain/kotlin/com/clipevery/main.kt +++ b/composeApp/src/desktopMain/kotlin/com/clipevery/main.kt @@ -44,70 +44,81 @@ fun exitClipEveryApplication(exitApplication: () -> Unit) { exitApplication() } -fun main() = application { +fun main(args: Array) { + val logLevel = if (args.isNotEmpty()) { + args[0] + } else { + "info" + } val pathProvider = getPathProvider() - initLogger(pathProvider.resolve("clipevery.log", AppFileType.LOG).pathString) + initLogger(pathProvider.resolve("clipevery.log", AppFileType.LOG).pathString, logLevel) val logger = KotlinLogging.logger {} logger.info { "Starting Clipevery" } - val ioScope = rememberCoroutineScope { ioDispatcher } - val koinApplication = Dependencies.koinApplication initInject(koinApplication) - val appUI = koinApplication.koin.get() + application { + val ioScope = rememberCoroutineScope { ioDispatcher } - val trayIcon = if(currentPlatform().isMacos()) { - painterResource("clipevery_mac_tray.png") - } else { - painterResource("clipevery_icon.png") - } + val appUI = koinApplication.koin.get() - val windowState = rememberWindowState( - placement = WindowPlacement.Floating, - position = WindowPosition.PlatformDefault, - size = getPreferredWindowSize(appUI) - ) - - Tray( - icon = trayIcon, - mouseListener = getTrayMouseAdapter(windowState) { appUI.showWindow = !appUI.showWindow }, - ) - - val exitApplication: () -> Unit = { - appUI.showWindow = false - ioScope.launch { - exitClipEveryApplication { exitApplication() } + val trayIcon = if (currentPlatform().isMacos()) { + painterResource("clipevery_mac_tray.png") + } else { + painterResource("clipevery_icon.png") + } + + val windowState = rememberWindowState( + placement = WindowPlacement.Floating, + position = WindowPosition.PlatformDefault, + size = getPreferredWindowSize(appUI) + ) + + Tray( + icon = trayIcon, + mouseListener = getTrayMouseAdapter(windowState) { + appUI.showWindow = !appUI.showWindow + }, + ) + + val exitApplication: () -> Unit = { + appUI.showWindow = false + ioScope.launch { + exitClipEveryApplication { exitApplication() } + } } - } - Window( - onCloseRequest = exitApplication, - visible = appUI.showWindow, - state = windowState, - title = "Clipevery", - icon = painterResource("clipevery_icon.png"), - alwaysOnTop = true, - undecorated = true, - transparent = true, - resizable = false - ) { - - LaunchedEffect(Unit) { - window.addWindowFocusListener(object : java.awt.event.WindowFocusListener { - override fun windowGainedFocus(e: java.awt.event.WindowEvent?) { - appUI.showWindow = true - } - - override fun windowLostFocus(e: java.awt.event.WindowEvent?) { - appUI.showWindow = false - } - }) + Window( + onCloseRequest = exitApplication, + visible = appUI.showWindow, + state = windowState, + title = "Clipevery", + icon = painterResource("clipevery_icon.png"), + alwaysOnTop = true, + undecorated = true, + transparent = true, + resizable = false + ) { + + LaunchedEffect(Unit) { + window.addWindowFocusListener(object : java.awt.event.WindowFocusListener { + override fun windowGainedFocus(e: java.awt.event.WindowEvent?) { + appUI.showWindow = true + } + + override fun windowLostFocus(e: java.awt.event.WindowEvent?) { + appUI.showWindow = false + } + }) + } + ClipeveryApp( + koinApplication, + hideWindow = { appUI.showWindow = false }, + exitApplication = exitApplication + ) } - ClipeveryApp(koinApplication, - hideWindow = { appUI.showWindow = false }, - exitApplication = exitApplication) } } diff --git a/composeApp/src/desktopMain/kotlin/com/clipevery/presist/DesktopFilePersist.kt b/composeApp/src/desktopMain/kotlin/com/clipevery/presist/DesktopFilePersist.kt index 955946238..10c6357a5 100644 --- a/composeApp/src/desktopMain/kotlin/com/clipevery/presist/DesktopFilePersist.kt +++ b/composeApp/src/desktopMain/kotlin/com/clipevery/presist/DesktopFilePersist.kt @@ -4,6 +4,10 @@ import com.clipevery.path.PathProvider import com.clipevery.path.getPathProvider import java.nio.file.Path +fun getFilePersist(): FilePersist { + return DesktopFilePersist() +} + class DesktopFilePersist: FilePersist { override val pathProvider: PathProvider = getPathProvider()