Skip to content

Commit

Permalink
refactor: optimize logging system
Browse files Browse the repository at this point in the history
This commit addresses a series of fixes, refactors,
and documentation changes meant to optimize the new
logging system to be better with:

- Improved memory usage by eliminating unnecessary data
  allocations and improving log record handling.

- Enhanced logging system with support for foreign
  implementations (as tested with Kotlin).

- Improved documentation across board.

- Cleaner refactors and simpler code that improve
  clarity.

- Streamlined logging configuration.

- Pre-existing bug fixes.
  • Loading branch information
enigbe committed Jan 30, 2025
1 parent 63f08b2 commit e509cf8
Show file tree
Hide file tree
Showing 11 changed files with 352 additions and 322 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ libc = "0.2"
uniffi = { version = "0.27.3", features = ["build"], optional = true }
serde = { version = "1.0.210", default-features = false, features = ["std", "derive"] }
serde_json = { version = "1.0.128", default-features = false, features = ["std"] }
log = { version = "0.4.22", features = ["std"]}
log = { version = "0.4.22", default-features = false, features = ["std"]}

vss-client = "0.3"
prost = { version = "0.11.6", default-features = false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@
*/
package org.lightningdevkit.ldknode

import kotlin.UInt
import kotlin.test.Test
import kotlin.test.assertEquals
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlin.io.path.createTempDirectory
import kotlin.test.Test
import org.junit.runner.RunWith
import org.lightningdevkit.ldknode.*;
import android.content.Context.MODE_PRIVATE
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.lightningdevkit.ldknode.*

@RunWith(AndroidJUnit4::class)
class AndroidLibTest {
@Test fun node_start_stop() {
@Test
fun node_start_stop() {
val tmpDir1 = createTempDirectory("ldk_node").toString()
println("Random dir 1: $tmpDir1")
val tmpDir2 = createTempDirectory("ldk_node").toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import java.net.http.HttpRequest
import java.net.http.HttpResponse
import kotlin.io.path.createTempDirectory
import kotlin.test.assertEquals
import kotlin.test.assertTrue

fun runCommandAndWait(vararg cmd: String): String {
println("Running command \"${cmd.joinToString(" ")}\"")
Expand Down Expand Up @@ -92,6 +93,60 @@ fun waitForBlock(esploraEndpoint: String, blockHash: String) {
}
}

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
}

fun getLogMessages(): List<String> {
return logMessages.toList()
}

override fun log(record: LogRecord) {
val recordLevel =
when (record.level.toString().lowercase()) {
"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 logMessage = formatRecord(record)
logMessages.add(logMessage)
println("$logMessage")
}
}

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 %-6s [%s:%s] %s\n",
timestamp,
record.level,
record.modulePath,
record.line,
record.args
)
}

private fun isLevelEnabled(level: LogLevel): Boolean {
return level.ordinal <= currentLogLevel.ordinal
}
}

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class LibraryTest {

Expand All @@ -106,6 +161,9 @@ class LibraryTest {
}

@Test fun fullCycle() {
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 @@ -129,8 +187,11 @@ class LibraryTest {

val builder1 = Builder.fromConfig(config1)
builder1.setChainSourceEsplora(esploraEndpoint, null)
builder1.setCustomLogger(logWriter1)

val builder2 = Builder.fromConfig(config2)
builder2.setChainSourceEsplora(esploraEndpoint, null)
builder2.setCustomLogger(logWriter2)

val node1 = builder1.build()
val node2 = builder2.build()
Expand Down Expand Up @@ -263,6 +324,9 @@ class LibraryTest {
assert(spendableBalance1AfterClose < 100000u)
assertEquals(102500uL, spendableBalance2AfterClose)

assertTrue(logWriter1.getLogMessages().isNotEmpty())
assertTrue(logWriter2.getLogMessages().isNotEmpty())

node1.stop()
node2.stop()
}
Expand Down
28 changes: 10 additions & 18 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ enum LogLevel {
"Error",
};

dictionary FilesystemLoggerConfig {
string log_file_path;
dictionary LogRecord {
LogLevel level;
string args;
string module_path;
u32 line;
};

dictionary LogFacadeLoggerConfig {
LogLevel level;
[Trait, WithForeign]
interface LogWriter {
void log(LogRecord record);
};

interface Builder {
Expand All @@ -57,8 +60,9 @@ interface Builder {
void set_gossip_source_rgs(string rgs_server_url);
void set_liquidity_source_lsps2(SocketAddress address, PublicKey node_id, string? token);
void set_storage_dir_path(string storage_dir_path);
void set_filesystem_logger(FilesystemLoggerConfig fs_config);
void set_log_facade_logger(LogFacadeLoggerConfig lf_config);
void set_filesystem_logger(string? log_file_path, LogLevel? log_level);
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 Expand Up @@ -593,18 +597,6 @@ dictionary NodeAnnouncementInfo {
sequence<SocketAddress> addresses;
};

dictionary LogRecord {
LogLevel level;
string args;
string module_path;
u32 line;
};

[Trait]
interface LogWriter {
void log(LogRecord record);
};

[Custom]
typedef string Txid;

Expand Down
Loading

0 comments on commit e509cf8

Please sign in to comment.