Skip to content

Commit

Permalink
Add custom test logger that writes to stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
sstone committed Feb 7, 2024
1 parent 87d472a commit fb32db0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/commonTest/kotlin/fr/acinq/lightning/tests/utils/TestLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,25 @@

package fr.acinq.lightning.tests.utils

import co.touchlab.kermit.CommonWriter
import co.touchlab.kermit.NoTagFormatter
import co.touchlab.kermit.Severity
import co.touchlab.kermit.loggerConfigInit
import co.touchlab.kermit.*
import fr.acinq.lightning.logging.LoggerFactory

expect fun printToStderr(msg: String)

class StderrWriter(val formatter: MessageStringFormatter) : LogWriter() {
override fun log(severity: Severity, message: String, tag: String, throwable: Throwable?) {
printToStderr(formatter.formatMessage(severity, Tag(tag), Message(message)))
throwable?.let {
printToStderr(it.toString())
printToStderr(it.stackTraceToString())
}
}
}

val testLoggerFactory: LoggerFactory by lazy {
LoggerFactory(
loggerConfigInit(
logWriters = arrayOf(CommonWriter(NoTagFormatter)),
logWriters = arrayOf(StderrWriter(NoTagFormatter)),
minSeverity = Severity.Info
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package fr.acinq.lightning.tests.utils

actual fun printToStderr(msg: String) {
System.err.println(msg)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package fr.acinq.lightning.tests.utils

import kotlinx.cinterop.ExperimentalForeignApi
import platform.posix.fflush
import platform.posix.fprintf

@OptIn(ExperimentalForeignApi::class)
actual fun printToStderr(msg: String) {
val stderr = platform.posix.fdopen(2, "w")
fprintf(stderr, msg + "\n")
fflush(stderr)
}

0 comments on commit fb32db0

Please sign in to comment.