Skip to content

Commit

Permalink
refactor: remove Android CustomLogWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
enigbe committed Jan 20, 2025
1 parent 255b86f commit d6dde2c
Showing 1 changed file with 5 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,16 @@
*/
package org.lightningdevkit.ldknode

import kotlin.UInt
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
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 com.sun.jna.Pointer
import org.lightningdevkit.ldknode.*

@RunWith(AndroidJUnit4::class)
class AndroidLibTest {
@Test fun node_start_stop() {
val logWriter1 = CustomLogWriter(CustomLogWriter.LogLevel.TRACE)
val logWriter2 = CustomLogWriter(CustomLogWriter.LogLevel.TRACE)

@Test
fun node_start_stop() {
val tmpDir1 = createTempDirectory("ldk_node").toString()
println("Random dir 1: $tmpDir1")
val tmpDir2 = createTempDirectory("ldk_node").toString()
Expand All @@ -42,9 +34,6 @@ class AndroidLibTest {
val builder1 = Builder.fromConfig(config1)
val builder2 = Builder.fromConfig(config2)

builder1.setCustomLogger(logWriter1)
builder2.setCustomLogger(logWriter2)

val node1 = builder1.build()
val node2 = builder2.build()

Expand All @@ -63,63 +52,7 @@ 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(Pointer.NULL) {
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)
}
}

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.args
)
}

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

0 comments on commit d6dde2c

Please sign in to comment.