diff --git a/build.gradle.kts b/build.gradle.kts index d51c4d05..13319727 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -50,4 +50,6 @@ allprojects { mavenCentral() google() } + tasks.getByName("dokkaHtml").dependsOn(":kermit:transformIosMainCInteropDependenciesMetadataForIde") } + diff --git a/gradle.properties b/gradle.properties index 79d08a27..1cb68a75 100644 --- a/gradle.properties +++ b/gradle.properties @@ -24,4 +24,5 @@ POM_DEVELOPER_ORG=Touchlab POM_DEVELOPER_URL=https://touchlab.co/ kotlin.js.ir.output.granularity=whole-program -enableWasm=true \ No newline at end of file +enableWasm=true +kotlin.mpp.enableCInteropCommonization=true \ No newline at end of file diff --git a/kermit-core/build.gradle.kts b/kermit-core/build.gradle.kts index 454ab27a..26660185 100644 --- a/kermit-core/build.gradle.kts +++ b/kermit-core/build.gradle.kts @@ -75,6 +75,12 @@ kotlin { implementation(kotlin("test-junit")) } + targets.withType().all { + if (konanTarget.family.isAppleFamily) { + compilations.getByName("main").cinterops.create("os_log") + } + } + getByName("androidUnitTest").dependencies { implementation(libs.androidx.runner) implementation(libs.roboelectric) diff --git a/kermit-core/src/appleMain/kotlin/co/touchlab/kermit/OSLogWriter.kt b/kermit-core/src/appleMain/kotlin/co/touchlab/kermit/OSLogWriter.kt index 8b0fa96b..a5306f7f 100644 --- a/kermit-core/src/appleMain/kotlin/co/touchlab/kermit/OSLogWriter.kt +++ b/kermit-core/src/appleMain/kotlin/co/touchlab/kermit/OSLogWriter.kt @@ -10,6 +10,7 @@ package co.touchlab.kermit +import co.touchlab.kermit.darwin.* import kotlinx.cinterop.ExperimentalForeignApi import kotlinx.cinterop.ptr import platform.darwin.OS_LOG_DEFAULT @@ -18,8 +19,7 @@ import platform.darwin.OS_LOG_TYPE_DEFAULT import platform.darwin.OS_LOG_TYPE_ERROR import platform.darwin.OS_LOG_TYPE_FAULT import platform.darwin.OS_LOG_TYPE_INFO -import platform.darwin.__dso_handle -import platform.darwin._os_log_internal +import platform.darwin.os_log_type_t import kotlin.experimental.ExperimentalNativeApi /** @@ -55,11 +55,11 @@ open class OSLogWriter internal constructor( } @OptIn(ExperimentalNativeApi::class) - open fun logThrowable(osLogSeverity: UByte, throwable: Throwable) { + open fun logThrowable(osLogSeverity: os_log_type_t, throwable: Throwable) { darwinLogger.log(osLogSeverity, throwable.getStackTrace().joinToString("\n")) } - private fun kermitSeverityToOsLogType(severity: Severity): UByte = when (severity) { + private fun kermitSeverityToOsLogType(severity: Severity): os_log_type_t = when (severity) { Severity.Verbose, Severity.Debug -> OS_LOG_TYPE_DEBUG Severity.Info -> OS_LOG_TYPE_INFO Severity.Warn -> OS_LOG_TYPE_DEFAULT @@ -73,18 +73,13 @@ open class OSLogWriter internal constructor( internal interface DarwinLogger { - fun log(osLogSeverity: UByte, message: String) + fun log(osLogSeverity: os_log_type_t, message: String) } +@OptIn(ExperimentalForeignApi::class) private object DarwinLoggerActual : DarwinLogger { - @OptIn(ExperimentalForeignApi::class) - override fun log(osLogSeverity: UByte, message: String) { - _os_log_internal( - __dso_handle.ptr, - OS_LOG_DEFAULT, - osLogSeverity, - "%s", - message - ) + private val logger = darwin_log_create("", "")!! + override fun log(osLogSeverity: os_log_type_t, message: String) { + darwin_log_with_type(logger, osLogSeverity, message) } } \ No newline at end of file diff --git a/kermit-core/src/appleMain/kotlin/co/touchlab/kermit/XcodeSeverityWriter.kt b/kermit-core/src/appleMain/kotlin/co/touchlab/kermit/XcodeSeverityWriter.kt index 8eafba8b..67ba394d 100644 --- a/kermit-core/src/appleMain/kotlin/co/touchlab/kermit/XcodeSeverityWriter.kt +++ b/kermit-core/src/appleMain/kotlin/co/touchlab/kermit/XcodeSeverityWriter.kt @@ -11,6 +11,7 @@ package co.touchlab.kermit import kotlin.experimental.ExperimentalNativeApi +import platform.darwin.os_log_type_t /** * Development-focused LogWriter. Will write a colored emoji according to Severity, and write the Throwable stack trace @@ -22,7 +23,7 @@ open class XcodeSeverityWriter(private val messageStringFormatter: MessageString "${emojiPrefix(severity)} ${messageStringFormatter.formatMessage(null, tag, message)}" @OptIn(ExperimentalNativeApi::class) - override fun logThrowable(osLogSeverity: UByte, throwable: Throwable) { + override fun logThrowable(osLogSeverity: os_log_type_t, throwable: Throwable) { // oslog cuts off longer strings, so for local development, println is more useful println(throwable.getStackTrace().joinToString("\n")) } diff --git a/kermit-core/src/nativeInterop/cInterop/os_log.def b/kermit-core/src/nativeInterop/cInterop/os_log.def new file mode 100644 index 00000000..baed7c77 --- /dev/null +++ b/kermit-core/src/nativeInterop/cInterop/os_log.def @@ -0,0 +1,21 @@ +package = co.touchlab.kermit.darwin +--- + +#include + +/** + * Passing `os_log_t` between this file and a normal kotlin file isn't working correctly. + * There's a weird type mismatch: + * - from here: os_log_t = CPointer + * - normal kotlin: os_log_t = NSObject? + * For now we're just avoiding the issue by using a generic pointer. + */ +typedef void* darwin_os_log_t; + +darwin_os_log_t darwin_log_create(const char *subsystem, const char *category) { + return os_log_create(subsystem, category); +} + +void darwin_log_with_type(darwin_os_log_t log, os_log_type_t type, const char *msg) { + os_log_with_type((os_log_t)log, type, "%s", msg); +}