Skip to content

Commit 574842c

Browse files
authored
Merge pull request #450 from tnull/2025-01-407-followups
2 parents aaf74cc + b968ea7 commit 574842c

File tree

6 files changed

+38
-57
lines changed

6 files changed

+38
-57
lines changed

src/builder.rs

+28-47
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, DEFAULT_LOG_FILE_PATH, DEFAULT_LOG_LEVEL,
11-
DEFAULT_STORAGE_DIR_PATH, WALLET_KEYS_SEED_LEN,
10+
default_user_config, Config, EsploraSyncConfig, DEFAULT_LOG_FILENAME, DEFAULT_LOG_LEVEL,
11+
WALLET_KEYS_SEED_LEN,
1212
};
1313

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

112112
#[derive(Clone)]
113113
enum LogWriterConfig {
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_FILE_PATH`] 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-
},
114+
File { log_file_path: Option<String>, log_level: Option<LogLevel> },
126115
Log(LogLevel),
127116
Custom(Arc<dyn LogWriter>),
128117
}
@@ -143,15 +132,6 @@ impl std::fmt::Debug for LogWriterConfig {
143132
}
144133
}
145134

146-
impl Default for LogWriterConfig {
147-
fn default() -> Self {
148-
Self::File {
149-
log_file_path: Some(DEFAULT_LOG_FILE_PATH.to_string()),
150-
log_level: Some(DEFAULT_LOG_LEVEL),
151-
}
152-
}
153-
}
154-
155135
/// An error encountered during building a [`Node`].
156136
///
157137
/// [`Node`]: crate::Node
@@ -349,9 +329,11 @@ impl NodeBuilder {
349329

350330
/// Configures the [`Node`] instance to write logs to the filesystem.
351331
///
352-
/// The `log_file_path` defaults to the [`DEFAULT_LOG_FILE_PATH`] in the default
353-
/// storage directory if set to None.
354-
/// The `log_level` defaults to [`DEFAULT_LOG_LEVEL`] if set to None.
332+
/// The `log_file_path` defaults to [`DEFAULT_LOG_FILENAME`] in the configured
333+
/// [`Config::storage_dir_path`] if set to `None`.
334+
/// The `log_level` defaults to [`DEFAULT_LOG_LEVEL`] if set to `None`.
335+
///
336+
/// [`DEFAULT_LOG_FILENAME`]: crate::config::DEFAULT_LOG_FILENAME
355337
pub fn set_filesystem_logger(
356338
&mut self, log_file_path: Option<String>, log_level: Option<LogLevel>,
357339
) -> &mut Self {
@@ -674,9 +656,11 @@ impl ArcedNodeBuilder {
674656

675657
/// Configures the [`Node`] instance to write logs to the filesystem.
676658
///
677-
/// The `log_file_path` defaults to the [`DEFAULT_LOG_FILENAME`] in the default
678-
/// storage directory if set to None.
679-
/// The `log_level` defaults to [`DEFAULT_LOG_LEVEL`] if set to None.
659+
/// The `log_file_path` defaults to [`DEFAULT_LOG_FILENAME`] in the configured
660+
/// [`Config::storage_dir_path`] if set to `None`.
661+
/// The `log_level` defaults to [`DEFAULT_LOG_LEVEL`] if set to `None`.
662+
///
663+
/// [`DEFAULT_LOG_FILENAME`]: crate::config::DEFAULT_LOG_FILENAME
680664
pub fn set_filesystem_logger(
681665
&self, log_file_path: Option<String>, log_level: Option<LogLevel>,
682666
) {
@@ -1317,34 +1301,31 @@ fn build_with_store_internal(
13171301
}
13181302

13191303
/// Sets up the node logger.
1320-
///
1321-
/// If `log_writer_conf` is set to None, uses [`LogWriterConfig::default()`].
1322-
/// The `node_conf` is provided to access the configured storage directory.
13231304
fn setup_logger(
1324-
log_writer_conf: &Option<LogWriterConfig>, node_conf: &Config,
1305+
log_writer_config: &Option<LogWriterConfig>, config: &Config,
13251306
) -> Result<Arc<Logger>, BuildError> {
1326-
let is_default = log_writer_conf.is_none();
1327-
let default_lw_config = LogWriterConfig::default();
1328-
let log_writer_config =
1329-
if let Some(conf) = log_writer_conf { conf } else { &default_lw_config };
1330-
13311307
let logger = match log_writer_config {
1332-
LogWriterConfig::File { log_file_path, log_level } => {
1333-
let fp = DEFAULT_LOG_FILE_PATH
1334-
.replace(DEFAULT_STORAGE_DIR_PATH, &node_conf.storage_dir_path);
1335-
let log_file_path =
1336-
if is_default { &fp } else { log_file_path.as_ref().map(|p| p).unwrap_or(&fp) };
1337-
1338-
let log_level = log_level.unwrap_or(DEFAULT_LOG_LEVEL);
1308+
Some(LogWriterConfig::File { log_file_path, log_level }) => {
1309+
let log_file_path = log_file_path
1310+
.clone()
1311+
.unwrap_or_else(|| format!("{}/{}", config.storage_dir_path, DEFAULT_LOG_FILENAME));
1312+
let log_level = log_level.unwrap_or_else(|| DEFAULT_LOG_LEVEL);
13391313

13401314
Logger::new_fs_writer(log_file_path, log_level)
13411315
.map_err(|_| BuildError::LoggerSetupFailed)?
13421316
},
1343-
LogWriterConfig::Log(log_level) => Logger::new_log_facade(*log_level),
1317+
Some(LogWriterConfig::Log(log_level)) => Logger::new_log_facade(*log_level),
13441318

1345-
LogWriterConfig::Custom(custom_log_writer) => {
1319+
Some(LogWriterConfig::Custom(custom_log_writer)) => {
13461320
Logger::new_custom_writer(Arc::clone(&custom_log_writer))
13471321
},
1322+
None => {
1323+
// Default to use `FileWriter`
1324+
let log_file_path = format!("{}/{}", config.storage_dir_path, DEFAULT_LOG_FILENAME);
1325+
let log_level = DEFAULT_LOG_LEVEL;
1326+
Logger::new_fs_writer(log_file_path, log_level)
1327+
.map_err(|_| BuildError::LoggerSetupFailed)?
1328+
},
13481329
};
13491330

13501331
Ok(Arc::new(logger))

src/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ const DEFAULT_ANCHOR_PER_CHANNEL_RESERVE_SATS: u64 = 25_000;
3232
/// The default log level.
3333
pub const DEFAULT_LOG_LEVEL: LogLevel = LogLevel::Debug;
3434

35-
/// The default log file path.
36-
pub const DEFAULT_LOG_FILE_PATH: &'static str = "/tmp/ldk_node/ldk_node.log";
35+
/// The default log file name.
36+
pub const DEFAULT_LOG_FILENAME: &'static str = "ldk_node.log";
3737

3838
/// The default storage directory.
3939
pub const DEFAULT_STORAGE_DIR_PATH: &str = "/tmp/ldk_node";

src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ use types::{
144144
pub use types::{ChannelDetails, CustomTlvRecord, PeerDetails, UserChannelId};
145145

146146
use logger::{log_error, log_info, log_trace, LdkLogger, Logger};
147-
pub use logger::{LogLevel, LogRecord, LogWriter};
148147

149148
use lightning::chain::BestBlock;
150149
use lightning::events::bump_transaction::Wallet as LdkWallet;

src/logger.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ impl LogWriter for Writer {
143143
($log_level:expr, $($args:tt)*) => {
144144
match $log_level {
145145
LogLevel::Gossip | LogLevel::Trace => trace!($($args)*),
146-
LogLevel::Debug => debug!($($args)*),
147-
LogLevel::Info => info!($($args)*),
148-
LogLevel::Warn => warn!($($args)*),
149-
LogLevel::Error => error!($($args)*),
146+
LogLevel::Debug => debug!($($args)*),
147+
LogLevel::Info => info!($($args)*),
148+
LogLevel::Warn => warn!($($args)*),
149+
LogLevel::Error => error!($($args)*),
150150
}
151151
};
152152
}
@@ -174,7 +174,7 @@ pub(crate) struct Logger {
174174
impl Logger {
175175
/// Creates a new logger with a filesystem writer. The parameters to this function
176176
/// are the path to the log file, and the log level.
177-
pub fn new_fs_writer(file_path: &str, level: LogLevel) -> Result<Self, ()> {
177+
pub fn new_fs_writer(file_path: String, level: LogLevel) -> Result<Self, ()> {
178178
if let Some(parent_dir) = Path::new(&file_path).parent() {
179179
fs::create_dir_all(parent_dir)
180180
.map_err(|e| eprintln!("ERROR: Failed to create log parent directory: {}", e))?;
@@ -187,7 +187,7 @@ impl Logger {
187187
.map_err(|e| eprintln!("ERROR: Failed to open log file: {}", e))?;
188188
}
189189

190-
Ok(Self { writer: Writer::FileWriter { file_path: file_path.to_string(), level } })
190+
Ok(Self { writer: Writer::FileWriter { file_path, level } })
191191
}
192192

193193
pub fn new_log_facade(level: LogLevel) -> Self {

src/uniffi_types.rs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub use crate::config::{
1414
default_config, AnchorChannelsConfig, EsploraSyncConfig, MaxDustHTLCExposure,
1515
};
1616
pub use crate::graph::{ChannelInfo, ChannelUpdateInfo, NodeAnnouncementInfo, NodeInfo};
17+
pub use crate::logger::{LogLevel, LogRecord, LogWriter};
1718
pub use crate::payment::store::{
1819
ConfirmationStatus, LSPFeeLimits, PaymentDirection, PaymentKind, PaymentStatus,
1920
};

src/wallet/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use persist::KVStoreWalletPersister;
99

10-
use crate::logger::{log_debug, log_error, log_info, log_trace, Logger, LdkLogger};
10+
use crate::logger::{log_debug, log_error, log_info, log_trace, LdkLogger, Logger};
1111

1212
use crate::fee_estimator::{ConfirmationTarget, FeeEstimator};
1313
use crate::payment::store::{ConfirmationStatus, PaymentStore};

0 commit comments

Comments
 (0)