-
Notifications
You must be signed in to change notification settings - Fork 63
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
Add a log lines for datadir, conf file and options #204
Conversation
teos/src/main.rs
Outdated
let cloned_conf_file_path = conf_file_path.clone(); | ||
|
||
// Log datadir path | ||
log::info!("Using data directory: {:?}", &path); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned by @sr-gi in the conversation below, the data directory information is stored in path_network
variable instead of the path variable. Kindly look into it.
reference: #202 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed. Given the tower can be run in multiple networks using the same root dir, the data dir needs to include the network path (check in the issue how bitcoind
reports this, we're trying to follow the exact same pattern).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned by @sr-gi in the conversation below, the data directory information is stored in
path_network
variable instead of the path variable. Kindly look into it.reference: #202 (comment)
It's the default data directory
teos/src/main.rs
Outdated
let is_default = conf.is_default(); | ||
conf.patch_with_options(opt); | ||
conf.verify().unwrap_or_else(|e| { | ||
eprintln!("{e}"); | ||
std::process::exit(1); | ||
}); | ||
|
||
// Log config file path and any non-default options | ||
log::info!("Using configuration file: {:?}", &cloned_conf_file_path); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing items as per the requirements of issue #202 -
- Logging the default data directory
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing items as per the requirements of issue #202 -
- Logging the default data directory
Added the default data dir now and working on the network dir that we are currently using.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Building on top of @void-ness review. Check comments inline.
Also, this is not logging a single new line, even though you've added some logging. This is because the logs are called before the logger is initialized, which happens here: https://github.com/talaia-labs/rust-teos/blob/master/teos/src/main.rs#L88-L104. Please do test functionality locally before submitting a PR.
teos/src/main.rs
Outdated
let cloned_conf_file_path = conf_file_path.clone(); | ||
|
||
// Log datadir path | ||
log::info!("Using data directory: {:?}", &path); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed. Given the tower can be run in multiple networks using the same root dir, the data dir needs to include the network path (check in the issue how bitcoind
reports this, we're trying to follow the exact same pattern).
teos/src/main.rs
Outdated
let conf_file_path = path.join("teos.toml"); | ||
|
||
// Clone the conf_file_path variable before passing it to the from_file function | ||
let cloned_conf_file_path = conf_file_path.clone(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cloning this is not necessary. Instead, you can make config::from_file
receive &Pathbuf
instead of Pathbuf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cloning this is not necessary. Instead, you can make
config::from_file
receive&Pathbuf
instead ofPathbuf
I tried with &Pathbuf but it says expected struct Pathbuf, found &Pathbuf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cloning this is not necessary. Instead, you can make
config::from_file
receive&Pathbuf
instead ofPathbuf
I did reference it but shows error
`let mut conf = config::from_file::(&conf_file_path);
let is_default = conf.is_default();
conf.patch_with_options(opt);
conf.verify().unwrap_or_else(|e| {
eprintln!("{e}");
std::process::exit(1);
});
// log default data dir
log::info!("Using default data directory: {:?}", &path);
// Log config file path and any non-default options
log::info!("Using configuration file: {:?}", &conf_file_path);`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cloning this is not necessary. Instead, you can make
config::from_file
receive&Pathbuf
instead ofPathbuf
If I log the config file path before config::from_file, it is not showing error. So, I did this first log::info!("Using configuration file: {:?}", conf_file_path);
then let mut conf = config::from_file::<Config>(conf_file_path);
. It is working now.
Sorry. I'll change it to the requirements and make a pr again. |
No worries. You don't need to create a new PR btw, just append the fixes to this one. |
Yes I'll do that only. My midsems exams are ending tomorrow so after that I'll be free. Will give my full time from tomorrow. |
teos/src/main.rs
Outdated
let conf_file_path = path.join("teos.toml"); | ||
|
||
// Log config file path | ||
log::info!("Using configuration file: {:?}", conf_file_path); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still being logged before the logger is initialized, hence it would never be shown
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still being logged before the logger is initialized, hence it would never be shown
Tried to log it after logger but shows error move occurs because conf_file_path has type pathbuf , which does implement the copy trait
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still being logged before the logger is initialized, hence it would never be shown
Tried to log it after logger but shows error
move occurs because conf_file_path has type pathbuf , which does implement the copy trait
So, I did logged before the config::from_file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make it so Config:: from_file
borrows a Pathbuf
instead of taking ownership of it, then you'll be able to just pass a reference to conf_file_path
instead of moving it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I referenced it with let mut conf = config::from_file::<Config>(&conf_file_path);
but still shows error and states consider removing the borrow
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I referenced it with
let mut conf = config::from_file::<Config>(&conf_file_path);
but still shows error and statesconsider removing the borrow
.
Like this:-
`async fn main() {
let opt = Opt::from_args();
let path = config::data_dir_absolute_path(opt.data_dir.clone());
let conf_file_path = path.join("teos.toml");
// Create data dir if it does not exist
fs::create_dir_all(&path).unwrap_or_else(|e| {
eprintln!("Cannot create data dir: {e:?}");
std::process::exit(1);
});
// Load conf (from file or defaults) and patch it with the command line parameters received (if any)
let mut conf = config::from_file::<Config>(&conf_file_path);
let is_default = conf.is_default();
conf.patch_with_options(opt);
conf.verify().unwrap_or_else(|e| {
eprintln!("{e}");
std::process::exit(1);
});`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I referenced it with
let mut conf = config::from_file::<Config>(&conf_file_path);
but still shows error and statesconsider removing the borrow
.Like this:-
`async fn main() { let opt = Opt::from_args(); let path = config::data_dir_absolute_path(opt.data_dir.clone()); let conf_file_path = path.join("teos.toml"); // Create data dir if it does not exist fs::create_dir_all(&path).unwrap_or_else(|e| { eprintln!("Cannot create data dir: {e:?}"); std::process::exit(1); });
// Load conf (from file or defaults) and patch it with the command line parameters received (if any) let mut conf = config::from_file::<Config>(&conf_file_path); let is_default = conf.is_default(); conf.patch_with_options(opt); conf.verify().unwrap_or_else(|e| { eprintln!("{e}"); std::process::exit(1); });`
Then after log I have added this:-
`
// Set log level
SimpleLogger::new()
.with_level(if conf.deps_debug {
LevelFilter::Debug
} else {
LevelFilter::Warn
})
.with_module_level(
"teos",
if conf.debug {
LevelFilter::Debug
} else {
LevelFilter::Info
},
)
.init()
.unwrap();
// log default data dir
log::info!("Using default data directory: {:?}", &path);
// Log config file path
log::info!("Using configuration file: {:?}", &conf_file_path);`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I referenced it with
let mut conf = config::from_file::<Config>(&conf_file_path);
but still shows error and statesconsider removing the borrow
.
That doesn't make sense. Have you updated the method signature? Just tested it and it works.
diff --git a/teos/src/cli.rs b/teos/src/cli.rs
index 5c20f00..eba032b 100644
--- a/teos/src/cli.rs
+++ b/teos/src/cli.rs
@@ -27,7 +27,7 @@ async fn main() {
let command = opt.command.clone();
// Load conf (from file or defaults) and patch it with the command line parameters received (if any)
- let mut conf = config::from_file::<Config>(path.join("teos.toml"));
+ let mut conf = config::from_file::<Config>(&path.join("teos.toml"));
conf.patch_with_options(opt);
let key = fs::read(&path.join("client-key.pem"))
diff --git a/teos/src/config.rs b/teos/src/config.rs
index 1669b25..50fb8a3 100644
--- a/teos/src/config.rs
+++ b/teos/src/config.rs
@@ -16,7 +16,7 @@ pub fn data_dir_absolute_path(data_dir: String) -> PathBuf {
}
}
-pub fn from_file<T: Default + serde::de::DeserializeOwned>(path: PathBuf) -> T {
+pub fn from_file<T: Default + serde::de::DeserializeOwned>(path: &PathBuf) -> T {
match std::fs::read(path) {
Ok(file_content) => toml::from_slice::<T>(&file_content).map_or_else(
|e| {
diff --git a/teos/src/main.rs b/teos/src/main.rs
index e7b8f37..1ab249e 100644
--- a/teos/src/main.rs
+++ b/teos/src/main.rs
@@ -71,9 +71,6 @@ async fn main() {
let path = config::data_dir_absolute_path(opt.data_dir.clone());
let conf_file_path = path.join("teos.toml");
- // Log config file path
- log::info!("Using configuration file: {:?}", conf_file_path);
-
// Create data dir if it does not exist
fs::create_dir_all(&path).unwrap_or_else(|e| {
eprintln!("Cannot create data dir: {e:?}");
@@ -81,7 +78,7 @@ async fn main() {
});
// Load conf (from file or defaults) and patch it with the command line parameters received (if any)
- let mut conf = config::from_file::<Config>(conf_file_path);
+ let mut conf = config::from_file::<Config>(&conf_file_path);
let is_default = conf.is_default();
conf.patch_with_options(opt);
conf.verify().unwrap_or_else(|e| {
@@ -89,9 +86,6 @@ async fn main() {
std::process::exit(1);
});
- // log default data dir
- log::info!("Using default data directory: {:?}", &path);
-
// log the non-default options that the daemon is using
if !is_default {
if conf.api_bind != "localhost" {
@@ -165,6 +159,12 @@ async fn main() {
log::info!("Loading configuration from file")
}
+ // Log config file path
+ log::info!("Using configuration file: {:?}", conf_file_path);
+
+ // log default data dir
+ log::info!("Using default data directory: {:?}", &path);
+
// Create network dir
let path_network = path.join(conf.btc_network.clone());
fs::create_dir_all(&path_network).unwrap_or_else(|e| {
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I referenced it with
let mut conf = config::from_file::<Config>(&conf_file_path);
but still shows error and statesconsider removing the borrow
.That doesn't make sense. Have you updated the method signature? Just tested it and it works.
diff --git a/teos/src/cli.rs b/teos/src/cli.rs index 5c20f00..eba032b 100644 --- a/teos/src/cli.rs +++ b/teos/src/cli.rs @@ -27,7 +27,7 @@ async fn main() { let command = opt.command.clone(); // Load conf (from file or defaults) and patch it with the command line parameters received (if any) - let mut conf = config::from_file::<Config>(path.join("teos.toml")); + let mut conf = config::from_file::<Config>(&path.join("teos.toml")); conf.patch_with_options(opt); let key = fs::read(&path.join("client-key.pem")) diff --git a/teos/src/config.rs b/teos/src/config.rs index 1669b25..50fb8a3 100644 --- a/teos/src/config.rs +++ b/teos/src/config.rs @@ -16,7 +16,7 @@ pub fn data_dir_absolute_path(data_dir: String) -> PathBuf { } } -pub fn from_file<T: Default + serde::de::DeserializeOwned>(path: PathBuf) -> T { +pub fn from_file<T: Default + serde::de::DeserializeOwned>(path: &PathBuf) -> T { match std::fs::read(path) { Ok(file_content) => toml::from_slice::<T>(&file_content).map_or_else( |e| { diff --git a/teos/src/main.rs b/teos/src/main.rs index e7b8f37..1ab249e 100644 --- a/teos/src/main.rs +++ b/teos/src/main.rs @@ -71,9 +71,6 @@ async fn main() { let path = config::data_dir_absolute_path(opt.data_dir.clone()); let conf_file_path = path.join("teos.toml"); - // Log config file path - log::info!("Using configuration file: {:?}", conf_file_path); - // Create data dir if it does not exist fs::create_dir_all(&path).unwrap_or_else(|e| { eprintln!("Cannot create data dir: {e:?}"); @@ -81,7 +78,7 @@ async fn main() { }); // Load conf (from file or defaults) and patch it with the command line parameters received (if any) - let mut conf = config::from_file::<Config>(conf_file_path); + let mut conf = config::from_file::<Config>(&conf_file_path); let is_default = conf.is_default(); conf.patch_with_options(opt); conf.verify().unwrap_or_else(|e| { @@ -89,9 +86,6 @@ async fn main() { std::process::exit(1); }); - // log default data dir - log::info!("Using default data directory: {:?}", &path); - // log the non-default options that the daemon is using if !is_default { if conf.api_bind != "localhost" { @@ -165,6 +159,12 @@ async fn main() { log::info!("Loading configuration from file") } + // Log config file path + log::info!("Using configuration file: {:?}", conf_file_path); + + // log default data dir + log::info!("Using default data directory: {:?}", &path); + // Create network dir let path_network = path.join(conf.btc_network.clone()); fs::create_dir_all(&path_network).unwrap_or_else(|e| {
Yes, working now. Forgot to update the borrow of cli.rs file. Thank you!
teos/src/main.rs
Outdated
if !is_default { | ||
if conf.api_bind != "localhost" { | ||
log::info!("Using non-default API bind address: {}", conf.api_bind); | ||
} | ||
if conf.api_port != 9814 { | ||
log::info!("Using non-default API port: {}", conf.api_port); | ||
} | ||
if conf.rpc_bind != "localhost" { | ||
log::info!("Using non-default RPC bind address: {}", conf.rpc_bind); | ||
} | ||
if conf.rpc_port != 8814 { | ||
log::info!("Using non-default RPC port: {}", conf.rpc_port); | ||
} | ||
if conf.btc_network != "mainnet" { | ||
log::info!("Using non-default Bitcoin network: {}", conf.btc_network); | ||
} | ||
if conf.btc_rpc_user != "user" { | ||
log::info!("Using non-default Bitcoin RPC user: {}", conf.btc_rpc_user); | ||
} | ||
if conf.btc_rpc_password != "passwd" { | ||
log::info!( | ||
"Using non-default Bitcoin RPC password: {}", | ||
conf.btc_rpc_password | ||
); | ||
} | ||
if conf.btc_rpc_connect != "localhost" { | ||
log::info!( | ||
"Using non-default Bitcoin RPC connect address: {}", | ||
conf.btc_rpc_connect | ||
); | ||
} | ||
if conf.btc_rpc_port != 0 { | ||
log::info!("Using non-default Bitcoin RPC port: {}", conf.btc_network); | ||
} | ||
if conf.tor_control_port != 9051 { | ||
println!( | ||
"Using non-default Tor control port: {}", | ||
conf.tor_control_port | ||
); | ||
} | ||
if conf.onion_hidden_service_port != 9814 { | ||
println!( | ||
"Using non-default onion hidden service port: {}", | ||
conf.onion_hidden_service_port | ||
); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the right way to handle this. We don't want to add all this boilerplate to main. Also, most of this info is already in Config. The right way to handle this will most likely involve the Default and Eq traits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the right way to handle this. We don't want to add all this boilerplate to main. Also, most of this info is already in Config. The right way to handle this will most likely involve the Default and Eq traits.
So, I have to log the non-default options and Eq traits and remove the ones who has default value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not the right way to handle this. We don't want to add all this boilerplate to main. Also, most of this info is already in Config. The right way to handle this will most likely involve the Default and Eq traits.
Please check the latest commit and let me know if it's correct or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check the latest commit and let me know if it's correct or not.
if conf.btc_rpc_port != 0 {
log::info!("Using non-default Bitcoin RPC port: {}", conf.btc_network);
}
if conf.btc_rpc_user != "user" {
log::info!("Using non-default Bitcoin RPC user: {}", conf.btc_rpc_user);
}
if conf.btc_rpc_password != "passwd" {
log::info!("Using non-default Bitcoin RPC password: {}", conf.btc_rpc_password);
}
if conf.btc_network != "mainnet" {
log::info!("Using non-default Bitcoin network: {}", conf.btc_network);
}
if conf.btc_rpc_connect != "localhost" {
log::info!("Using non-default Bitcoin RPC connect address: {}", conf.btc_rpc_connect);
}
You are still using hard coded default values ("mainnet", "localhost", etc...) but you could create a default config struct and compare it against conf
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check the latest commit and let me know if it's correct or not.
if conf.btc_rpc_port != 0 { log::info!("Using non-default Bitcoin RPC port: {}", conf.btc_network); } if conf.btc_rpc_user != "user" { log::info!("Using non-default Bitcoin RPC user: {}", conf.btc_rpc_user); } if conf.btc_rpc_password != "passwd" { log::info!("Using non-default Bitcoin RPC password: {}", conf.btc_rpc_password); } if conf.btc_network != "mainnet" { log::info!("Using non-default Bitcoin network: {}", conf.btc_network); } if conf.btc_rpc_connect != "localhost" { log::info!("Using non-default Bitcoin RPC connect address: {}", conf.btc_rpc_connect); }
You are still using hard coded default values ("mainnet", "localhost", etc...) but you could create a default config struct and compare it against
conf
instead.
So, I should create a default create config struct in config.rs and match it main using conf?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, I should create a default config struct
Yeah. But it's already there. Config
already implements std::Default
.
and match it main using conf?
Try to keep such checks off of main.rs
so not to bloat it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, I should create a default config struct
Yeah. But it's already there.
Config
already implementsstd::Default
changed now. please let me know if it's ok now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are still bloating main with the comparisons. Just implement a function in config that deals with the diff between the given Config
and the default one. It could be something along the lines of a method that returns a HashMap
where keys are the config names, and values are a tuple of the expected value and the received value. Then, you can simply iterate through the map in main and log accordingly.
@sr-gi please check the latest commit. |
@sr-gi tested my latest commit and it works fine. Please check once and let me know if I'm still missing something. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All commits need to be squashed.
Just a couple more changes, my bad on suggesting something that feels a bit weird ui-wise :(
teos/src/main.rs
Outdated
@@ -119,6 +135,9 @@ async fn main() { | |||
DBM::new(path_network.join("teos_db.sql3")).unwrap(), | |||
)); | |||
|
|||
// Log datadir path |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's move this some lines up some it is before the options, so right after log::info!("Config file: {:?}", &conf_file_path);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's move this some lines up some it is before the options, so right after
log::info!("Config file: {:?}", &conf_file_path);
If I move this some lines up it shows error as path_network
is defined below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Define path_network
first then? I don't really see the issue.
There's also the option of moving everything else down.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Define
path_network
first then? I don't really see the issue.There's also the option of moving everything else down.
okay
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Define
path_network
first then? I don't really see the issue.There's also the option of moving everything else down.
Done. Can I squash all the commits now as there is no issue left to solve?
Something else to have in mind (I just realized about this while testing it) but that can be left for a follow-up: right now we'll be logging something along the lines of:
For each arg that has been changed. We should note what made the change, so a follow-up PR should transform that log into either:
or
For context, this is exactly how |
Do I need to modify the |
@sr-gi Can I squash all commits now? |
Yes, once every comment has been addressed. It should all be a single commit. |
…aia-labs#202 Following up from talaia-labs#204. This commit enhances the logging output to provide clearer information about the data directory and configuration file details. By providing this information, it improves the user experience and make it easier for users to find and understand the relevant paths and settings. Suggestions added
fixes: #202
cargo fmt
andcargo clippy