Skip to content

Commit a23344d

Browse files
committed
refactor: drop FilesystemLoggerConfig
Additionally, cleans up the logic in setup_logger to utilize the now public DEFAULT_STORAGE_DIR_PATH
1 parent c679302 commit a23344d

File tree

3 files changed

+34
-41
lines changed

3 files changed

+34
-41
lines changed

src/builder.rs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
use crate::chain::{ChainSource, DEFAULT_ESPLORA_SERVER_URL};
99
use crate::config::{
10-
default_user_config, Config, EsploraSyncConfig, FilesystemLoggerConfig, DEFAULT_LOG_FILENAME,
11-
DEFAULT_LOG_LEVEL, WALLET_KEYS_SEED_LEN,
10+
default_user_config, Config, EsploraSyncConfig, DEFAULT_LOG_FILE_PATH, DEFAULT_LOG_LEVEL,
11+
DEFAULT_STORAGE_DIR_PATH, WALLET_KEYS_SEED_LEN,
1212
};
1313

1414
use crate::connection::ConnectionManager;
@@ -111,15 +111,30 @@ impl Default for LiquiditySourceConfig {
111111

112112
#[derive(Clone)]
113113
enum LogWriterConfig {
114-
File(FilesystemLoggerConfig),
114+
File {
115+
/// The log file path.
116+
///
117+
/// This specifies the log file path if a destination other than the storage
118+
/// directory, i.e. [`Config::storage_dir_path`], is preferred. If unconfigured,
119+
/// defaults to [`DEFAULT_LOG_FILENAME`] in default storage directory.
120+
log_file_path: Option<String>,
121+
/// This specifies the log level.
122+
///
123+
/// If unconfigured, defaults to `Debug`.
124+
log_level: Option<LogLevel>,
125+
},
115126
Log(LogLevel),
116127
Custom(Arc<dyn LogWriter>),
117128
}
118129

119130
impl std::fmt::Debug for LogWriterConfig {
120131
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
121132
match self {
122-
LogWriterConfig::File(config) => f.debug_tuple("File").field(config).finish(),
133+
LogWriterConfig::File { log_level, log_file_path } => f
134+
.debug_struct("LogWriterConfig")
135+
.field("log_level", log_level)
136+
.field("log_file_path", log_file_path)
137+
.finish(),
123138
LogWriterConfig::Log(level) => f.debug_tuple("Log").field(level).finish(),
124139
LogWriterConfig::Custom(_) => {
125140
f.debug_tuple("Custom").field(&"<config internal to custom log writer>").finish()
@@ -130,7 +145,10 @@ impl std::fmt::Debug for LogWriterConfig {
130145

131146
impl Default for LogWriterConfig {
132147
fn default() -> Self {
133-
Self::File(FilesystemLoggerConfig::default())
148+
Self::File {
149+
log_file_path: Some(DEFAULT_LOG_FILE_PATH.to_string()),
150+
log_level: Some(DEFAULT_LOG_LEVEL),
151+
}
134152
}
135153
}
136154

@@ -337,8 +355,7 @@ impl NodeBuilder {
337355
pub fn set_filesystem_logger(
338356
&mut self, log_file_path: Option<String>, log_level: Option<LogLevel>,
339357
) -> &mut Self {
340-
self.log_writer_config =
341-
Some(LogWriterConfig::File(FilesystemLoggerConfig { log_file_path, log_level }));
358+
self.log_writer_config = Some(LogWriterConfig::File { log_file_path, log_level });
342359
self
343360
}
344361

@@ -1310,11 +1327,11 @@ fn setup_logger(
13101327
if let Some(conf) = log_writer_conf { conf } else { &default_lw_config };
13111328

13121329
let logger = match log_writer_config {
1313-
LogWriterConfig::File(fs_logger_config) => {
1314-
let fp = format!("{}/{}", node_conf.storage_dir_path, DEFAULT_LOG_FILENAME);
1315-
let log_file_path =
1316-
if let Some(fp) = &fs_logger_config.log_file_path { fp } else { &fp };
1317-
let log_level = fs_logger_config.log_level.unwrap_or(DEFAULT_LOG_LEVEL);
1330+
LogWriterConfig::File { log_file_path, log_level } => {
1331+
let fp = DEFAULT_LOG_FILE_PATH
1332+
.replace(DEFAULT_STORAGE_DIR_PATH, &node_conf.storage_dir_path);
1333+
let log_file_path = log_file_path.as_ref().map(|p| p.as_str()).unwrap_or(&fp);
1334+
let log_level = log_level.unwrap_or(DEFAULT_LOG_LEVEL);
13181335

13191336
Logger::new_fs_writer(log_file_path, log_level)
13201337
.map_err(|_| BuildError::LoggerSetupFailed)?

src/config.rs

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use bitcoin::Network;
2222
use std::time::Duration;
2323

2424
// Config defaults
25-
const DEFAULT_STORAGE_DIR_PATH: &str = "/tmp/ldk_node";
2625
const DEFAULT_NETWORK: Network = Network::Bitcoin;
2726
const DEFAULT_BDK_WALLET_SYNC_INTERVAL_SECS: u64 = 80;
2827
const DEFAULT_LDK_WALLET_SYNC_INTERVAL_SECS: u64 = 30;
@@ -33,8 +32,11 @@ const DEFAULT_ANCHOR_PER_CHANNEL_RESERVE_SATS: u64 = 25_000;
3332
/// The default log level.
3433
pub const DEFAULT_LOG_LEVEL: LogLevel = LogLevel::Debug;
3534

36-
/// The default log filename.
37-
pub const DEFAULT_LOG_FILENAME: &'static str = "ldk_node.log";
35+
/// The default log file path.
36+
pub const DEFAULT_LOG_FILE_PATH: &'static str = "/tmp/ldk_node/ldk_node.log";
37+
38+
/// The default storage directory.
39+
pub const DEFAULT_STORAGE_DIR_PATH: &str = "/tmp/ldk_node";
3840

3941
// The 'stop gap' parameter used by BDK's wallet sync. This seems to configure the threshold
4042
// number of derivation indexes after which BDK stops looking for new scripts belonging to the wallet.
@@ -433,30 +435,6 @@ impl From<MaxDustHTLCExposure> for LdkMaxDustHTLCExposure {
433435
}
434436
}
435437

436-
/// Configuration options for logging to the filesystem.
437-
#[derive(Debug, Clone)]
438-
pub struct FilesystemLoggerConfig {
439-
/// The log file path.
440-
///
441-
/// This specifies the log file path if a destination other than the storage
442-
/// directory, i.e. [`Config::storage_dir_path`], is preferred. If unconfigured,
443-
/// defaults to [`DEFAULT_LOG_FILENAME`] in default storage directory.
444-
pub log_file_path: Option<String>,
445-
/// This specifies the log level.
446-
///
447-
/// If unconfigured, defaults to `Debug`.
448-
pub log_level: Option<LogLevel>,
449-
}
450-
451-
impl Default for FilesystemLoggerConfig {
452-
fn default() -> Self {
453-
Self {
454-
log_file_path: Some(format!("{}/{}", DEFAULT_STORAGE_DIR_PATH, DEFAULT_LOG_FILENAME)),
455-
log_level: Some(DEFAULT_LOG_LEVEL),
456-
}
457-
}
458-
}
459-
460438
#[cfg(test)]
461439
mod tests {
462440
use std::str::FromStr;

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ pub use event::Event;
111111

112112
pub use io::utils::generate_entropy_mnemonic;
113113

114-
pub use config::FilesystemLoggerConfig;
115-
116114
#[cfg(feature = "uniffi")]
117115
use uniffi_types::*;
118116

0 commit comments

Comments
 (0)