Skip to content

Commit

Permalink
Add a builder method to customize where logs are stored
Browse files Browse the repository at this point in the history
  • Loading branch information
orbitalturtle committed Jun 20, 2023
1 parent cf0094b commit e9fdd76
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
23 changes: 21 additions & 2 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::types::{
};
use crate::wallet::Wallet;
use crate::LogLevel;
use crate::DEFAULT_STORAGE_DIR_PATH;
use crate::{
Config, Node, BDK_CLIENT_CONCURRENCY, BDK_CLIENT_STOP_GAP, DEFAULT_ESPLORA_SERVER_URL,
WALLET_KEYS_SEED_LEN,
Expand Down Expand Up @@ -155,7 +156,20 @@ impl NodeBuilder {

/// Sets the used storage directory path.
pub fn set_storage_dir_path(&mut self, storage_dir_path: String) -> &mut Self {
self.config.storage_dir_path = storage_dir_path;
self.config.storage_dir_path = storage_dir_path.clone();

// We make the assumption that if the user is setting the storage_dir_path, they want to change the
// log_dir_path along with it. But if the user has /already/ set a custom log_dir_path, they don't want
// it to be changed to this new storage path.
if self.config.log_dir_path == DEFAULT_STORAGE_DIR_PATH.to_string() {
self.config.log_dir_path = storage_dir_path;
}
self
}

/// Sets the log dir path if logs need to live separate from the storage directory path.
pub fn set_log_dir_path(&mut self, log_dir_path: String) -> &mut Self {
self.config.log_dir_path = log_dir_path;
self
}

Expand Down Expand Up @@ -284,6 +298,11 @@ impl ArcedNodeBuilder {
self.inner.write().unwrap().set_storage_dir_path(storage_dir_path);
}

/// Sets the log dir path if logs need to live separate from the storage directory path.
pub fn set_log_dir_path(&self, log_dir_path: String) -> &mut Self {
self.inner.write().unwrap().set_log_dir_path(log_dir_path);
}

/// Sets the Bitcoin network used.
pub fn set_network(&self, network: Network) {
self.inner.write().unwrap().set_network(network);
Expand Down Expand Up @@ -335,7 +354,7 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
// Initialize the Logger
let log_file_path = format!(
"{}/logs/ldk_node_{}.log",
config.storage_dir_path,
config.log_dir_path,
chrono::offset::Local::now().format("%Y_%m_%d")
);
let logger = Arc::new(FilesystemLogger::new(log_file_path.clone(), config.log_level));
Expand Down
26 changes: 15 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,21 +202,24 @@ const WALLET_KEYS_SEED_LEN: usize = 64;
///
/// ### Defaults
///
/// | Parameter | Value |
/// |----------------------------------------|------------------|
/// | `storage_dir_path` | /tmp/ldk_node/ |
/// | `network` | Bitcoin |
/// | `listening_address` | None |
/// | `default_cltv_expiry_delta` | 144 |
/// | `onchain_wallet_sync_interval_secs` | 80 |
/// | `wallet_sync_interval_secs` | 30 |
/// | `fee_rate_cache_update_interval_secs` | 600 |
/// | `trusted_peers_0conf` | [] |
/// | `log_level` | Debug |
/// | Parameter | Value |
/// |----------------------------------------|----------------------------------|
/// | `storage_dir_path` | /tmp/ldk_node/ |
/// | `log_dir_path` | /tmp/ldk_node/logs/ldk_node.log |
/// | `network` | Bitcoin |
/// | `listening_address` | None |
/// | `default_cltv_expiry_delta` | 144 |
/// | `onchain_wallet_sync_interval_secs` | 80 |
/// | `wallet_sync_interval_secs` | 30 |
/// | `fee_rate_cache_update_interval_secs` | 600 |
/// | `trusted_peers_0conf` | [] |
/// | `log_level` | Debug |
///
pub struct Config {
/// The path where the underlying LDK and BDK persist their data.
pub storage_dir_path: String,
/// The path where logs are stored. By default this path is set to storage_dir_path.
pub log_dir_path: String,
/// The used Bitcoin network.
pub network: Network,
/// The IP address and TCP port the node will listen on.
Expand Down Expand Up @@ -251,6 +254,7 @@ impl Default for Config {
fn default() -> Self {
Self {
storage_dir_path: DEFAULT_STORAGE_DIR_PATH.to_string(),
log_dir_path: DEFAULT_STORAGE_DIR_PATH.to_string(),
network: DEFAULT_NETWORK,
listening_address: None,
default_cltv_expiry_delta: DEFAULT_CLTV_EXPIRY_DELTA,
Expand Down

0 comments on commit e9fdd76

Please sign in to comment.