Skip to content

Commit

Permalink
change darwin impl to delegate to os_log_t
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.
  • Loading branch information
oshai committed Aug 8, 2023
1 parent c6eaceb commit 9511bc3
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 78 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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<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, 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())
}
}
}
Original file line number Diff line number Diff line change
@@ -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.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package io.github.oshai.kotlinlogging.internal

internal actual typealias ErrorMessageProducer = DefaultErrorMessageProducer
Original file line number Diff line number Diff line change
@@ -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"))
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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 9511bc3

Please sign in to comment.