Skip to content

Commit

Permalink
Fall back to data_dir if log_file is unspecified.
Browse files Browse the repository at this point in the history
  • Loading branch information
mhrheaume committed Nov 19, 2024
1 parent a47a2ff commit 7335927
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 35 deletions.
2 changes: 1 addition & 1 deletion config_spec.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ 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. By default this is stored in ~/.lndk"
doc = "The path to the lndk log file. If not specified, defaults to `<data_dir>/lndk.log`."

[[param]]
name = "log_level"
Expand Down
16 changes: 7 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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<String>, log_dir: Option<String>) -> Result<(), ()> {
pub fn setup_logger(log_level: Option<String>, log_file: Option<PathBuf>) -> Result<(), ()> {
let log_level = match log_level {
Some(level_str) => match LevelFilter::from_str(&level_str) {
Ok(level) => level,
Expand All @@ -85,22 +87,18 @@ pub fn setup_logger(log_level: Option<String>, log_dir: Option<String>) -> 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()
Expand Down
13 changes: 9 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -37,9 +37,14 @@ async fn main() -> Result<(), ()> {
.unwrap_or_exit()
.0;

let data_dir = create_data_dir(config.data_dir)
let data_dir = create_data_dir(&config.data_dir)
.map_err(|e| println!("Error creating LNDK's data dir {e:?}"))?;
setup_logger(config.log_level, config.log_file)?;

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,
Expand Down Expand Up @@ -157,7 +162,7 @@ async fn main() -> Result<(), ()> {
}

// Creates lndk's data directory at the specified directory, or ~/.lndk if not specified.
fn create_data_dir(data_dir: Option<String>) -> Result<PathBuf, std::io::Error> {
fn create_data_dir(data_dir: &Option<String>) -> Result<PathBuf, std::io::Error> {
let path = match data_dir {
Some(dir) => PathBuf::from(&dir),
None => home_dir().unwrap().join(DEFAULT_DATA_DIR),
Expand Down
8 changes: 1 addition & 7 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,7 @@ pub async fn setup_lndk(
let handler = Arc::new(lndk::OfferHandler::default());
let messenger = lndk::LndkOnionMessenger::new();

let log_file = Some(
lndk_dir
.join(format!("lndk-logs.txt"))
.to_str()
.unwrap()
.to_string(),
);
let log_file = Some(lndk_dir.join(format!("lndk-logs.txt")));
setup_logger(None, log_file).unwrap();

return (lndk_cfg, handler, messenger, shutdown);
Expand Down
16 changes: 2 additions & 14 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,7 @@ async fn test_lndk_send_invoice_request() {
}
}

let log_file = Some(
lndk_dir
.join(format!("lndk-logs.txt"))
.to_str()
.unwrap()
.to_string(),
);
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.
Expand Down Expand Up @@ -266,13 +260,7 @@ async fn test_lndk_send_invoice_request() {
rate_limit_period_secs: 1,
};

let log_file = Some(
lndk_dir
.join(format!("lndk-logs.txt"))
.to_str()
.unwrap()
.to_string(),
);
let log_file = Some(lndk_dir.join(format!("lndk-logs.txt")));
setup_logger(None, log_file).unwrap();

let handler = Arc::new(lndk::OfferHandler::default());
Expand Down

0 comments on commit 7335927

Please sign in to comment.