Skip to content

Commit

Permalink
test: setup node with filesystem logger at gossip log level
Browse files Browse the repository at this point in the history
- Remove remnants of TestLogWriter and MockLogger that will be
   reintroduced in a later PR to add a more structured TestConfig
   object.
  • Loading branch information
enigbe committed Dec 20, 2024
1 parent 6766c60 commit b927b31
Showing 1 changed file with 6 additions and 69 deletions.
75 changes: 6 additions & 69 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
#![cfg(any(test, cln_test, vss_test))]
#![allow(dead_code)]

use chrono::Utc;
use ldk_node::config::{Config, EsploraSyncConfig};
use ldk_node::io::sqlite_store::SqliteStore;
use ldk_node::logger::{LogLevel, LogRecord, LogWriter};
use ldk_node::logger::LogLevel;
use ldk_node::payment::{PaymentDirection, PaymentKind, PaymentStatus};
use ldk_node::{
Builder, CustomTlvRecord, Event, FilesystemLoggerConfig, LightningBalance, Node, NodeError,
Expand Down Expand Up @@ -39,11 +38,9 @@ use electrum_client::ElectrumApi;
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};

use log::{LevelFilter, Log};

use std::env;
use std::path::PathBuf;
use std::sync::{Arc, Mutex, RwLock};
use std::sync::{Arc, RwLock};
use std::time::Duration;

macro_rules! expect_event {
Expand Down Expand Up @@ -252,70 +249,6 @@ pub(crate) enum TestChainSource<'a> {
BitcoindRpc(&'a BitcoinD),
}

#[derive(Clone)]
pub(crate) enum TestLogWriter {
File(FilesystemLoggerConfig),
LogFacade(LogLevel),
Custom(Arc<dyn LogWriter + Send + Sync>),
}

/// Simple in-memory mock `log` logger for tests.
pub(crate) struct MockLogger {
logs: Arc<Mutex<Vec<String>>>,
}

impl MockLogger {
pub fn new() -> Self {
Self { logs: Arc::new(Mutex::new(Vec::new())) }
}

pub fn retrieve_logs(&self) -> Vec<String> {
self.logs.lock().unwrap().clone()
}
}

impl Log for MockLogger {
fn log(&self, record: &log::Record) {
let message = format!(
"{} [{}] {}",
Utc::now().format("%Y-%m-%d %H:%M:%S"),
record.level(),
record.args()
);
self.logs.lock().unwrap().push(message);
}

fn enabled(&self, _metadata: &log::Metadata) -> bool {
true
}

fn flush(&self) {}
}

impl LogWriter for MockLogger {
fn log(&self, record: LogRecord) {
let message = format!(
"{} [{}] {}",
Utc::now().format("%Y-%m-%d %H:%M:%S"),
record.level,
record.args
);
self.logs.lock().unwrap().push(message);
}
}

pub(crate) fn init_log_logger(level: LevelFilter) -> Arc<MockLogger> {
let logger = Arc::new(MockLogger::new());
log::set_boxed_logger(Box::new(logger.clone())).unwrap();
log::set_max_level(level);
logger
}

pub(crate) fn init_custom_logger() -> Arc<MockLogger> {
let logger = Arc::new(MockLogger::new());
logger
}

macro_rules! setup_builder {
($builder: ident, $config: expr) => {
#[cfg(feature = "uniffi")]
Expand Down Expand Up @@ -374,6 +307,10 @@ pub(crate) fn setup_node(
},
}

let mut fs_config = FilesystemLoggerConfig::default();
fs_config.log_level = Some(LogLevel::Gossip);
builder.set_filesystem_logger(fs_config);

if let Some(seed) = seed_bytes {
builder.set_entropy_seed_bytes(seed).unwrap();
}
Expand Down

0 comments on commit b927b31

Please sign in to comment.