@@ -12,7 +12,8 @@ use ldk_node::config::{Config, EsploraSyncConfig};
1212use ldk_node:: io:: sqlite_store:: SqliteStore ;
1313use ldk_node:: payment:: { PaymentDirection , PaymentKind , PaymentStatus } ;
1414use ldk_node:: {
15- Builder , CustomTlvRecord , Event , LightningBalance , Node , NodeError , PendingSweepBalance ,
15+ Builder , CustomTlvRecord , Event , FilesystemLoggerConfig , LightningBalance ,
16+ LogFacadeLoggerConfig , Node , NodeError , PendingSweepBalance ,
1617} ;
1718
1819use lightning:: ln:: msgs:: SocketAddress ;
@@ -39,9 +40,11 @@ use electrum_client::ElectrumApi;
3940use rand:: distributions:: Alphanumeric ;
4041use rand:: { thread_rng, Rng } ;
4142
43+ use log:: { LevelFilter , Log } ;
44+
4245use std:: env;
4346use std:: path:: PathBuf ;
44- use std:: sync:: { Arc , RwLock } ;
47+ use std:: sync:: { Arc , Mutex , RwLock } ;
4548use std:: time:: Duration ;
4649
4750macro_rules! expect_event {
@@ -250,6 +253,47 @@ pub(crate) enum TestChainSource<'a> {
250253 BitcoindRpc ( & ' a BitcoinD ) ,
251254}
252255
256+ #[ derive( Clone ) ]
257+ pub ( crate ) enum TestLogWriter {
258+ File ( FilesystemLoggerConfig ) ,
259+ LogFacade ( LogFacadeLoggerConfig ) ,
260+ }
261+
262+ /// Simple in-memory mock `log` logger for tests.
263+ pub ( crate ) struct MockLogger {
264+ logs : Arc < Mutex < Vec < String > > > ,
265+ }
266+
267+ impl MockLogger {
268+ pub fn new ( ) -> Self {
269+ Self { logs : Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) }
270+ }
271+
272+ pub fn retrieve_logs ( & self ) -> Vec < String > {
273+ self . logs . lock ( ) . unwrap ( ) . clone ( )
274+ }
275+ }
276+
277+ impl Log for MockLogger {
278+ fn log ( & self , record : & log:: Record ) {
279+ let message = format ! ( "[{}] {}" , record. level( ) , record. args( ) ) ;
280+ self . logs . lock ( ) . unwrap ( ) . push ( message) ;
281+ }
282+
283+ fn enabled ( & self , _metadata : & log:: Metadata ) -> bool {
284+ true
285+ }
286+
287+ fn flush ( & self ) { }
288+ }
289+
290+ pub ( crate ) fn init_mock_logger ( level : LevelFilter ) -> Arc < MockLogger > {
291+ let logger = Arc :: new ( MockLogger :: new ( ) ) ;
292+ log:: set_boxed_logger ( Box :: new ( logger. clone ( ) ) ) . unwrap ( ) ;
293+ log:: set_max_level ( level) ;
294+ logger
295+ }
296+
253297macro_rules! setup_builder {
254298 ( $builder: ident, $config: expr) => {
255299 #[ cfg( feature = "uniffi" ) ]
@@ -263,11 +307,11 @@ pub(crate) use setup_builder;
263307
264308pub ( crate ) fn setup_two_nodes (
265309 chain_source : & TestChainSource , allow_0conf : bool , anchor_channels : bool ,
266- anchors_trusted_no_reserve : bool ,
310+ anchors_trusted_no_reserve : bool , log_writer : TestLogWriter ,
267311) -> ( TestNode , TestNode ) {
268312 println ! ( "== Node A ==" ) ;
269313 let config_a = random_config ( anchor_channels) ;
270- let node_a = setup_node ( chain_source, config_a, None ) ;
314+ let node_a = setup_node ( chain_source, config_a, None , log_writer . clone ( ) ) ;
271315
272316 println ! ( "\n == Node B ==" ) ;
273317 let mut config_b = random_config ( anchor_channels) ;
@@ -282,12 +326,13 @@ pub(crate) fn setup_two_nodes(
282326 . trusted_peers_no_reserve
283327 . push ( node_a. node_id ( ) ) ;
284328 }
285- let node_b = setup_node ( chain_source, config_b, None ) ;
329+ let node_b = setup_node ( chain_source, config_b, None , log_writer ) ;
286330 ( node_a, node_b)
287331}
288332
289333pub ( crate ) fn setup_node (
290334 chain_source : & TestChainSource , config : Config , seed_bytes : Option < Vec < u8 > > ,
335+ log_writer : TestLogWriter ,
291336) -> TestNode {
292337 setup_builder ! ( builder, config) ;
293338 match chain_source {
@@ -308,6 +353,15 @@ pub(crate) fn setup_node(
308353 } ,
309354 }
310355
356+ match log_writer {
357+ TestLogWriter :: File ( fs_config) => {
358+ builder. set_filesystem_logger ( fs_config) ;
359+ } ,
360+ TestLogWriter :: LogFacade ( lf_config) => {
361+ builder. set_log_facade_logger ( lf_config) ;
362+ } ,
363+ }
364+
311365 if let Some ( seed) = seed_bytes {
312366 builder. set_entropy_seed_bytes ( seed) . unwrap ( ) ;
313367 }
0 commit comments