Skip to content

Commit

Permalink
change darwin impl to delegate to os_log_t (#348)
Browse files Browse the repository at this point in the history
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.

* change subsystem and category resolution based on #227
* add constant logger
* add constant constantOsDefaultLogger
  • Loading branch information
oshai committed Aug 9, 2023
1 parent c6eaceb commit b1e8e8b
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 78 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Expand Up @@ -166,7 +166,7 @@ kotlin {
dependsOn(nativeMain)
}
val darwinMain by creating {
dependsOn(nativeMain)
dependsOn(commonMain)
}
linuxTargets.forEach {
getByName("${it.targetName}Main") {
Expand Down
@@ -0,0 +1,42 @@
package io.github.oshai.kotlinlogging

import kotlinx.cinterop.ptr
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<os_log_t> {

override fun at(level: Level, marker: Marker?, block: KLoggingEventBuilder.() -> Unit) {
if (isLoggingEnabledFor(level, marker)) {
KLoggingEventBuilder().apply(block).run {
_os_log_internal(__dso_handle.ptr, underlyingLogger, 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(underlyingLogger, level.toDarwinLevel())
}
}
}
@@ -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<String?> = AtomicReference(null)
public var category: AtomicReference<String?> = AtomicReference(null)
}

This file was deleted.

This file was deleted.

@@ -0,0 +1,3 @@
package io.github.oshai.kotlinlogging.internal

internal actual typealias ErrorMessageProducer = DefaultErrorMessageProducer
@@ -0,0 +1,42 @@
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 kotlin.native.concurrent.AtomicReference
import platform.darwin.OS_LOG_DEFAULT
import platform.darwin.os_log_create

/** factory methods to obtain a [KLogger] */
internal actual object KLoggerFactory {

private val constantLogger: AtomicReference<KLogger?> = AtomicReference(null)
private val constantOsDefaultLogger: KLogger = DarwinKLogger("", OS_LOG_DEFAULT)

/** 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 -> {
constantLogger.value
?: DarwinKLogger(name, os_log_create(subsystemConfigured, categoryConfigured)).also {
constantLogger.value = it
}
}
subsystemConfigured != null || categoryConfigured != null -> {
DarwinKLogger(name, os_log_create(subsystemConfigured ?: name, categoryConfigured ?: name))
}
name.isBlank() -> constantOsDefaultLogger
name.contains(".") -> {
DarwinKLogger(
name,
os_log_create(name.substringBeforeLast("."), name.substringAfterLast("."))
)
}
else -> {
DarwinKLogger(name, os_log_create(name, ""))
}
}
}
}
@@ -0,0 +1,6 @@
package io.github.oshai.kotlinlogging.internal

internal actual object KLoggerNameResolver {

internal actual fun name(func: () -> Unit): String = func::class.qualifiedName ?: ""
}

0 comments on commit b1e8e8b

Please sign in to comment.