Skip to content

Commit a0f6ac4

Browse files
committed
fix: Prevent Darwin log message redaction by using os_log_with_type via a new cinterop and improve marker handling.
1 parent dafab36 commit a0f6ac4

4 files changed

Lines changed: 28 additions & 8 deletions

File tree

build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,10 @@ kotlin {
271271
getByName("${it.targetName}Test") {
272272
dependsOn(darwinTest)
273273
}
274+
val main by it.compilations.getting
275+
val oslog by main.cinterops.creating {
276+
defFile(project.file("src/darwinMain/cinterop/oslog.def"))
277+
}
274278
}
275279
}
276280
}

src/darwinMain/cinterop/oslog.def

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package = io.github.oshai.kotlinlogging.cinterop
2+
language = Objective-C
3+
headers = os/log.h
4+
5+
---
6+
#include <os/log.h>
7+
8+
static inline void kotlin_logging_os_log(os_log_t log, os_log_type_t type, const char* message) {
9+
os_log_with_type(log, type, "%{public}s", message);
10+
}

src/darwinMain/kotlin/io/github/oshai/kotlinlogging/DarwinKLogger.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
package io.github.oshai.kotlinlogging
44

5+
import io.github.oshai.kotlinlogging.cinterop.kotlin_logging_os_log
56
import io.github.oshai.kotlinlogging.internal.DarwinFormatter
67
import kotlinx.cinterop.ExperimentalForeignApi
7-
import kotlinx.cinterop.ptr
88
import platform.darwin.OS_LOG_TYPE_DEBUG
99
import platform.darwin.OS_LOG_TYPE_DEFAULT
1010
import platform.darwin.OS_LOG_TYPE_ERROR
@@ -21,14 +21,17 @@ public class DarwinKLogger(override val name: String, override val underlyingLog
2121
override fun at(level: Level, marker: Marker?, block: KLoggingEventBuilder.() -> Unit) {
2222
if (isLoggingEnabledFor(level, marker)) {
2323
KLoggingEventBuilder().apply(block).run {
24-
val formattedMessage: String = DarwinFormatter.getFormattedMessage(this, marker)
25-
_os_log_internal(
26-
__dso_handle.ptr,
24+
val message = DarwinFormatter.getFormattedMessage(this, marker)
25+
val formattedMessage = if (marker != null) {
26+
// Separating marker and message because OSLog doesn't have a way to pass marker as a separate argument
27+
// formatting manually to string for now
28+
marker.toString() + " " + message
29+
} else {
30+
message
31+
}
32+
kotlin_logging_os_log(
2733
underlyingLogger,
2834
level.toDarwinLevel(),
29-
// Use %{public}s to prevent redaction of the log message in historical logs (log show)
30-
// See https://github.com/oshai/kotlin-logging/issues/588
31-
"%{public}s",
3235
formattedMessage,
3336
)
3437
}

src/darwinTest/kotlin/io/github/oshai/kotlinlogging/Issue588Test.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ package io.github.oshai.kotlinlogging
22

33
import platform.Foundation.NSUUID
44
import kotlin.test.Test
5+
import kotlin.test.assertTrue
56

67
class Issue588Test {
78
@Test
89
fun testHistoricalLogging() {
910
val uuid = NSUUID().UUIDString
1011
val logger = KotlinLogging.logger("issue588.repro")
12+
1113
logger.info { "Test message execution $uuid" }
12-
println("Logged message with UUID: $uuid")
14+
logger.error { "Test error execution $uuid" }
15+
}
1316
}
1417
}

0 commit comments

Comments
 (0)