From 26aff81d678c84fe563bd1867c6a65dfd56a3a59 Mon Sep 17 00:00:00 2001 From: Enigbe Date: Fri, 13 Dec 2024 08:58:41 +0100 Subject: [PATCH] test(android): set custom log writer via builder method This commit adds a CustomLogWriter class to the kotlin library test, configuring the writer via the exposed node builder and tests the ability to log to the custom writer destination. [WIP] --- .../lightningdevkit/ldknode/AndroidLibTest.kt | 55 +++++++++++++++++++ bindings/ldk_node.udl | 1 + 2 files changed, 56 insertions(+) diff --git a/bindings/kotlin/ldk-node-android/lib/src/androidTest/kotlin/org/lightningdevkit/ldknode/AndroidLibTest.kt b/bindings/kotlin/ldk-node-android/lib/src/androidTest/kotlin/org/lightningdevkit/ldknode/AndroidLibTest.kt index 03c4b88a7..e87fad923 100644 --- a/bindings/kotlin/ldk-node-android/lib/src/androidTest/kotlin/org/lightningdevkit/ldknode/AndroidLibTest.kt +++ b/bindings/kotlin/ldk-node-android/lib/src/androidTest/kotlin/org/lightningdevkit/ldknode/AndroidLibTest.kt @@ -6,6 +6,7 @@ package org.lightningdevkit.ldknode import kotlin.UInt import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertTrue import kotlin.io.path.createTempDirectory import org.junit.runner.RunWith import org.lightningdevkit.ldknode.*; @@ -16,6 +17,9 @@ import androidx.test.ext.junit.runners.AndroidJUnit4 @RunWith(AndroidJUnit4::class) class AndroidLibTest { @Test fun node_start_stop() { + val logWriter1 = CustomLogWriter(CustomLogWriter.LogLevel.GOSSIP) + val logWriter2 = CustomLogWriter(CustomLogWriter.LogLevel.GOSSIP) + val tmpDir1 = createTempDirectory("ldk_node").toString() println("Random dir 1: $tmpDir1") val tmpDir2 = createTempDirectory("ldk_node").toString() @@ -37,6 +41,9 @@ class AndroidLibTest { val builder1 = Builder.fromConfig(config1) val builder2 = Builder.fromConfig(config2) + builder1.set_custom_logger(logWriter1) + builder2.set_custom_logger(logWriter2) + val node1 = builder1.build() val node2 = builder2.build() @@ -55,7 +62,55 @@ class AndroidLibTest { val address2 = node2.onchain_payment().newOnchainAddress() println("Funding address 2: $address2") + assertTrue(logWriter1.getLogMessages().isNotEmpty()) + assertTrue(logWriter2.getLogMessages().isNotEmpty()) + node1.stop() node2.stop() } } + +class CustomLogWriter(private var currentLogLevel: LogLevel = LogLevel.INFO): LogWriter { + enum class LogLevel { + ERROR, WARN, INFO, DEBUG, TRACE, GOSSIP + } + + private val logMessages = mutableListOf() + + fun setLogLevel(level: LogLevel) { + currentLogLevel = level + } + + override fun log(record: LogRecord) { + val recordLevel = when(record.level.toLowerCase()) { + "error" -> LogLevel.ERROR + "warn" -> LogLevel.WARN + "info" -> LogLevel.INFO + "debug" -> LogLevel.DEBUG + "trace" -> LogLevel.TRACE + "gossip" -> LogLevel.GOSSIP + else -> LogLevel.INFO + } + + if (isLevelEnabled(recordLevel)) { + val log_message = formatRecord(record) + logMessages.add(log_message) + } + } + + private fun formatRecord(record: LogRecord): String { + val timestamp = java.time.LocalDateTime.now().format(java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + return String.format( + "%s %-5s [%s:%d] %s\n", + timestamp, + record.level, + record.modulePath, + record.line, + record.message + ) + } + + private fun isLevelEnabled(level: LogLevel): Boolean { + return level.ordinal <= currentLogLevel.ordinal + } +} diff --git a/bindings/ldk_node.udl b/bindings/ldk_node.udl index 742a6ea42..36ebbaa84 100644 --- a/bindings/ldk_node.udl +++ b/bindings/ldk_node.udl @@ -55,6 +55,7 @@ interface Builder { void set_storage_dir_path(string storage_dir_path); void set_filesystem_logger(FilesystemLoggerConfig fs_config); void set_log_facade_logger(LogLevel log_level); + void set_custom_logger(LogWriter log_writer); void set_network(Network network); [Throws=BuildError] void set_listening_addresses(sequence listening_addresses);