From fb32db0ec82329913b9c8bb34a25376ac1787c52 Mon Sep 17 00:00:00 2001 From: sstone Date: Wed, 7 Feb 2024 19:06:25 +0100 Subject: [PATCH] Add custom test logger that writes to stderr --- .../acinq/lightning/tests/utils/TestLogger.kt | 19 ++++++++++++++----- .../lightning/tests/utils/TestLogger.jvm.kt | 5 +++++ .../tests/utils/TestLogger.native.kt | 12 ++++++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 src/jvmTest/kotlin/fr/acinq/lightning/tests/utils/TestLogger.jvm.kt create mode 100644 src/nativeTest/kotlin/fr/acinq/lightning/tests/utils/TestLogger.native.kt diff --git a/src/commonTest/kotlin/fr/acinq/lightning/tests/utils/TestLogger.kt b/src/commonTest/kotlin/fr/acinq/lightning/tests/utils/TestLogger.kt index 45279f2c5..f938ed59e 100644 --- a/src/commonTest/kotlin/fr/acinq/lightning/tests/utils/TestLogger.kt +++ b/src/commonTest/kotlin/fr/acinq/lightning/tests/utils/TestLogger.kt @@ -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 ) ) diff --git a/src/jvmTest/kotlin/fr/acinq/lightning/tests/utils/TestLogger.jvm.kt b/src/jvmTest/kotlin/fr/acinq/lightning/tests/utils/TestLogger.jvm.kt new file mode 100644 index 000000000..cef9d44e7 --- /dev/null +++ b/src/jvmTest/kotlin/fr/acinq/lightning/tests/utils/TestLogger.jvm.kt @@ -0,0 +1,5 @@ +package fr.acinq.lightning.tests.utils + +actual fun printToStderr(msg: String) { + System.err.println(msg) +} \ No newline at end of file diff --git a/src/nativeTest/kotlin/fr/acinq/lightning/tests/utils/TestLogger.native.kt b/src/nativeTest/kotlin/fr/acinq/lightning/tests/utils/TestLogger.native.kt new file mode 100644 index 000000000..9cbe0fe1d --- /dev/null +++ b/src/nativeTest/kotlin/fr/acinq/lightning/tests/utils/TestLogger.native.kt @@ -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) +} \ No newline at end of file