Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allowed custom datadir for loggers #405

Merged
merged 13 commits into from
Feb 19, 2025
Merged

Conversation

0xEgao
Copy link

@0xEgao 0xEgao commented Feb 8, 2025

Please let me know if you'd like me to make any changes, this pr tries to solve the issue #402

Copy link

@mojoX911 mojoX911 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack

src/utill.rs Outdated
@@ -241,40 +243,12 @@ pub fn setup_directory_logger(filter: LevelFilter) {
/// Setup function that will only run once, even if called multiple times.
/// Takes log level to set the desired logging verbosity
// TODO: Use the above setup logger functions.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the todos comments now can be removed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed all the todos

@0xEgao 0xEgao requested a review from mojoX911 February 8, 2025 06:35
@mojoX911
Copy link

mojoX911 commented Feb 9, 2025

@0xEgao there are some test failure. Please look into it. Additionally you can add the git pre-commit hook in your local to always have basic checks covered before committing.

https://github.com/citadel-tech/coinswap?tab=readme-ov-file#setting-up-git-hooks

@0xEgao
Copy link
Author

0xEgao commented Feb 9, 2025

Hey @mojoX911 thanks for the feedback ,i have ran the neccessary tests as u suggested.You can review it now

Copy link

codecov bot commented Feb 9, 2025

Codecov Report

Attention: Patch coverage is 68.75000% with 5 lines in your changes missing coverage. Please review.

Project coverage is 70.43%. Comparing base (a00d05a) to head (4f3cae3).
Report is 37 commits behind head on master.

Files with missing lines Patch % Lines
src/utill.rs 84.61% 2 Missing ⚠️
src/bin/directoryd.rs 0.00% 1 Missing ⚠️
src/bin/makerd.rs 0.00% 1 Missing ⚠️
src/bin/taker.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #405      +/-   ##
==========================================
+ Coverage   70.26%   70.43%   +0.17%     
==========================================
  Files          34       34              
  Lines        4261     4245      -16     
==========================================
- Hits         2994     2990       -4     
+ Misses       1267     1255      -12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -10,6 +10,7 @@ use coinswap::{
},
utill::{read_message, send_message, setup_directory_logger},
};
//use dirs::data_dir;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commented lines like these are left in some places. Please remove them.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have made the changes, please review it.

@0xEgao 0xEgao requested a review from mojoX911 February 9, 2025 14:26
Copy link

@mojoX911 mojoX911 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, few more fixes needed.

@@ -35,9 +35,9 @@ struct Cli {
}

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

setup_directory_logger(log::LevelFilter::Info, None);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pass the args.data_dir value here. This will make the log dir always fixed to default. Same for all other apps.

@@ -43,7 +43,7 @@ fn send_rpc_req(mut stream: TcpStream, req: RpcMsgReq) -> Result<(), DirectorySe
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove the setup logger functions from the cli apps.

