From 7636aefd47acfb2ac3b25c3d3394efb0492a1c23 Mon Sep 17 00:00:00 2001 From: Robbie Hanson <304604+robbiehanson@users.noreply.github.com> Date: Mon, 16 Oct 2023 14:42:07 -0500 Subject: [PATCH] Replacing usage of private API with public API, by switching to cInterop. Fixes issue #339 --- gradle.properties | 5 ++-- kermit-core/build.gradle.kts | 4 ++++ .../kotlin/co/touchlab/kermit/OSLogWriter.kt | 23 ++++++++----------- .../src/nativeInterop/cInterop/os_log.def | 21 +++++++++++++++++ 4 files changed, 37 insertions(+), 16 deletions(-) create mode 100644 kermit-core/src/nativeInterop/cInterop/os_log.def diff --git a/gradle.properties b/gradle.properties index 7a60f215..1223aa1c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -3,7 +3,7 @@ android.useAndroidX=true org.gradle.jvmargs=-Xmx4g SONATYPE_HOST=DEFAULT -RELEASE_SIGNING_ENABLED=true +RELEASE_SIGNING_ENABLED=false GROUP=co.touchlab VERSION_NAME=2.0.2 @@ -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 1809ce6c..5f6708df 100644 --- a/kermit-core/build.gradle.kts +++ b/kermit-core/build.gradle.kts @@ -143,6 +143,10 @@ kotlin { } ) + if (konanTarget.family.isAppleFamily) { + compilations.getByName("main").cinterops.create("os_log") + } + testSourceSet.dependsOn( if (konanTarget.family.isAppleFamily) { darwinTest diff --git a/kermit-core/src/darwinMain/kotlin/co/touchlab/kermit/OSLogWriter.kt b/kermit-core/src/darwinMain/kotlin/co/touchlab/kermit/OSLogWriter.kt index 8b0fa96b..a5306f7f 100644 --- a/kermit-core/src/darwinMain/kotlin/co/touchlab/kermit/OSLogWriter.kt +++ b/kermit-core/src/darwinMain/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/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); +}