Skip to content

Commit

Permalink
OSLogWriter not working properly (#381)
Browse files Browse the repository at this point in the history
* Replacing usage of private API with public API, by switching to cInterop. Fixes issue #339

* Update build.gradle.kts

---------

Co-authored-by: Kevin Schildhorn <[email protected]>
  • Loading branch information
robbiehanson and KevinSchildhorn authored Nov 6, 2024
1 parent e87d7da commit a38320f
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 16 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@ allprojects {
mavenCentral()
google()
}
tasks.getByName("dokkaHtml").dependsOn(":kermit:transformIosMainCInteropDependenciesMetadataForIde")
}

3 changes: 2 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions kermit-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ kotlin {
implementation(kotlin("test-junit"))
}

targets.withType<org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget>().all {
if (konanTarget.family.isAppleFamily) {
compilations.getByName("main").cinterops.create("os_log")
}
}

getByName("androidUnitTest").dependencies {
implementation(libs.androidx.runner)
implementation(libs.roboelectric)
Expand Down
23 changes: 9 additions & 14 deletions kermit-core/src/appleMain/kotlin/co/touchlab/kermit/OSLogWriter.kt
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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"))
}
Expand Down
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 a38320f

Please sign in to comment.