Skip to content

Commit

Permalink
OSLogWriter public logging by configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
rocketraman committed Nov 14, 2024
1 parent 06509d9 commit 648d20f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
21 changes: 14 additions & 7 deletions kermit-core/src/appleMain/kotlin/co/touchlab/kermit/OSLogWriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?) {
Expand Down Expand Up @@ -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)
}
}
}
9 changes: 9 additions & 0 deletions kermit-core/src/nativeInterop/cInterop/os_log.def
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit 648d20f

Please sign in to comment.