Skip to content

Commit

Permalink
Merge pull request #319 from Wukong247/2024-12-03-fix-logger-dir-bug
Browse files Browse the repository at this point in the history
fix logger bug
  • Loading branch information
mojoX911 authored Dec 4, 2024
2 parents e0a772a + 4747c6f commit 6e5ce00
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/bin/directory-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use coinswap::{
directory::DirectoryServerError,
rpc::{RpcMsgReq, RpcMsgResp},
},
utill::{read_message, send_message, setup_logger},
utill::{read_message, send_message, setup_directory_logger},
};

/// directory-cli is a command line app to send RPC messages to directory server.
Expand Down Expand Up @@ -41,7 +41,7 @@ fn send_rpc_req(req: &RpcMsgReq) -> Result<(), DirectoryServerError> {
}

fn main() -> Result<(), DirectoryServerError> {
setup_logger(log::LevelFilter::Info);
setup_directory_logger(log::LevelFilter::Info);
let cli = App::parse();

match cli.command {
Expand Down
4 changes: 2 additions & 2 deletions src/bin/directoryd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use clap::Parser;
use coinswap::{
market::directory::{start_directory_server, DirectoryServer, DirectoryServerError},
tor::setup_mitosis,
utill::{setup_logger, ConnectionType},
utill::{setup_directory_logger, ConnectionType},
};
use std::{path::PathBuf, str::FromStr, sync::Arc};

Expand All @@ -19,7 +19,7 @@ struct Cli {
}

fn main() -> Result<(), DirectoryServerError> {
setup_logger(log::LevelFilter::Info);
setup_directory_logger(log::LevelFilter::Info);

let args = Cli::parse();

Expand Down
4 changes: 2 additions & 2 deletions src/bin/maker-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{net::TcpStream, time::Duration};
use clap::Parser;
use coinswap::{
maker::{MakerError, RpcMsgReq, RpcMsgResp},
utill::{read_message, send_message, setup_logger},
utill::{read_message, send_message, setup_maker_logger},
};

/// maker-cli is a command line app to send RPC messages to maker server.
Expand Down Expand Up @@ -50,7 +50,7 @@ enum Commands {
}

fn main() -> Result<(), MakerError> {
setup_logger(log::LevelFilter::Info);
setup_maker_logger(log::LevelFilter::Info);
let cli = App::parse();

match cli.command {
Expand Down
4 changes: 2 additions & 2 deletions src/bin/makerd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use clap::Parser;
use coinswap::{
maker::{start_maker_server, Maker, MakerBehavior, MakerError},
tor::setup_mitosis,
utill::{parse_proxy_auth, setup_logger, ConnectionType},
utill::{parse_proxy_auth, setup_maker_logger, ConnectionType},
wallet::RPCConfig,
};
use std::{path::PathBuf, str::FromStr, sync::Arc};
Expand Down Expand Up @@ -52,7 +52,7 @@ struct Cli {
}

fn main() -> Result<(), MakerError> {
setup_logger(log::LevelFilter::Info);
setup_maker_logger(log::LevelFilter::Info);

let args = Cli::parse();

Expand Down
4 changes: 2 additions & 2 deletions src/bin/taker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use bitcoind::bitcoincore_rpc::{json::ListUnspentResultEntry, Auth};
use clap::Parser;
use coinswap::{
taker::{error::TakerError, SwapParams, Taker, TakerBehavior},
utill::{parse_proxy_auth, setup_logger, ConnectionType},
utill::{parse_proxy_auth, setup_taker_logger, ConnectionType},
wallet::{Destination, RPCConfig, SendAmount},
};
use log::LevelFilter;
Expand Down Expand Up @@ -143,7 +143,7 @@ fn main() -> Result<(), TakerError> {
},
};

setup_logger(log_level);
setup_taker_logger(log_level);

match args.command {
Commands::SeedUtxo => {
Expand Down
63 changes: 63 additions & 0 deletions src/utill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,69 @@ pub fn seed_phrase_to_unique_id(seed: &str) -> String {
hash
}

pub fn setup_taker_logger(filter: LevelFilter) {
env::set_var("RUST_LOG", "coinswap=info");
let log_dir = get_taker_dir().join("debug.log");

let stdout = ConsoleAppender::builder().build();
let file_appender = FileAppender::builder().build(log_dir).unwrap();

let config = Config::builder()
.appender(Appender::builder().build("stdout", Box::new(stdout)))
.appender(Appender::builder().build("file", Box::new(file_appender)))
.logger(
Logger::builder()
.appender("file")
.build("coinswap::taker", filter),
)
.build(Root::builder().appender("stdout").build(filter))
.unwrap();

log4rs::init_config(config).unwrap();
}

pub fn setup_maker_logger(filter: LevelFilter) {
env::set_var("RUST_LOG", "coinswap=info");
let log_dir = get_maker_dir().join("debug.log");

let stdout = ConsoleAppender::builder().build();
let file_appender = FileAppender::builder().build(log_dir).unwrap();

let config = Config::builder()
.appender(Appender::builder().build("stdout", Box::new(stdout)))
.appender(Appender::builder().build("file", Box::new(file_appender)))
.logger(
Logger::builder()
.appender("file")
.build("coinswap::maker", filter),
)
.build(Root::builder().appender("stdout").build(filter))
.unwrap();

log4rs::init_config(config).unwrap();
}

pub fn setup_directory_logger(filter: LevelFilter) {
env::set_var("RUST_LOG", "coinswap=info");
let log_dir = get_dns_dir().join("debug.log");

let stdout = ConsoleAppender::builder().build();
let file_appender = FileAppender::builder().build(log_dir).unwrap();

let config = Config::builder()
.appender(Appender::builder().build("stdout", Box::new(stdout)))
.appender(Appender::builder().build("file", Box::new(file_appender)))
.logger(
Logger::builder()
.appender("file")
.build("coinswap::market", filter),
)
.build(Root::builder().appender("stdout").build(filter))
.unwrap();

log4rs::init_config(config).unwrap();
}

/// Setup function that will only run once, even if called multiple times.
/// Takes log level to set the desired logging verbosity
pub fn setup_logger(filter: LevelFilter) {
Expand Down

0 comments on commit 6e5ce00

Please sign in to comment.