Skip to content

Commit

Permalink
Merge pull request #86 from lndk-org/log-race-fix
Browse files Browse the repository at this point in the history
Init logger in a thread-safe manner
  • Loading branch information
carlaKC authored Jan 29, 2024
2 parents 6aa94ad + aea10c9 commit 2a439c6
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,22 @@ use log4rs::config::{Appender, Config as LogConfig, Root};
use log4rs::encode::pattern::PatternEncoder;
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::Once;
use tonic_lnd::lnrpc::GetInfoRequest;

static INIT: Once = Once::new();

pub struct Cfg {
pub lnd: LndCfg,
pub log_dir: Option<String>,
}

pub fn init_logger(config: LogConfig) {
INIT.call_once(|| {
log4rs::init_config(config).expect("failed to initialize logger");
});
}

pub async fn run(args: Cfg) -> Result<(), ()> {
let log_dir = args.log_dir.unwrap_or_else(|| {
home_dir()
Expand Down Expand Up @@ -58,7 +67,7 @@ pub async fn run(args: Cfg) -> Result<(), ()> {
)
.unwrap();

let _log_handle = log4rs::init_config(config).unwrap();
init_logger(config);

let mut client = get_lnd_client(args.lnd).expect("failed to connect");

Expand Down

0 comments on commit 2a439c6

Please sign in to comment.