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

extract DarwinFormatter and add test #394

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ kotlin {
val darwinMain by creating {
dependsOn(commonMain)
}
val darwinTest by creating {
dependencies {
implementation(kotlin("test"))
}
}
linuxTargets.forEach {
getByName("${it.targetName}Main") {
dependsOn(linuxMain)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

package io.github.oshai.kotlinlogging

import io.github.oshai.kotlinlogging.internal.DarwinFormatter
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.ptr
import platform.darwin.OS_LOG_TYPE_DEBUG
Expand All @@ -20,17 +21,7 @@ public class DarwinKLogger(override val name: String, override val underlyingLog
override fun at(level: Level, marker: Marker?, block: KLoggingEventBuilder.() -> Unit) {
if (isLoggingEnabledFor(level, marker)) {
KLoggingEventBuilder().apply(block).run {
val formattedMessage = buildString {
marker?.getName()?.let {
append(it)
append(" ")
}
append(message)
cause?.stackTraceToString()?.let {
append('\n')
append(it)
}
}
val formattedMessage: String = DarwinFormatter.getFormattedMessage(this, marker)
_os_log_internal(
__dso_handle.ptr,
underlyingLogger,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.github.oshai.kotlinlogging.internal

import io.github.oshai.kotlinlogging.KLoggingEventBuilder
import io.github.oshai.kotlinlogging.Marker

internal object DarwinFormatter {

internal fun getFormattedMessage(builder: KLoggingEventBuilder, marker: Marker?): String {
return buildString {
marker?.getName()?.let {
append(it)
append(" ")
}
append(builder.message)
builder.cause?.stackTraceToString()?.let {
append('\n')
append(it)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.oshai.kotlinlogging.internal

import kotlin.test.*

class DarwinFormatterTest {

@Test
fun simpleTest() {
KLoggingEventBuilder().apply { message = "msg" }.run {
assertEquals("msg1", DarwinFormatter.getFormattedMessage(this, null))
}
}
}
Loading