diff --git a/Cargo.lock b/Cargo.lock index 9a85afe..996fed2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -898,9 +898,9 @@ dependencies = [ [[package]] name = "httparse" -version = "1.9.4" +version = "1.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fcc0b4a115bf80b728eb8ea024ad5bd707b615bfed49e0665b6e0f86fd082d9" +checksum = "7d71d3574edd2771538b901e6549113b4006ece66150fb69c0fb6d9a2adae946" [[package]] name = "httpdate" @@ -1148,7 +1148,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "536e7c782167a2d48346ca0b2677fad19eaef20f19a4ab868e4d5b96ca879def" dependencies = [ "bech32", - "reqwest 0.12.7", + "reqwest 0.12.8", "serde", "serde_json", ] @@ -1178,6 +1178,15 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "matchers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" +dependencies = [ + "regex-automata 0.1.10", +] + [[package]] name = "memchr" version = "2.7.4" @@ -1260,7 +1269,7 @@ dependencies = [ "negentropy 0.3.1", "negentropy 0.4.3", "once_cell", - "reqwest 0.12.7", + "reqwest 0.12.8", "scrypt", "serde", "serde_json", @@ -1273,7 +1282,7 @@ dependencies = [ [[package]] name = "nostr-commander" -version = "0.3.0" +version = "0.3.1" dependencies = [ "anyhow", "atty", @@ -1703,8 +1712,17 @@ checksum = "38200e5ee88914975b69f657f0801b6f6dccafd44fd9326302a4aaeecfacb1d8" dependencies = [ "aho-corasick", "memchr", - "regex-automata", - "regex-syntax", + "regex-automata 0.4.8", + "regex-syntax 0.8.5", +] + +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax 0.6.29", ] [[package]] @@ -1715,9 +1733,15 @@ checksum = "368758f23274712b504848e9d5a6f010445cc8b87a7cdb4d7cbee666c1288da3" dependencies = [ "aho-corasick", "memchr", - "regex-syntax", + "regex-syntax 0.8.5", ] +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + [[package]] name = "regex-syntax" version = "0.8.5" @@ -1770,9 +1794,9 @@ dependencies = [ [[package]] name = "reqwest" -version = "0.12.7" +version = "0.12.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f8f4955649ef5c38cc7f9e8aa41761d48fb9677197daea9984dc54f56aad5e63" +checksum = "f713147fbe92361e52392c73b8c9e48c04c6625bce969ef54dc901e58e042a7b" dependencies = [ "base64 0.22.1", "bytes", @@ -1793,7 +1817,7 @@ dependencies = [ "pin-project-lite", "quinn", "rustls 0.23.13", - "rustls-pemfile 2.1.3", + "rustls-pemfile 2.2.0", "rustls-pki-types", "serde", "serde_json", @@ -1889,11 +1913,10 @@ dependencies = [ [[package]] name = "rustls-pemfile" -version = "2.1.3" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "196fe16b00e106300d3e45ecfcb764fa292a535d7326a29a5875c579c7417425" +checksum = "dce314e5fee3f39953d46bb63bb8a46d40c2f8fb7cc5a3b6cab2bde9721d6e50" dependencies = [ - "base64 0.22.1", "rustls-pki-types", ] @@ -2442,10 +2465,14 @@ version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" dependencies = [ + "matchers", "nu-ansi-term", + "once_cell", + "regex", "sharded-slab", "smallvec", "thread_local", + "tracing", "tracing-core", "tracing-log", ] diff --git a/Cargo.toml b/Cargo.toml index b1a716b..f85d9f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ [package] name = "nostr-commander" -version = "0.3.0" +version = "0.3.1" edition = "2021" description = "simple but convenient CLI-based Nostr client app for publishing,sending and subscribing" documentation = "https://docs.rs/nostr-commander" @@ -31,8 +31,8 @@ thiserror = "1.0" serde = { version = "1.0", features = ["derive"] } url = { version = "2.4", features = ["serde"] } serde_json = "1.0" -tracing-subscriber = "0.3" tracing = "0.1" +tracing-subscriber = { version = "0.3", features = ["env-filter"] } directories = "5.0" update-informer = "1.1" chrono = "0.4" diff --git a/README.md b/README.md index f656982..067e415 100644 --- a/README.md +++ b/README.md @@ -209,16 +209,27 @@ Options: default log level set with environment variable 'RUST_LOG' will be used. If used, log level will be set to 'DEBUG' and debugging information will be printed. '-d' is a shortcut for '--log-level - DEBUG'. See also '--log-level'. '-d' takes precedence over - '--log-level'. Additionally, have a look also at the option - '--verbose' - - --log-level + DEBUG'. If used once as in '-d' it will set and/or overwrite + --log-level to '--log-level debug'. If used twice as in '-d -d' it + will set and/or overwrite --log-level to '--log-level debug debug'. + And third or futher occurance of '-d' will be ignored. See also + '--log-level'. '-d' takes precedence over '--log-level'. + Additionally, have a look also at the option '--verbose' + + --log-level ... Set the log level by overwriting the default log level. Details:: If not used, then the default log level set with environment variable - 'RUST_LOG' will be used. See also '--debug' and '--verbose' - - [default: none] + 'RUST_LOG' will be used. If used with one value specified this value + is assigned to the log level of matrix-commander-rs. If used with two + values specified the first value is assigned to the log level of + matrix-commander-rs. The second value is asigned to the lower level + modules. More than two values should not be specified. --debug + overwrites -log-level. See also '--debug' and '--verbose'. + Alternatively you can use the RUST_LOG environment variable. An + example use of RUST_LOG is to use neither --log-level nor --debug, + and to set RUST_LOG="error,matrix_commander_rs=debug" which turns off + debugging on all lower level modules and turns debugging on only for + matrix-commander-rs Possible values: - none: None: not set, default diff --git a/VERSION b/VERSION index 0d91a54..9e11b32 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.3.0 +0.3.1 diff --git a/help.help.txt b/help.help.txt index b1b127c..a7c9817 100644 --- a/help.help.txt +++ b/help.help.txt @@ -21,7 +21,7 @@ Options: Prints README.md file, the documenation in Markdown. -d, --debug... Overwrite the default log level. - --log-level + --log-level ... Set the log level by overwriting the default log level. --verbose... Set the verbosity level. diff --git a/help.manual.txt b/help.manual.txt index 62b0f8a..2d26a50 100644 --- a/help.manual.txt +++ b/help.manual.txt @@ -48,16 +48,27 @@ Options: default log level set with environment variable 'RUST_LOG' will be used. If used, log level will be set to 'DEBUG' and debugging information will be printed. '-d' is a shortcut for '--log-level - DEBUG'. See also '--log-level'. '-d' takes precedence over - '--log-level'. Additionally, have a look also at the option - '--verbose' - - --log-level + DEBUG'. If used once as in '-d' it will set and/or overwrite + --log-level to '--log-level debug'. If used twice as in '-d -d' it + will set and/or overwrite --log-level to '--log-level debug debug'. + And third or futher occurance of '-d' will be ignored. See also + '--log-level'. '-d' takes precedence over '--log-level'. + Additionally, have a look also at the option '--verbose' + + --log-level ... Set the log level by overwriting the default log level. Details:: If not used, then the default log level set with environment variable - 'RUST_LOG' will be used. See also '--debug' and '--verbose' - - [default: none] + 'RUST_LOG' will be used. If used with one value specified this value + is assigned to the log level of matrix-commander-rs. If used with two + values specified the first value is assigned to the log level of + matrix-commander-rs. The second value is asigned to the lower level + modules. More than two values should not be specified. --debug + overwrites -log-level. See also '--debug' and '--verbose'. + Alternatively you can use the RUST_LOG environment variable. An + example use of RUST_LOG is to use neither --log-level nor --debug, + and to set RUST_LOG="error,matrix_commander_rs=debug" which turns off + debugging on all lower level modules and turns debugging on only for + matrix-commander-rs Possible values: - none: None: not set, default diff --git a/help.usage.txt b/help.usage.txt index 235306e..9f43414 100644 --- a/help.usage.txt +++ b/help.usage.txt @@ -7,7 +7,7 @@ Options: --manual --readme -d, --debug... - --log-level + --log-level ... --verbose... -c, --credentials --create-user diff --git a/src/main.rs b/src/main.rs index a2301e5..6dda354 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,7 +39,7 @@ use std::str::FromStr; use std::time::Duration; use thiserror::Error; use tracing::{debug, enabled, error, info, trace, warn, Level}; -use tracing_subscriber; +use tracing_subscriber::EnvFilter; use update_informer::{registry, Check}; use nostr_sdk::prelude::*; @@ -61,6 +61,8 @@ const PKG_NAME: &str = "nostr-commander"; const BIN_NAME_O: Option<&str> = option_env!("CARGO_BIN_NAME"); /// fallback if static compile time value is None const BIN_NAME: &str = "nostr-commander-rs"; +/// fallback if static compile time value is None +const BIN_NAME_UNDERSCORE: &str = "nostr_commander_rs"; /// he repo name from Cargo.toml at compile time, /// e.g. string `https://github.com/8go/nostr-commander-rs/` const PKG_REPOSITORY_O: Option<&str> = option_env!("CARGO_PKG_REPOSITORY"); @@ -150,8 +152,8 @@ pub enum Error { #[error("Unknown CLI parameter")] UnknownCliParameter, - #[error("Unsupported CLI parameter")] - UnsupportedCliParameter, + #[error("Unsupported CLI parameter: {0}")] + UnsupportedCliParameter(&'static str), #[error("Missing User")] MissingUser, @@ -396,6 +398,11 @@ pub struct Args { /// If used, log level will be set to 'DEBUG' and debugging information /// will be printed. /// '-d' is a shortcut for '--log-level DEBUG'. + /// If used once as in '-d' it will set and/or overwrite + /// --log-level to '--log-level debug'. + /// If used twice as in '-d -d' it will set and/or overwrite + /// --log-level to '--log-level debug debug'. + /// And third or futher occurance of '-d' will be ignored. /// See also '--log-level'. '-d' takes precedence over '--log-level'. /// Additionally, have a look also at the option '--verbose'. #[arg(short, long, action = clap::ArgAction::Count, default_value_t = 0u8, )] @@ -405,11 +412,23 @@ pub struct Args { /// Details:: /// If not used, then the default /// log level set with environment variable 'RUST_LOG' will be used. + /// If used with one value specified this value is assigned to the + /// log level of matrix-commander-rs. + /// If used with two values specified the first value is assigned to the + /// log level of matrix-commander-rs. The second value is asigned to the + /// lower level modules. + /// More than two values should not be specified. + /// --debug overwrites -log-level. /// See also '--debug' and '--verbose'. + /// Alternatively you can use the RUST_LOG environment variable. + /// An example use of RUST_LOG is to use neither --log-level nor --debug, + /// and to set RUST_LOG="error,matrix_commander_rs=debug" which turns + /// off debugging on all lower level modules and turns debugging on only + /// for matrix-commander-rs. // Possible values are // '{trace}', '{debug}', '{info}', '{warn}', and '{error}'. - #[arg(long, value_enum, default_value_t = LogLevel::default(), ignore_case = true, )] - log_level: LogLevel, + #[arg(long, value_delimiter = ' ', num_args = 1..3, ignore_case = true, )] + log_level: Option>, /// Set the verbosity level. /// Details:: @@ -896,7 +915,7 @@ impl Args { contribute: false, version: None, debug: 0u8, - log_level: LogLevel::None, + log_level: None, verbose: 0u8, // plain: false, // credentials file path @@ -982,7 +1001,7 @@ impl Relay { /// future access. #[derive(Clone, Debug, Serialize, Deserialize)] pub struct Credentials { - secret_key_bech32: String, // nsec1...// private key + secret_key_bech32: String, // nsec1...// private_key public_key_bech32: String, // npub1... relays: Vec, metadata: Metadata, @@ -1591,7 +1610,7 @@ pub(crate) fn is_relay_url(relay: &Url) -> bool { /// Handle the --create_user CLI argument pub(crate) fn cli_create_user(ap: &mut Args) -> Result<(), Error> { if !ap.create_user { - return Err(Error::UnsupportedCliParameter); + return Err(Error::MissingCliParameter); } if credentials_exist(ap) { error!(concat!( @@ -2874,34 +2893,100 @@ async fn main() -> Result<(), Error> { // handle log level and debug options let env_org_rust_log = env::var("RUST_LOG").unwrap_or_default().to_uppercase(); - // eprintln!("Original log_level option is {:?}", ap.log_level); - // eprintln!("Original RUST_LOG is {:?}", &env_org_rust_log); + // println!("Original log_level option is {:?}", ap.log_level); + // println!("Original RUST_LOG is {:?}", &env_org_rust_log); + match ap.debug.cmp(&1) { + Ordering::Equal => { + // -d overwrites --log-level + let llvec = vec![LogLevel::Debug]; + ap.log_level = Some(llvec); + } + Ordering::Greater => { + // -d overwrites --log-level + let mut llvec = vec![LogLevel::Debug]; + llvec.push(LogLevel::Debug); + ap.log_level = Some(llvec); + } + Ordering::Less => (), + } + match ap.log_level.clone() { + None => { + tracing_subscriber::fmt() + .with_writer(io::stderr) + .with_env_filter(EnvFilter::from_default_env()) // support the standard RUST_LOG env variable + .init(); + debug!("Neither --debug nor --log-level was used. Using environment vaiable RUST_LOG."); + } + Some(llvec) => { + if llvec.len() == 1 { + if llvec[0].is_none() { + return Err(Error::UnsupportedCliParameter( + "Value 'none' not allowed for --log-level argument", + )); + } + // .with_env_filter("matrix_commander_rs=debug") // only set matrix_commander_rs + let mut rlogstr: String = BIN_NAME_UNDERSCORE.to_owned(); + rlogstr.push('='); // add char + rlogstr.push_str(&llvec[0].to_string()); + tracing_subscriber::fmt() + .with_writer(io::stderr) + .with_env_filter(rlogstr.clone()) // support the standard RUST_LOG env variable for default value + .init(); + debug!( + "The --debug or --log-level was used once or with one value. \ + Specifying logging equivalent to RUST_LOG seting of '{}'.", + rlogstr + ); + } else { + if llvec[0].is_none() || llvec[1].is_none() { + return Err(Error::UnsupportedCliParameter( + "Value 'none' not allowed for --log-level argument", + )); + } + // RUST_LOG="error,matrix_commander_rs=debug" .. This will only show matrix-comander-rs + // debug info, and erors for all other modules + let mut rlogstr: String = llvec[1].to_string().to_owned(); + rlogstr.push(','); // add char + rlogstr.push_str(BIN_NAME_UNDERSCORE); + rlogstr.push('='); + rlogstr.push_str(&llvec[0].to_string()); + tracing_subscriber::fmt() + .with_writer(io::stderr) + .with_env_filter(rlogstr.clone()) + .init(); + debug!( + "The --debug or --log-level was used twice or with two values. \ + Specifying logging equivalent to RUST_LOG seting of '{}'.", + rlogstr + ); + } + if llvec.len() > 2 { + debug!("The --log-level option was incorrectly used more than twice. Ignoring third and further use.") + } + } + } if ap.debug > 0 { - // -d overwrites --log-level - ap.log_level = LogLevel::Debug - } - if ap.log_level.is_none() { - ap.log_level = LogLevel::from_str(&env_org_rust_log, true).unwrap_or(LogLevel::Error); - } - // overwrite environment variable, important because it might have been empty/unset - env::set_var("RUST_LOG", ap.log_level.to_string()); - - // set log level e.g. via RUST_LOG=DEBUG cargo run, use newly set venv var value - // Send *all* output from Debug to Error to stderr - tracing_subscriber::fmt() - .with_writer(io::stderr) - .with_max_level(Level::from_str(&ap.log_level.to_string()).unwrap_or(Level::ERROR)) - .init(); - debug!("Original RUST_LOG env var is {}", env_org_rust_log); + info!("The --debug option overwrote the --log-level option.") + } + if ap.debug > 2 { + debug!("The --debug option was incorrectly used more than twice. Ignoring third and further use.") + } + debug!("Original RUST_LOG env var is '{}'", env_org_rust_log); debug!( - "Final RUST_LOG env var is {}", + "Final RUST_LOG env var is '{}'", env::var("RUST_LOG").unwrap_or_default().to_uppercase() ); - debug!("Final log_level option is {:?}", ap.log_level); + debug!("Final log-level option is {:?}", ap.log_level); if enabled!(Level::TRACE) { - debug!("Log level is set to TRACE."); + debug!( + "Log level of module {} is set to TRACE.", + get_prog_without_ext() + ); } else if enabled!(Level::DEBUG) { - debug!("Log level is set to DEBUG."); + debug!( + "Log level of module {} is set to DEBUG.", + get_prog_without_ext() + ); } debug!("Version is {}", get_version()); debug!("Package name is {}", get_pkg_name());