From 9511bc37ac66f3e9a17c260ecd6171e7e6e5584e Mon Sep 17 00:00:00 2001 From: oshai Date: Wed, 9 Aug 2023 01:02:52 +0300 Subject: [PATCH] change darwin impl to delegate to os_log_t based on previous impl and https://blog.shipbook.io/ios-logs looks like a better approach will be to just delegate logs to the os logger. --- build.gradle.kts | 2 +- .../oshai/kotlinlogging/DarwinKLogger.kt | 43 +++++++++++++++++ .../KotlinLoggingConfiguration.kt | 7 ++- .../oshai/kotlinlogging/OSLogAppender.kt | 48 ------------------- .../kotlinlogging/OSLogSubsystemAppender.kt | 28 ----------- .../internal/ErrorMessageProducer.kt | 3 ++ .../kotlinlogging/internal/KLoggerFactory.kt | 30 ++++++++++++ .../internal/KLoggerNameResolver.kt | 6 +++ 8 files changed, 89 insertions(+), 78 deletions(-) create mode 100644 src/darwinMain/kotlin/io/github/oshai/kotlinlogging/DarwinKLogger.kt delete mode 100644 src/darwinMain/kotlin/io/github/oshai/kotlinlogging/OSLogAppender.kt delete mode 100644 src/darwinMain/kotlin/io/github/oshai/kotlinlogging/OSLogSubsystemAppender.kt create mode 100644 src/darwinMain/kotlin/io/github/oshai/kotlinlogging/internal/ErrorMessageProducer.kt create mode 100644 src/darwinMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerFactory.kt create mode 100644 src/darwinMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerNameResolver.kt diff --git a/build.gradle.kts b/build.gradle.kts index cc828dd4..8a628eb2 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -166,7 +166,7 @@ kotlin { dependsOn(nativeMain) } val darwinMain by creating { - dependsOn(nativeMain) + dependsOn(commonMain) } linuxTargets.forEach { getByName("${it.targetName}Main") { diff --git a/src/darwinMain/kotlin/io/github/oshai/kotlinlogging/DarwinKLogger.kt b/src/darwinMain/kotlin/io/github/oshai/kotlinlogging/DarwinKLogger.kt new file mode 100644 index 00000000..b8ea0cfe --- /dev/null +++ b/src/darwinMain/kotlin/io/github/oshai/kotlinlogging/DarwinKLogger.kt @@ -0,0 +1,43 @@ +package io.github.oshai.kotlinlogging + +import kotlinx.cinterop.ptr +import platform.darwin.OS_LOG_DEFAULT +import platform.darwin.OS_LOG_TYPE_DEBUG +import platform.darwin.OS_LOG_TYPE_DEFAULT +import platform.darwin.OS_LOG_TYPE_ERROR +import platform.darwin.OS_LOG_TYPE_INFO +import platform.darwin.__dso_handle +import platform.darwin._os_log_internal +import platform.darwin.os_log_t +import platform.darwin.os_log_type_enabled +import platform.darwin.os_log_type_t + +public class DarwinKLogger(override val name: String, override val underlyingLogger: os_log_t) : + KLogger, DelegatingKLogger { + + override fun at(level: Level, marker: Marker?, block: KLoggingEventBuilder.() -> Unit) { + if (isLoggingEnabledFor(level, marker)) { + KLoggingEventBuilder().apply(block).run { + _os_log_internal(__dso_handle.ptr, OS_LOG_DEFAULT, level.toDarwinLevel(), message) + } + } + } + + private fun Level.toDarwinLevel(): os_log_type_t { + return when (this) { + Level.TRACE -> OS_LOG_TYPE_DEBUG + Level.DEBUG -> OS_LOG_TYPE_DEBUG + Level.INFO -> OS_LOG_TYPE_INFO + Level.WARN -> OS_LOG_TYPE_DEFAULT + Level.ERROR -> OS_LOG_TYPE_ERROR + Level.OFF -> throw Exception("off level cannot be mapped to darwin level") + } + } + + override fun isLoggingEnabledFor(level: Level, marker: Marker?): Boolean { + return when (level) { + Level.OFF -> false + else -> os_log_type_enabled(OS_LOG_DEFAULT, level.toDarwinLevel()) + } + } +} diff --git a/src/darwinMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt b/src/darwinMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt index 3cb53b66..d9b2c927 100644 --- a/src/darwinMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt +++ b/src/darwinMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt @@ -1,3 +1,8 @@ package io.github.oshai.kotlinlogging -public actual val DefaultAppender: Appender = OSLogAppender() +import kotlin.native.concurrent.AtomicReference + +public object KotlinLoggingConfiguration { + public var subsystem: AtomicReference = AtomicReference(null) + public var category: AtomicReference = AtomicReference(null) +} diff --git a/src/darwinMain/kotlin/io/github/oshai/kotlinlogging/OSLogAppender.kt b/src/darwinMain/kotlin/io/github/oshai/kotlinlogging/OSLogAppender.kt deleted file mode 100644 index 59602be9..00000000 --- a/src/darwinMain/kotlin/io/github/oshai/kotlinlogging/OSLogAppender.kt +++ /dev/null @@ -1,48 +0,0 @@ -package io.github.oshai.kotlinlogging - -import kotlinx.cinterop.ptr -import platform.darwin.OS_LOG_DEFAULT -import platform.darwin.OS_LOG_TYPE_DEBUG -import platform.darwin.OS_LOG_TYPE_DEFAULT -import platform.darwin.OS_LOG_TYPE_ERROR -import platform.darwin.OS_LOG_TYPE_INFO -import platform.darwin.__dso_handle -import platform.darwin._os_log_internal -import platform.darwin.os_log_t -import platform.darwin.os_log_type_enabled -import platform.darwin.os_log_type_t - -public open class OSLogAppender : Appender { - override val includePrefix: Boolean = true - - protected open fun logger(loggerName: String): os_log_t { - return OS_LOG_DEFAULT - } - - private fun log(level: os_log_type_t, loggerName: String, message: String) { - val logger = logger(loggerName) - if (os_log_type_enabled(logger, level)) { - _os_log_internal(__dso_handle.ptr, logger, level, message) - } - } - - override fun trace(loggerName: String, message: String) { - log(OS_LOG_TYPE_DEBUG, loggerName, message) - } - - override fun debug(loggerName: String, message: String) { - log(OS_LOG_TYPE_DEBUG, loggerName, message) - } - - override fun info(loggerName: String, message: String) { - log(OS_LOG_TYPE_INFO, loggerName, message) - } - - override fun warn(loggerName: String, message: String) { - log(OS_LOG_TYPE_DEFAULT, loggerName, message) - } - - override fun error(loggerName: String, message: String) { - log(OS_LOG_TYPE_ERROR, loggerName, message) - } -} diff --git a/src/darwinMain/kotlin/io/github/oshai/kotlinlogging/OSLogSubsystemAppender.kt b/src/darwinMain/kotlin/io/github/oshai/kotlinlogging/OSLogSubsystemAppender.kt deleted file mode 100644 index bfa1a2d3..00000000 --- a/src/darwinMain/kotlin/io/github/oshai/kotlinlogging/OSLogSubsystemAppender.kt +++ /dev/null @@ -1,28 +0,0 @@ -package io.github.oshai.kotlinlogging - -import kotlin.native.concurrent.AtomicReference -import platform.darwin.os_log_create -import platform.darwin.os_log_t - -public class OSLogSubsystemAppender(public val subsystem: String) : OSLogAppender() { - override val includePrefix: Boolean = false - - private val logs: AtomicReference> = AtomicReference(mapOf()) - - override fun logger(loggerName: String): os_log_t { - var logger: os_log_t - do { - val existing = logs.value - logger = existing[loggerName] - if (logger != null) { - return logger - } - - val updated = existing.toMutableMap() - logger = os_log_create(subsystem, loggerName) - updated[loggerName] = logger - } while (!logs.compareAndSet(existing, updated)) - - return logger - } -} diff --git a/src/darwinMain/kotlin/io/github/oshai/kotlinlogging/internal/ErrorMessageProducer.kt b/src/darwinMain/kotlin/io/github/oshai/kotlinlogging/internal/ErrorMessageProducer.kt new file mode 100644 index 00000000..6d35a773 --- /dev/null +++ b/src/darwinMain/kotlin/io/github/oshai/kotlinlogging/internal/ErrorMessageProducer.kt @@ -0,0 +1,3 @@ +package io.github.oshai.kotlinlogging.internal + +internal actual typealias ErrorMessageProducer = DefaultErrorMessageProducer diff --git a/src/darwinMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerFactory.kt b/src/darwinMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerFactory.kt new file mode 100644 index 00000000..396fc90a --- /dev/null +++ b/src/darwinMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerFactory.kt @@ -0,0 +1,30 @@ +package io.github.oshai.kotlinlogging.internal + +import io.github.oshai.kotlinlogging.DarwinKLogger +import io.github.oshai.kotlinlogging.KLogger +import io.github.oshai.kotlinlogging.KotlinLoggingConfiguration +import platform.darwin.os_log_create + +/** factory methods to obtain a [KLogger] */ +internal actual object KLoggerFactory { + + /** get logger by explicit name */ + internal actual fun logger(name: String): KLogger { + val subsystemConfigured = KotlinLoggingConfiguration.subsystem.value + val categoryConfigured = KotlinLoggingConfiguration.category.value + return when { + subsystemConfigured != null || categoryConfigured != null -> { + DarwinKLogger(name, os_log_create(subsystemConfigured ?: name, categoryConfigured ?: name)) + } + name.contains(".") -> { + DarwinKLogger( + name, + os_log_create(name.substringBeforeLast("."), name.substringAfterLast(".")) + ) + } + else -> { + DarwinKLogger(name, os_log_create(name, "default")) + } + } + } +} diff --git a/src/darwinMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerNameResolver.kt b/src/darwinMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerNameResolver.kt new file mode 100644 index 00000000..fb420756 --- /dev/null +++ b/src/darwinMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerNameResolver.kt @@ -0,0 +1,6 @@ +package io.github.oshai.kotlinlogging.internal + +internal actual object KLoggerNameResolver { + + internal actual fun name(func: () -> Unit): String = func::class.qualifiedName ?: "" +}