Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change darwin impl to delegate to os_log_t #348

Merged
merged 6 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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")
}
}
oshai marked this conversation as resolved.
Show resolved Hide resolved

override fun isLoggingEnabledFor(level: Level, marker: Marker?): Boolean {
return when (level) {
Level.OFF -> false
else -> os_log_type_enabled(underlyingLogger, 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,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("."))
oshai marked this conversation as resolved.
Show resolved Hide resolved
)
}
else -> {
DarwinKLogger(name, os_log_create(name, ""))
}
}
}
}
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 ?: ""
}
Loading