-
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change darwin impl to delegate to os_log_t (#348)
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
Showing
8 changed files
with
100 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/darwinMain/kotlin/io/github/oshai/kotlinlogging/DarwinKLogger.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
|
||
override fun isLoggingEnabledFor(level: Level, marker: Marker?): Boolean { | ||
return when (level) { | ||
Level.OFF -> false | ||
else -> os_log_type_enabled(underlyingLogger, level.toDarwinLevel()) | ||
} | ||
} | ||
} |
7 changes: 6 additions & 1 deletion
7
src/darwinMain/kotlin/io/github/oshai/kotlinlogging/KotlinLoggingConfiguration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
48 changes: 0 additions & 48 deletions
48
src/darwinMain/kotlin/io/github/oshai/kotlinlogging/OSLogAppender.kt
This file was deleted.
Oops, something went wrong.
28 changes: 0 additions & 28 deletions
28
src/darwinMain/kotlin/io/github/oshai/kotlinlogging/OSLogSubsystemAppender.kt
This file was deleted.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
src/darwinMain/kotlin/io/github/oshai/kotlinlogging/internal/ErrorMessageProducer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
42 changes: 42 additions & 0 deletions
42
src/darwinMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(".")) | ||
) | ||
} | ||
else -> { | ||
DarwinKLogger(name, os_log_create(name, "")) | ||
} | ||
} | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/darwinMain/kotlin/io/github/oshai/kotlinlogging/internal/KLoggerNameResolver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ?: "" | ||
} |