Skip to content

Commit

Permalink
🔊 Adding logs to aid diagnostic applications (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
guiyanakuang authored Nov 20, 2023
1 parent 7042d43 commit 35c6bb7
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 30 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ captures
!*.xcodeproj/project.xcworkspace/
!*.xcworkspace/contents.xcworkspacedata
**/xcshareddata/WorkspaceSettings.xcsettings
logs
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ plugins {
// in each subproject's classloader
alias(libs.plugins.jetbrainsCompose) apply false
alias(libs.plugins.kotlinMultiplatform) apply false
alias(libs.plugins.kotlinSerialization) apply false
}
11 changes: 8 additions & 3 deletions composeApp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ kotlin {
implementation("net.java.dev.jna:jna-platform:5.13.0")
implementation("com.google.zxing:core:3.5.2")
implementation("com.google.zxing:javase:3.5.2")
implementation("ch.qos.logback:logback-classic:1.4.11")
}
commonMain.dependencies {
implementation(compose.runtime)
Expand All @@ -27,6 +28,7 @@ kotlin {
@OptIn(ExperimentalComposeLibrary::class)
implementation(compose.components.resources)
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.6.1")
implementation("io.github.oshai:kotlin-logging-jvm:5.1.0")
}

val commonTest by getting {
Expand All @@ -44,15 +46,18 @@ compose.desktop {

nativeDistributions {
targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
packageName = "com.clipevery"

packageName = "Clipevery"
packageVersion = "1.0.0"
macOS {
bundleID = "com.clipevery.desktop"
bundleID = "com.clipevery"
appCategory = "public.app-category.utilities"
infoPlist {
dockName = "Clipevery"
extraKeysRawXml = """
<key>LSUIElement</key>
<string>true</string>
""".trimIndent()
"""
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.clipevery.config

enum class ConfigType {
enum class FileType {
USER,
SYSTEM
APP,
LOG
}
3 changes: 3 additions & 0 deletions composeApp/src/commonMain/kotlin/com/clipevery/log/Logger.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.clipevery.log

expect fun initLogger(logPath: String)
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.clipevery.path

import com.clipevery.config.ConfigType
import com.clipevery.config.FileType
import java.nio.file.Path

interface PathProvider {
fun resolve(configName: String, configType: ConfigType): Path {
return when (configType) {
ConfigType.USER -> resolveUser(configName)
ConfigType.SYSTEM -> resolveApp(configName)
fun resolve(fileName: String, fileType: FileType): Path {
return when (fileType) {
FileType.USER -> resolveUser(fileName)
FileType.APP -> resolveApp(fileName)
FileType.LOG -> resolveLog(fileName)
}
}

fun resolveUser(configName: String): Path
fun resolveUser(fileName: String?): Path

fun resolveApp(configName: String): Path
fun resolveApp(fileName: String?): Path

fun resolveLog(fileName: String?): Path
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.clipevery.presist

import com.clipevery.config.ConfigType
import com.clipevery.config.FileType
import com.clipevery.path.PathProvider
import java.nio.file.Path

interface FilePersist {

val pathProvider: PathProvider

fun getPersist(configName: String, configType: ConfigType): OneFilePersist {
return createOneFilePersist(pathProvider.resolve(configName, configType))
fun getPersist(configName: String, fileType: FileType): OneFilePersist {
return createOneFilePersist(pathProvider.resolve(configName, fileType))
}

fun createOneFilePersist(path: Path): OneFilePersist
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.clipevery.log

import ch.qos.logback.classic.Level
import ch.qos.logback.classic.LoggerContext
import ch.qos.logback.classic.encoder.PatternLayoutEncoder
import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.core.rolling.RollingFileAppender
import ch.qos.logback.core.rolling.TimeBasedRollingPolicy
import org.slf4j.Logger
import org.slf4j.LoggerFactory

actual fun initLogger(logPath: String) {
val context = LoggerFactory.getILoggerFactory() as LoggerContext

val encoder = PatternLayoutEncoder()
encoder.context = context
encoder.pattern = "%date %level [%thread] %logger{10} [%file:%line] %msg%n"
encoder.start()

val rollingFileAppender = RollingFileAppender<ILoggingEvent>()
rollingFileAppender.context = context
rollingFileAppender.file = logPath
rollingFileAppender.encoder = encoder

val rollingPolicy = TimeBasedRollingPolicy<ILoggingEvent>()
rollingPolicy.context = context
rollingPolicy.setParent(rollingFileAppender)
rollingPolicy.fileNamePattern = "$logPath.%d{yyyy-MM-dd}.log"
rollingPolicy.maxHistory = 30
rollingPolicy.start()

rollingFileAppender.rollingPolicy = rollingPolicy
rollingFileAppender.start()

val rootLogger = context.getLogger(Logger.ROOT_LOGGER_NAME) as ch.qos.logback.classic.Logger
rootLogger.level = Level.DEBUG
rootLogger.addAppender(rollingFileAppender)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.clipevery.path

import com.clipevery.platform.currentPlatform
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths

Expand All @@ -23,38 +24,73 @@ class WindowsPathProvider: PathProvider {

private val userDir = System.getProperty("user.dir")

override fun resolveUser(configName: String): Path {
return Paths.get(userHomePath).resolve(".clipevery").resolve(configName)
override fun resolveUser(fileName: String?): Path {
return fileName?.let {
Paths.get(userHomePath).resolve(".clipevery").resolve(fileName)
} ?: Paths.get(userHomePath).resolve(".clipevery")
}

override fun resolveApp(configName: String): Path {
return Paths.get(userDir).resolve(configName)
override fun resolveApp(fileName: String?): Path {
return fileName?.let {
Paths.get(userDir).resolve(fileName)
} ?: Paths.get(userDir)
}

override fun resolveLog(fileName: String?): Path {
return fileName?.let {
Paths.get(userDir).resolve("logs").resolve(fileName)
} ?: Paths.get(userDir).resolve("logs")
}
}


class MacosPathProvider: PathProvider {

private val userHomePath = System.getProperty("user.home")
private val appPath = System.getProperty("jpackage.app-path")

private val userDir = System.getProperty("user.dir")
private fun getAppSupportPath(): Path {
val userHome = System.getProperty("user.home")
val appSupportPath = Paths.get(userHome, "Library", "Application Support", "Clipevery")

override fun resolveUser(configName: String): Path {
return Paths.get(userHomePath).resolve(".clipevery").resolve(configName)
if (Files.notExists(appSupportPath)) {
Files.createDirectories(appSupportPath)
}

return appSupportPath
}

override fun resolveApp(configName: String): Path {
return Paths.get(userDir).resolve(configName)
override fun resolveUser(fileName: String?): Path {
return fileName?.let {
getAppSupportPath().resolve(fileName)
} ?: getAppSupportPath()
}

override fun resolveApp(fileName: String?): Path {
val appPath = Paths.get(appPath).parent.parent

return fileName?.let {
appPath.resolve(fileName)
} ?: appPath
}

override fun resolveLog(fileName: String?): Path {
return fileName?.let {
getAppSupportPath().resolve("logs").resolve(fileName)
} ?: getAppSupportPath().resolve("logs")
}
}

class LinuxPathProvider: PathProvider {

override fun resolveUser(configName: String): Path {
override fun resolveUser(fileName: String?): Path {
TODO("Not yet implemented")
}

override fun resolveApp(fileName: String?): Path {
TODO("Not yet implemented")
}

override fun resolveApp(configName: String): Path {
override fun resolveLog(fileName: String?): Path {
TODO("Not yet implemented")
}
}
24 changes: 21 additions & 3 deletions composeApp/src/desktopMain/kotlin/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,29 @@ import com.clipevery.AppConfig
import com.clipevery.ClipeveryApp
import com.clipevery.Dependencies
import com.clipevery.config.ConfigManager
import com.clipevery.config.ConfigType
import com.clipevery.config.FileType
import com.clipevery.log.initLogger
import com.clipevery.net.ClipServer
import com.clipevery.path.PathProvider
import com.clipevery.path.getPathProvider
import com.clipevery.presist.DesktopOneFilePersist
import com.clipevery.presist.FilePersist
import com.clipevery.presist.OneFilePersist
import com.clipevery.utils.ioDispatcher
import io.github.oshai.kotlinlogging.KotlinLogging
import kotlinx.coroutines.CoroutineScope
import java.nio.file.Path
import kotlin.io.path.pathString
import kotlin.system.exitProcess


fun main() = application {
val pathProvider = getPathProvider()
initLogger(pathProvider.resolveLog("clipevery.log").pathString)
val logger = KotlinLogging.logger {}

logger.info { "Starting Clipevery" }

val ioScope = rememberCoroutineScope { ioDispatcher }

val dependencies = remember {
Expand Down Expand Up @@ -49,11 +58,20 @@ fun main() = application {
private fun getDependencies(
ioScope: CoroutineScope
) = object : Dependencies() {

val logger = KotlinLogging.logger {}

override val clipServer: ClipServer = object : ClipServer {
}

override val filePersist: FilePersist = object : FilePersist {
override val pathProvider: PathProvider = getPathProvider()
override val pathProvider: PathProvider = run {
val innerPathProvider = getPathProvider()
logger.info { "userPath ${innerPathProvider.resolveUser(null)}" }
logger.info { "appPath ${innerPathProvider.resolveApp(null)}" }
logger.info { "logPath ${innerPathProvider.resolveLog(null)}" }
getPathProvider()
}

override fun createOneFilePersist(path: Path): OneFilePersist {
return DesktopOneFilePersist(path)
Expand All @@ -62,7 +80,7 @@ private fun getDependencies(

override val configManager: ConfigManager = object : ConfigManager(ioScope) {

val configFilePersist = filePersist.getPersist("appConfig.json", ConfigType.USER)
val configFilePersist = filePersist.getPersist("appConfig.json", FileType.USER)

override fun loadConfig(): AppConfig? {
return configFilePersist.readAs(AppConfig::class)
Expand Down

0 comments on commit 35c6bb7

Please sign in to comment.