Skip to content

Commit

Permalink
Replacing usage of private API with public API, by switching to cInte…
Browse files Browse the repository at this point in the history
…rop. Fixes issue touchlab#339
  • Loading branch information
robbiehanson committed Oct 16, 2023
1 parent 96281ab commit 7636aef
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
5 changes: 3 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -24,4 +24,5 @@ POM_DEVELOPER_ORG=Touchlab
POM_DEVELOPER_URL=https://touchlab.co/

kotlin.js.ir.output.granularity=whole-program
enableWasm=true
enableWasm=true
kotlin.mpp.enableCInteropCommonization=true
4 changes: 4 additions & 0 deletions kermit-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ kotlin {
}
)

if (konanTarget.family.isAppleFamily) {
compilations.getByName("main").cinterops.create("os_log")
}

testSourceSet.dependsOn(
if (konanTarget.family.isAppleFamily) {
darwinTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

/**
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
}
21 changes: 21 additions & 0 deletions kermit-core/src/nativeInterop/cInterop/os_log.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package = co.touchlab.kermit.darwin
---

#include <os/log.h>

/**
* 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<os_log_s>
* - 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);
}

0 comments on commit 7636aef

Please sign in to comment.