Skip to content

Commit

Permalink
test(android): set custom log writer via builder method
Browse files Browse the repository at this point in the history
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]
  • Loading branch information
enigbe committed Dec 13, 2024
1 parent 0347e04 commit 26aff81
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand All @@ -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()
Expand All @@ -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()

Expand All @@ -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<String>()

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
}
}
1 change: 1 addition & 0 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -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<SocketAddress> listening_addresses);
Expand Down

0 comments on commit 26aff81

Please sign in to comment.