Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSLogWriter not working properly #381

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Loading