@@ -76,7 +76,7 @@ enum Commands {
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same. Remove and check if anything fails in tests. It should not.

@@ -49,8 +49,7 @@ struct Cli {
}

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

setup_maker_logger(log::LevelFilter::Info, None);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass on args.data_dir.

src/bin/taker.rs Outdated
@@ -114,6 +114,7 @@ fn main() -> Result<(), TakerError> {
args.command,
Commands::Recover | Commands::FetchOffers | Commands::Coinswap { .. }
),
None, //default path handled inside the function.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass the args value.

@@ -533,7 +532,7 @@ impl TestFramework {
Arc<DirectoryServer>,
JoinHandle<()>,
) {
setup_logger(log::LevelFilter::Info);
setup_logger(log::LevelFilter::Info, None);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be called after creating the temp folder in the line below. Then pass the temp folder.

src/utill.rs Outdated
Comment on lines 248 to 250
setup_taker_logger(filter, true, data_dir.clone());
setup_maker_logger(filter, data_dir.clone());
setup_directory_logger(filter, data_dir.clone());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will merge all the logs into one file as data_dir value is same for all. You need to pass different subfolder for each logger. Just do data_dir.join)("maker") etc for each case.

This is still not perfect as it will merge all the maker logs together. but its test only, doesn't matter much.

Copy link
Author

@0xEgao 0xEgao Feb 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @mojoX911 is this supposed to be like this

pub fn setup_logger(filter: LevelFilter, data_dir: Option<PathBuf>) {
    Once::new().call_once(|| {
        env::set_var("RUST_LOG", "coinswap=info");
        setup_taker_logger(
            filter,
            true,
            Some(data_dir.clone().unwrap_or_else(get_taker_dir).join("taker")),
        );
        setup_maker_logger(
            filter,
            Some(data_dir.clone().unwrap_or_else(get_maker_dir).join("maker")),
        );
        setup_directory_logger(
            filter,
            Some(
                data_dir
                    .clone()
                    .unwrap_or_else(get_dns_dir)
                    .join("directory"),
            ),
        );
    });
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. You need to do data_dir.and_then(|d| Some(d.join("taker")))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @mojoX911 i have committed all the changes you requested,you can review them now

@0xEgao 0xEgao requested a review from mojoX911 February 15, 2025 08:28
Copy link

@mojoX911 mojoX911 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK. Just one last question.

src/utill.rs Outdated
Comment on lines 178 to 180
if let Err(e) = log4rs::init_config(config) {
eprintln!("Failed to initialize logger: {}", e);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason why we are suppressing the error?

Copy link
Author

@0xEgao 0xEgao Feb 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was getting my local branch specific error,so i tried to handle a particular error case,thanks for asking this,i have revert it back to unwrap(). You can review it.

@0xEgao 0xEgao requested a review from mojoX911 February 17, 2025 07:01
Copy link

@mojoX911 mojoX911 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few nits and its good to go.

src/utill.rs Outdated
Comment on lines 204 to 206
if let Err(e) = log4rs::init_config(config) {
eprintln!("Failed to initialize logger: {}", e);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't suppress the error. unwrap.

src/utill.rs Outdated
Comment on lines 234 to 236
if let Err(e) = log4rs::init_config(config) {
eprintln!("Failed to initialize logger: {}", e);
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have removed all suppressing error now.

src/utill.rs Outdated
Comment on lines 245 to 247
setup_taker_logger(filter, true, data_dir.clone().map(|d| d.join("taker")));
setup_maker_logger(filter, data_dir.clone().map(|d| d.join("maker")));
setup_directory_logger(filter, data_dir.clone().map(|d| d.join("directory")));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think clone is not needed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion,instead of clone() i have used as_ref() for better memory efficiency.

@@ -79,8 +79,7 @@ impl Wallet {
/// ### Behavior
/// - If [SendAmount::Max] is used, the function creates a transaction for the maximum possible
/// value to the specified destination.
/// - If [SendAmount::Amount] is used, a custom value is sent, and any remaining funds
/// are held in a change address, if applicable.
/// - If [SendAmount::Amount] is used, a custom value is sent, and any remaining funds are held in a change address, if applicable.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No contextual change. Remove.

Copy link
Author

@0xEgao 0xEgao Feb 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was recieving an error while doing cargo clippy... in this line so i changed the comment to fit into one line.

@@ -52,10 +52,10 @@ impl TakerCli {
args.push(arg);
}

let output = Command::new("./target/debug/taker")
let output = Command::new(env!("CARGO_BIN_EXE_taker"))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good change. Can you add this to all places in the lib where we are using ./target/debug/... stuffs? This was another fix not listed in the issues, but we can get it done here.

Copy link
Author

@0xEgao 0xEgao Feb 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the complement,i have changed all the ./target/debug/...

@0xEgao 0xEgao requested a review from mojoX911 February 17, 2025 20:49
Copy link

@mojoX911 mojoX911 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack. Awaiting CI.

@0xEgao
Copy link
Author

0xEgao commented Feb 19, 2025

hey @mojoX911 the test is failing because the logging framework only allows one global logger to be set,and an attempt to create more logger result to this test failure,for that i did earlier was suppressing the error, i.e checking if logger was already initialized,do you suggest to go back to this,or any other way?

@mojoX911
Copy link

Ah right. Probably thats the reason why we had the repeated function. Suppressing the error won't work, as they will silently fail the log setup.

lets revert back the original global setup logger, and just pass in custom datadirs into that.

@0xEgao
Copy link
Author

0xEgao commented Feb 19, 2025

Reverting back is an option ,but i think the issue is with Once::new().call_once(...) because it creates new instance every time we call the function, Can we use OnceLock so that it only runs once globally and don't create new instance everytime?

@mojoX911
Copy link

Can we use OnceLock so that it only runs once globally and don't create new instance everytime?

We need to initiate 3 loggers for 3 systems, can we do it with OnceLock with the new design?

@0xEgao
Copy link
Author

0xEgao commented Feb 19, 2025

Hey @mojoX911 now i have implemented the OnceLock ,ran the tests locally everything worked fine for me,you can review that now, happy to contribute :D

@0xEgao 0xEgao requested a review from mojoX911 February 19, 2025 11:27
Copy link

@mojoX911 mojoX911 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack

@mojoX911 mojoX911 merged commit 3bf3a4d into citadel-tech:master Feb 19, 2025
8 checks passed
@0xEgao 0xEgao deleted the datadir branch February 20, 2025 07:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants