diff --git a/config_spec.toml b/config_spec.toml index 4613925..1f65c78 100644 --- a/config_spec.toml +++ b/config_spec.toml @@ -38,9 +38,14 @@ optional = true doc = "The hex encoded macaroon to pass directly into LNDK" [[param]] -name = "log_dir" +name = "data_dir" type = "String" -doc = "The path to the lndk log file. By default this is stored in ~/.lndk" +doc = "The path to the lndk data directory. By default this is stored in ~/.lndk" + +[[param]] +name = "log_file" +type = "String" +doc = "The path to the lndk log file. If not specified, defaults to `/lndk.log`." [[param]] name = "log_level" diff --git a/src/lib.rs b/src/lib.rs index fa8a7bf..7b3b5b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,6 +42,7 @@ use log4rs::config::{Appender, Config as LogConfig, Logger, Root}; use log4rs::encode::pattern::PatternEncoder; use rate_limit::RateLimiterCfg; use std::collections::HashMap; +use std::path::PathBuf; use std::str::FromStr; use std::sync::{Arc, Mutex, Once}; use tokio::time::{sleep, timeout, Duration}; @@ -62,13 +63,14 @@ pub const DEFAULT_SERVER_HOST: &str = "127.0.0.1"; pub const DEFAULT_SERVER_PORT: u16 = 7000; pub const LDK_LOGGER_NAME: &str = "ldk"; pub const DEFAULT_DATA_DIR: &str = ".lndk"; +pub const DEFAULT_LOG_FILE: &str = "lndk.log"; pub const TLS_CERT_FILENAME: &str = "tls-cert.pem"; pub const TLS_KEY_FILENAME: &str = "tls-key.pem"; pub const DEFAULT_RESPONSE_INVOICE_TIMEOUT: u32 = 15; #[allow(clippy::result_unit_err)] -pub fn setup_logger(log_level: Option, log_dir: Option) -> Result<(), ()> { +pub fn setup_logger(log_level: Option, log_file: Option) -> Result<(), ()> { let log_level = match log_level { Some(level_str) => match LevelFilter::from_str(&level_str) { Ok(level) => level, @@ -85,22 +87,18 @@ pub fn setup_logger(log_level: Option, log_dir: Option) -> Resul None => LevelFilter::Trace, }; - let log_dir = log_dir.unwrap_or_else(|| { + let log_file = log_file.unwrap_or_else(|| { home_dir() .unwrap() - .join(".lndk") - .join("lndk.log") - .as_path() - .to_str() - .unwrap() - .to_string() + .join(DEFAULT_DATA_DIR) + .join(DEFAULT_LOG_FILE) }); // Log both to stdout and a log file. let stdout = ConsoleAppender::builder().build(); let lndk_logs = FileAppender::builder() .encoder(Box::new(PatternEncoder::new("{d} - {m}{n}"))) - .build(log_dir) + .build(log_file) .unwrap(); let config = LogConfig::builder() diff --git a/src/main.rs b/src/main.rs index 8490e6b..36bd98c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,7 +15,7 @@ use lndk::lnd::{get_lnd_client, validate_lnd_creds, LndCfg}; use lndk::server::{generate_tls_creds, read_tls, LNDKServer}; use lndk::{ lndkrpc, setup_logger, Cfg, LifecycleSignals, LndkOnionMessenger, OfferHandler, - DEFAULT_DATA_DIR, DEFAULT_SERVER_HOST, DEFAULT_SERVER_PORT, + DEFAULT_DATA_DIR, DEFAULT_LOG_FILE, DEFAULT_SERVER_HOST, DEFAULT_SERVER_PORT, }; use lndkrpc::offers_server::OffersServer; use log::{error, info}; @@ -37,9 +37,14 @@ async fn main() -> Result<(), ()> { .unwrap_or_exit() .0; - let data_dir = - create_data_dir().map_err(|e| println!("Error creating LNDK's data dir {e:?}"))?; - setup_logger(config.log_level, config.log_dir)?; + let data_dir = create_data_dir(&config.data_dir) + .map_err(|e| println!("Error creating LNDK's data dir {e:?}"))?; + + let log_file = config.log_file.map(PathBuf::from).or(config + .data_dir + .map(|data_dir| PathBuf::from(data_dir).join(DEFAULT_LOG_FILE))); + + setup_logger(config.log_level, log_file)?; let creds = validate_lnd_creds( config.cert_path, @@ -156,9 +161,13 @@ async fn main() -> Result<(), ()> { Ok(()) } -// Creates lndk's data directory at ~/.lndk. -fn create_data_dir() -> Result { - let path = home_dir().unwrap().join(DEFAULT_DATA_DIR); +// Creates lndk's data directory at the specified directory, or ~/.lndk if not specified. +fn create_data_dir(data_dir: &Option) -> Result { + let path = match data_dir { + Some(dir) => PathBuf::from(&dir), + None => home_dir().unwrap().join(DEFAULT_DATA_DIR), + }; + create_dir_all(&path)?; Ok(path) diff --git a/tests/common/mod.rs b/tests/common/mod.rs index cab5531..a331d89 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -193,14 +193,8 @@ pub async fn setup_lndk( let handler = Arc::new(lndk::OfferHandler::default()); let messenger = lndk::LndkOnionMessenger::new(); - let log_dir = Some( - lndk_dir - .join(format!("lndk-logs.txt")) - .to_str() - .unwrap() - .to_string(), - ); - setup_logger(None, log_dir).unwrap(); + let log_file = Some(lndk_dir.join(format!("lndk-logs.txt"))); + setup_logger(None, log_file).unwrap(); return (lndk_cfg, handler, messenger, shutdown); } diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 511391a..ec64402 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -209,14 +209,8 @@ async fn test_lndk_send_invoice_request() { } } - let log_dir = Some( - lndk_dir - .join(format!("lndk-logs.txt")) - .to_str() - .unwrap() - .to_string(), - ); - setup_logger(None, log_dir).unwrap(); + let log_file = Some(lndk_dir.join(format!("lndk-logs.txt"))); + setup_logger(None, log_file).unwrap(); // Make sure lndk successfully sends the invoice_request. let handler = Arc::new(lndk::OfferHandler::default()); @@ -266,14 +260,8 @@ async fn test_lndk_send_invoice_request() { rate_limit_period_secs: 1, }; - let log_dir = Some( - lndk_dir - .join(format!("lndk-logs.txt")) - .to_str() - .unwrap() - .to_string(), - ); - setup_logger(None, log_dir).unwrap(); + let log_file = Some(lndk_dir.join(format!("lndk-logs.txt"))); + setup_logger(None, log_file).unwrap(); let handler = Arc::new(lndk::OfferHandler::default()); let messenger = lndk::LndkOnionMessenger::new();