From 06509d9834aaeb29c895cf931b06f395c8b5fad2 Mon Sep 17 00:00:00 2001 From: Raman Gupta Date: Wed, 6 Nov 2024 16:33:23 -0500 Subject: [PATCH 1/2] iOS public logging Resolves #400 --- kermit-core/src/nativeInterop/cInterop/os_log.def | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kermit-core/src/nativeInterop/cInterop/os_log.def b/kermit-core/src/nativeInterop/cInterop/os_log.def index baed7c77..c64af624 100644 --- a/kermit-core/src/nativeInterop/cInterop/os_log.def +++ b/kermit-core/src/nativeInterop/cInterop/os_log.def @@ -17,5 +17,5 @@ darwin_os_log_t darwin_log_create(const char *subsystem, const char *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); + os_log_with_type((os_log_t)log, type, "%{public}s", msg); } From 648d20fb90ccc04dbd1a9ca7d86c6d367b685c8f Mon Sep 17 00:00:00 2001 From: Raman Gupta Date: Thu, 14 Nov 2024 11:33:39 -0500 Subject: [PATCH 2/2] OSLogWriter public logging by configuration --- .../kotlin/co/touchlab/kermit/OSLogWriter.kt | 21 ++++++++++++------- .../src/nativeInterop/cInterop/os_log.def | 9 ++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) 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 a5306f7f..091f529a 100644 --- a/kermit-core/src/appleMain/kotlin/co/touchlab/kermit/OSLogWriter.kt +++ b/kermit-core/src/appleMain/kotlin/co/touchlab/kermit/OSLogWriter.kt @@ -12,8 +12,6 @@ package co.touchlab.kermit import co.touchlab.kermit.darwin.* import kotlinx.cinterop.ExperimentalForeignApi -import kotlinx.cinterop.ptr -import platform.darwin.OS_LOG_DEFAULT import platform.darwin.OS_LOG_TYPE_DEBUG import platform.darwin.OS_LOG_TYPE_DEFAULT import platform.darwin.OS_LOG_TYPE_ERROR @@ -30,9 +28,9 @@ open class OSLogWriter internal constructor( private val darwinLogger: DarwinLogger ) : LogWriter() { - constructor(messageStringFormatter: MessageStringFormatter = DefaultFormatter) : this( + constructor(messageStringFormatter: MessageStringFormatter = DefaultFormatter, publicLogging: Boolean = false) : this( messageStringFormatter, - DarwinLoggerActual + DarwinLoggerActual(publicLogging) ) override fun log(severity: Severity, message: String, tag: String, throwable: Throwable?) { @@ -77,9 +75,18 @@ internal interface DarwinLogger { } @OptIn(ExperimentalForeignApi::class) -private object DarwinLoggerActual : DarwinLogger { +private class DarwinLoggerActual(publicLogging: Boolean) : DarwinLogger { private val logger = darwin_log_create("", "")!! + // see https://developer.apple.com/documentation/os/logging/generating_log_messages_from_your_code?language=objc + // iOS considers everything coming from Kermit as a dynamic string, so without publicLogging=true, all logs are + // private + private val darwinLogFn: (osLogSeverity: os_log_type_t, message: String) -> Unit = if (publicLogging) { + { osLogSeverity, message -> darwin_log_public_with_type(logger, osLogSeverity, message) } + } else { + { osLogSeverity, message -> darwin_log_with_type(logger, osLogSeverity, message) } + } + override fun log(osLogSeverity: os_log_type_t, message: String) { - darwin_log_with_type(logger, osLogSeverity, message) + darwinLogFn(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 index c64af624..62f4dd67 100644 --- a/kermit-core/src/nativeInterop/cInterop/os_log.def +++ b/kermit-core/src/nativeInterop/cInterop/os_log.def @@ -17,5 +17,14 @@ darwin_os_log_t darwin_log_create(const char *subsystem, const char *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); +} + +/** + * Uses format specifier %{public}s to make logging public. + * See https://developer.apple.com/documentation/os/logging/generating_log_messages_from_your_code?language=objc. + * We cannot pass the format specifier from Kotlin, as the API requires the value to be a string constant. + */ +void darwin_log_public_with_type(darwin_os_log_t log, os_log_type_t type, const char *msg) { os_log_with_type((os_log_t)log, type, "%{public}s", msg); }