Skip to content

Commit d614086

Browse files
angelixcdecker
authored andcommitted
feat(gl-sdk): add LogListener for foreign-binding log capture
Apps install a process-wide LogListener and receive every log message emitted by gl-sdk and the underlying gl-client library. The bridge sits on top of the `log` crate facade — gl-sdk's own `tracing` calls are routed through `tracing`'s `log` feature, and gl-client's direct `log::*!` calls flow through the same channel. Public surface (free functions, not encumbering the Node entry points): - `set_logger(level, listener)` — install once per process. - `set_log_level(level)` — adjust the filter at runtime. - `LogEntry { level, message, target, file, line }` — record forwarded to the listener. - `LogLevel { Error, Warn, Info, Debug, Trace }`. - `LogListener.on_log(entry)` — UniFFI callback interface. Tests - Python test_logging.py verifies LogLevel variants, LogEntry shape, LogListener callback type, and that set_log_level is callable. - Kotlin LoggingTest.kt installs the listener in @BeforeClass (handling the "already installed" error gracefully) and asserts that register_or_recover actually drives log entries through the listener with non-empty targets. Event-streaming changes (NodeEventListener trait, set_event_listener on Node, the encumbering `event_listener` argument on register / recover / connect / register_or_recover) are intentionally NOT in this commit. The simple top-level functions stay 3-arg. Event streaming can be reintroduced via the OOP / builder interface in a separate PR.
1 parent 7f8c28c commit d614086

7 files changed

Lines changed: 598 additions & 7 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Instrumented tests for the SDK logging framework.
2+
//
3+
// `setLogger` installs a process-wide logger — only one install
4+
// succeeds per process. A `@Before` step installs a shared capturing
5+
// listener; subsequent tests inspect it or call `setLogLevel` without
6+
// re-installing.
7+
8+
package com.blockstream.glsdk
9+
10+
import android.util.Log
11+
import androidx.test.ext.junit.runners.AndroidJUnit4
12+
import org.junit.Assert.*
13+
import org.junit.BeforeClass
14+
import org.junit.Test
15+
import org.junit.runner.RunWith
16+
import java.util.concurrent.ConcurrentLinkedQueue
17+
18+
@RunWith(AndroidJUnit4::class)
19+
class LoggingTest {
20+
21+
companion object {
22+
private val captured = ConcurrentLinkedQueue<LogEntry>()
23+
24+
@BeforeClass
25+
@JvmStatic
26+
fun installLogger() {
27+
val listener = object : LogListener {
28+
override fun onLog(entry: LogEntry) {
29+
captured.add(entry)
30+
Log.d("glsdk", "[${entry.level}] ${entry.target}: ${entry.message}")
31+
}
32+
}
33+
try {
34+
setLogger(LogLevel.TRACE, listener)
35+
} catch (e: Exception) {
36+
// Already installed by a prior test class in the same
37+
// process — expected when multiple test classes run.
38+
}
39+
}
40+
}
41+
42+
@Test
43+
fun set_log_level_does_not_throw() {
44+
setLogLevel(LogLevel.WARN)
45+
setLogLevel(LogLevel.TRACE)
46+
}
47+
48+
@Test
49+
fun listener_receives_logs_during_sdk_activity() {
50+
captured.clear()
51+
setLogLevel(LogLevel.TRACE)
52+
53+
val config = Config()
54+
val mnemonic = "zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong"
55+
try {
56+
registerOrRecover(mnemonic, null, config)
57+
} catch (_: Exception) {
58+
// May fail on network / credentials — we only care that logs flowed.
59+
}
60+
61+
val entries = captured.toList()
62+
Log.d("glsdk", "Captured ${entries.size} log entries")
63+
assertTrue(
64+
"Expected at least one log entry from gl-client during register_or_recover",
65+
entries.isNotEmpty(),
66+
)
67+
// Every entry should have a non-empty target and message
68+
for (entry in entries) {
69+
assertFalse("empty target in $entry", entry.target.isEmpty())
70+
assertNotNull(entry.message)
71+
}
72+
}
73+
}

libs/gl-sdk/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ anyhow = "1"
1515
bip39 = "2.2.0"
1616
gl-client = { version = "0.4.0", path = "../gl-client" }
1717
hex = "0.4"
18+
log = "0.4"
1819
lightning-invoice = "0.33"
1920
once_cell = "1.21.3"
2021
serde = { version = "1", features = ["derive"] }

0 commit comments

Comments
 (0)