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 27, 2023
1 parent 92ff8e9 commit 7f9dcf8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 13 deletions.
1 change: 1 addition & 0 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace ldk_node {

dictionary Config {
string storage_dir_path = "/tmp/ldk_node/";
string? log_dir_path = null;
Network network = "Bitcoin";
NetAddress? listening_address = null;
u32 default_cltv_expiry_delta = 144;
Expand Down
20 changes: 18 additions & 2 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,12 @@ impl NodeBuilder {
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 = Some(log_dir_path);
self
}

/// Sets the Bitcoin network used.
pub fn set_network(&mut self, network: Network) -> &mut Self {
self.config.network = network;
Expand Down Expand Up @@ -329,6 +335,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) {
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 @@ -377,10 +388,15 @@ fn build_with_store_internal<K: KVStore + Sync + Send + 'static>(
let bdk_data_dir = format!("{}/bdk", config.storage_dir_path);
fs::create_dir_all(bdk_data_dir.clone()).map_err(|_| BuildError::StoragePathAccessFailed)?;

let log_dir = match &config.log_dir_path {
Some(log_dir) => String::from(log_dir),
None => config.storage_dir_path.clone() + "logs/",
};

// Initialize the Logger
let log_file_path = format!(
"{}/logs/ldk_node_{}.log",
config.storage_dir_path,
"{}/ldk_node_{}.log",
log_dir,
chrono::offset::Local::now().format("%Y_%m_%d")
);
let logger = Arc::new(
Expand Down
26 changes: 15 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,21 +198,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` | None |
/// | `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 the logs can be found in /tmp/ldk_node/logs/.
pub log_dir_path: Option<String>,
/// The used Bitcoin network.
pub network: Network,
/// The IP address and TCP port the node will listen on.
Expand Down Expand Up @@ -247,6 +250,7 @@ impl Default for Config {
fn default() -> Self {
Self {
storage_dir_path: DEFAULT_STORAGE_DIR_PATH.to_string(),
log_dir_path: None,
network: DEFAULT_NETWORK,
listening_address: None,
default_cltv_expiry_delta: DEFAULT_CLTV_EXPIRY_DELTA,
Expand Down

0 comments on commit 7f9dcf8

Please sign in to comment.