diff --git a/Cargo.lock b/Cargo.lock index 0453a0b9..7fa93743 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -85,6 +85,12 @@ version = "1.0.75" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" +[[package]] +name = "arc-swap" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bddcadddf5e9015d310179a59bb28c4d4b9920ad0f11e8e14dbadf654890c9a6" + [[package]] name = "async-compression" version = "0.4.3" @@ -395,6 +401,17 @@ dependencies = [ "regex", ] +[[package]] +name = "derivative" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcc3dd5e9e9c0b295d6e1e4d811fb6f157d5ffd784b8d202fc62eac8035a770b" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "derive_more" version = "0.99.17" @@ -408,6 +425,12 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "destructure_traitobject" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c877555693c14d2f84191cfd3ad8582790fc52b5e2274b40b59cf5f5cea25c7" + [[package]] name = "digest" version = "0.10.7" @@ -836,6 +859,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "hyper" version = "0.14.27" @@ -1064,17 +1093,68 @@ dependencies = [ "cc", ] +[[package]] +name = "linked-hash-map" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" + [[package]] name = "linux-raw-sys" version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3852614a3bd9ca9804678ba6be5e3b8ce76dfc902cae004e3e0c44051b6e88db" +[[package]] +name = "lock_api" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "log" version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" +dependencies = [ + "serde", +] + +[[package]] +name = "log-mdc" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a94d21414c1f4a51209ad204c1776a3d0765002c76c6abcb602a6f09f1e881c7" + +[[package]] +name = "log4rs" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d36ca1786d9e79b8193a68d480a0907b612f109537115c6ff655a3a1967533fd" +dependencies = [ + "anyhow", + "arc-swap", + "chrono", + "derivative", + "fnv", + "humantime", + "libc", + "log", + "log-mdc", + "parking_lot", + "serde", + "serde-value", + "serde_json", + "serde_yaml", + "thiserror", + "thread-id", + "typemap-ors", + "winapi", +] [[package]] name = "md5" @@ -1344,6 +1424,38 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "ordered-float" +version = "2.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68f19d67e5a2795c94e73e0bb1cc1a7edeb2e28efd39e2e1c9b7a40c1108b11c" +dependencies = [ + "num-traits", +] + +[[package]] +name = "parking_lot" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3742b2c103b9f06bc9fff0a37ff4912935851bee6d36f3c02bcc755bcfec228f" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets 0.48.5", +] + [[package]] name = "parse-size" version = "1.0.0" @@ -1463,6 +1575,8 @@ dependencies = [ "json", "lazy_static", "link-cplusplus", + "log", + "log4rs", "md5", "modular-bitfield", "multipart", @@ -1717,6 +1831,12 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef703b7cb59335eae2eb93ceb664c0eb7ea6bf567079d843e09420219668e072" +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[package]] name = "security-framework" version = "2.9.2" @@ -1755,6 +1875,16 @@ dependencies = [ "serde_derive", ] +[[package]] +name = "serde-value" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3a1a3341211875ef120e117ea7fd5228530ae7e7036a779fdc9117be6b3282c" +dependencies = [ + "ordered-float", + "serde", +] + [[package]] name = "serde_derive" version = "1.0.188" @@ -1789,6 +1919,18 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_yaml" +version = "0.8.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "578a7433b776b56a35785ed5ce9a7e777ac0598aac5a6dd1b4b18a307c7fc71b" +dependencies = [ + "indexmap 1.9.3", + "ryu", + "serde", + "yaml-rust", +] + [[package]] name = "sha2" version = "0.10.8" @@ -1901,6 +2043,16 @@ dependencies = [ "syn 2.0.37", ] +[[package]] +name = "thread-id" +version = "4.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0ec81c46e9eb50deaa257be2f148adf052d1fb7701cfd55ccfab2525280b70b" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -2039,6 +2191,15 @@ dependencies = [ "unchecked-index", ] +[[package]] +name = "typemap-ors" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a68c24b707f02dd18f1e4ccceb9d49f2058c2fb86384ef9972592904d7a28867" +dependencies = [ + "unsafe-any-ors", +] + [[package]] name = "typenum" version = "1.17.0" @@ -2099,6 +2260,15 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" +[[package]] +name = "unsafe-any-ors" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0a303d30665362d9680d7d91d78b23f5f899504d4f08b3c4cf08d055d87c0ad" +dependencies = [ + "destructure_traitobject", +] + [[package]] name = "url" version = "2.4.1" @@ -2434,3 +2604,12 @@ dependencies = [ "cfg-if", "windows-sys 0.48.0", ] + +[[package]] +name = "yaml-rust" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" +dependencies = [ + "linked-hash-map", +] diff --git a/Cargo.toml b/Cargo.toml index ac15d2af..d055b254 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,8 @@ is-terminal = "0.4" itertools = "0.10" json = "0.12" lazy_static = "1.4" +log = "*" +log4rs = "1" md5 = "0.7" modular-bitfield = "0.11" multipart = { features = ["server"], git = 'https://github.com/lifegpc/multipart', optional = true, default-features = false } diff --git a/src/download.rs b/src/download.rs index bf4bd56b..50572c90 100644 --- a/src/download.rs +++ b/src/download.rs @@ -369,13 +369,11 @@ pub async fn download_artwork_app( ) -> Result<(), PixivDownloaderError> { let data = ac.get_illust_details(id).await?; let helper = get_helper(); - if helper.verbose() { - println!("{:#?}", data); - } + log::debug!("{:#?}", data); match crate::pixivapp::check::CheckUnknown::check_unknown(&data) { Ok(_) => {} Err(e) => { - println!( + log::warn!( "{} {}", gettext("Warning: Post info contains unknown data:"), e @@ -834,13 +832,11 @@ pub async fn download_fanbox_post( .await .try_err(gettext("Failed to get post info."))?; let helper = get_helper(); - if helper.verbose() { - println!("{:#?}", post); - } + log::debug!("{:#?}", post); match post.check_unknown() { Ok(_) => {} Err(e) => { - println!( + log::warn!( "{} {}", gettext("Warning: Post info contains unknown data:"), e @@ -851,7 +847,7 @@ pub async fn download_fanbox_post( .is_restricted() .try_err(gettext("Failed to check the post is restricted or not."))? { - println!("{}", gettext("Warning: This article is restricted.")); + log::warn!("{}", gettext("Warning: This article is restricted.")); // #TODO allow to continue return Ok(()); } @@ -1129,9 +1125,7 @@ pub async fn download_fanbox_creator_info( }; let data = Arc::new(data); let helper = get_helper(); - if helper.verbose() { - println!("{:#?}", data); - } + log::debug!("{:#?}", data); match data.check_unknown() { Ok(_) => {} Err(e) => { diff --git a/src/fanbox_api.rs b/src/fanbox_api.rs index 7ca4e835..451ce2bf 100644 --- a/src/fanbox_api.rs +++ b/src/fanbox_api.rs @@ -34,24 +34,22 @@ macro_rules! handle_data { Some(r) => { let status = r.status(); if status.as_u16() >= 400 { - println!("{} {}", $err, status); + log::error!(target: "fanbox_api", "{} {}", $err, status); return None; } match r.text_with_charset("UTF-8").await { Ok(data) => match json::parse(data.as_str()) { Ok(obj) => { - if get_helper().verbose() { - println!("{}\n{}", $info, obj.pretty(2),); - } + log::debug!(target: "fanbox_api", "{}\n{}", $info, obj.pretty(2),); Some(obj) } Err(e) => { - println!("{} {}", $err, e); + log::error!(target: "fanbox_api", "{} {}", $err, e); None } }, Err(e) => { - println!("{} {}", $err, e); + log::error!(target: "fanbox_api", "{} {}", $err, e); None } } @@ -117,28 +115,27 @@ impl FanboxClientInternal { let status = r.status(); let code = status.as_u16(); if code >= 400 { - println!("{} {}", gettext("Failed to get fanbox main page:"), status); + log::error!(target: "fanbox_api", "{} {}", gettext("Failed to get fanbox main page:"), status); return false; } match r.text_with_charset("UTF-8").await { Ok(data) => { let mut parser = MetaDataParser::new("metadata"); if !parser.parse(data.as_str()) { - println!("{}", gettext("Failed to parse fanbox main page.")); + log::error!(target: "fanbox_api", "{}", gettext("Failed to parse fanbox main page.")); return false; } - if get_helper().verbose() { - println!( - "{}\n{}", - gettext("Fanbox main page's data:"), - parser.value.as_ref().unwrap().pretty(2).as_str() - ); - } + log::debug!( + target: "fanbox_api", + "{}\n{}", + gettext("Fanbox main page's data:"), + parser.value.as_ref().unwrap().pretty(2).as_str() + ); self.data.replace_with2(parser.value); true } Err(e) => { - println!("{} {}", gettext("Failed to get fanbox main page:"), e); + log::error!(target: "fanbox_api", "{} {}", gettext("Failed to get fanbox main page:"), e); false } } @@ -325,7 +322,7 @@ impl FanboxClient { Some(s) => match FanboxItemList::new(&s["body"], Arc::clone(&self.client)) { Ok(item) => Some(item), Err(e) => { - println!("{}", e); + log::error!(target: "fanbox_api", "{}", e); None } }, @@ -340,7 +337,7 @@ impl FanboxClient { Some(s) => match FanboxPlanList::new(&s["body"]) { Ok(item) => Some(item), Err(e) => { - println!("{}", e); + log::error!(target: "fanbox_api", "{}", e); None } }, @@ -356,7 +353,7 @@ impl FanboxClient { Some(s) => match FanboxItemList::new(&s["body"], Arc::clone(&self.client)) { Ok(item) => Some(item), Err(e) => { - println!("{}", e); + log::error!(target: "fanbox_api", "{}", e); None } }, @@ -375,7 +372,7 @@ impl FanboxClient { Some(s) => match PaginatedCreatorPosts::new(&s, Arc::clone(&self.client)) { Ok(re) => Some(re), Err(e) => { - println!("{}", e); + log::error!(target: "fanbox_api", "{}", e); None } }, diff --git a/src/log_cfg.rs b/src/log_cfg.rs new file mode 100644 index 00000000..0028e65c --- /dev/null +++ b/src/log_cfg.rs @@ -0,0 +1,33 @@ +use crate::ext::rw_lock::GetRwLock; +use lazy_static::lazy_static; +use log::LevelFilter; +use log4rs::append::console::ConsoleAppender; +use log4rs::config::{Appender, Root}; +use log4rs::encode::pattern::PatternEncoder; +use log4rs::{init_config, Config, Handle}; +use std::sync::RwLock; + +lazy_static! { + static ref HANDLE: RwLock> = RwLock::new(None); +} + +pub fn init_with_level(level: LevelFilter) { + let stdout = ConsoleAppender::builder() + .encoder(Box::new(PatternEncoder::new("{m}{n}"))) + .build(); + let config = Config::builder() + .appender(Appender::builder().build("stdout", Box::new(stdout))) + .build(Root::builder().appender("stdout").build(level)) + .unwrap(); + let mut h = HANDLE.get_mut(); + if let Some(h) = h.as_ref() { + h.set_config(config); + } else { + let handle = init_config(config).unwrap(); + h.replace(handle); + } +} + +pub fn init_default() { + init_with_level(LevelFilter::Warn); +} diff --git a/src/main.rs b/src/main.rs index 36997318..745b8b2f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -34,6 +34,7 @@ mod fanbox; mod fanbox_api; mod i18n; mod list; +mod log_cfg; mod opt; mod opthelper; mod opts; @@ -78,7 +79,7 @@ impl Main { if s.save(conf.as_ref().unwrap()) { 0 } else { - println!( + log::error!( "{} {}", gettext("Failed to save config file:"), conf.as_ref().unwrap() @@ -106,6 +107,7 @@ impl Main { } pub async fn run(&mut self) -> i32 { + log_cfg::init_default(); self.cmd = opts::parse_cmd(); if self.cmd.is_none() { return 1; @@ -123,13 +125,14 @@ impl Main { }; let r = self.settings.as_mut().unwrap().read(&conf, fix_invalid); if !r { - println!("{} {}", gettext("Can not read config file:"), conf.as_str()); + log::error!("{} {}", gettext("Can not read config file:"), conf.as_str()); return 1; } } None => {} } get_helper().update(cmd.clone(), self.settings.as_ref().unwrap().clone()); + get_helper().init_log(); match cmd.cmd { Command::Config => { self.deal_config_cmd(); @@ -146,12 +149,12 @@ impl Main { match server.await { Ok(_) => {} Err(e) => { - println!("{}", e); + log::error!("{}", e); } } } Err(e) => { - println!("{} {}", gettext("Failed to start the server:"), e); + log::error!("{} {}", gettext("Failed to start the server:"), e); return 1; } } diff --git a/src/opthelper.rs b/src/opthelper.rs index b90af3cb..13a6ba1f 100644 --- a/src/opthelper.rs +++ b/src/opthelper.rs @@ -369,8 +369,10 @@ impl OptHelper { self._proxy_chain.get_ref().deref().clone() } - pub fn verbose(&self) -> bool { - self.opt.get_ref().verbose + pub fn init_log(&self) { + if self.opt.get_ref().verbose { + crate::log_cfg::init_with_level(log::LevelFilter::Debug); + } } /// Return retry count @@ -585,7 +587,7 @@ impl Default for OptHelper { fn default() -> Self { let mut l = NonTailList::default(); l += Duration::new(3, 0); - Self { + let s = Self { opt: RwLock::new(CommandOpts::default()), settings: RwLock::new(SettingStore::default()), default_retry_interval: l, @@ -594,7 +596,9 @@ impl Default for OptHelper { _proxy_chain: RwLock::new(ProxyChain::default()), #[cfg(feature = "server")] _cors_entries: RwLock::new(Vec::new()), - } + }; + s.init_log(); + s } } diff --git a/src/opts.rs b/src/opts.rs index c9683931..6de824b3 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -203,7 +203,7 @@ impl CommandOpts { if check_file_exists(&self._config.as_ref().unwrap()) { self._config.clone() } else { - println!( + log::error!( "{}", gettext("Warning: The specified config file not found.") ); @@ -257,7 +257,7 @@ pub fn print_usage(prog: &str, opts: &Options) { ) .as_str(); } - println!("{}", opts.usage(brief.as_str())); + log::error!("{}", opts.usage(brief.as_str())); } /// Error when parsing size @@ -665,7 +665,7 @@ pub fn parse_cmd() -> Option { let result = match opts.parse(&argv[1..]) { Ok(m) => m, Err(err) => { - println!("{}", err.to_string()); + log::error!("{}", err.to_string()); return None; } }; @@ -675,7 +675,7 @@ pub fn parse_cmd() -> Option { } let mut re = CommandOpts::new_with_command(&result.free[0]); if re.is_none() { - println!("{}", gettext("Unknown command.")); + log::error!("{}", gettext("Unknown command.")); print_usage(&argv[0], &opts); return None; } @@ -685,13 +685,13 @@ pub fn parse_cmd() -> Option { for url in result.free.iter().skip(1) { let id = PixivID::parse(url); if id.is_none() { - println!("{} {}", gettext("Failed to parse ID:"), url); + log::error!("{} {}", gettext("Failed to parse ID:"), url); return None; } ids.push(id.unwrap()); } if ids.is_empty() { - println!("{}", gettext("No URL or ID specified.")); + log::error!("{}", gettext("No URL or ID specified.")); print_usage(&argv[0], &opts); return None; } @@ -699,7 +699,7 @@ pub fn parse_cmd() -> Option { } Command::Config => { if result.free.len() < 2 { - println!("{}", gettext("No detailed command specified.")); + log::error!("{}", gettext("No detailed command specified.")); print_usage(&argv[0], &opts); return None; } @@ -712,7 +712,7 @@ pub fn parse_cmd() -> Option { None }; if re.as_ref().unwrap().config_cmd.is_none() { - println!("{}", gettext("Unknown config subcommand.")); + log::error!("{}", gettext("Unknown config subcommand.")); print_usage(&argv[0], &opts); return None; } @@ -724,7 +724,7 @@ pub fn parse_cmd() -> Option { match SocketAddr::from_str(address) { Ok(address) => re.as_mut().unwrap().server = Some(address), Err(e) => { - println!("{} {}", gettext("Failed to parse address:"), e); + log::error!("{} {}", gettext("Failed to parse address:"), e); return None; } } @@ -736,7 +736,7 @@ pub fn parse_cmd() -> Option { urls.push(url.to_owned()); } if urls.is_empty() { - println!("{}", gettext("No URL specified.")); + log::error!("{}", gettext("No URL specified.")); print_usage(&argv[0], &opts); return None; } @@ -775,7 +775,7 @@ pub fn parse_cmd() -> Option { re.as_mut().unwrap().retry = r; } Err(e) => { - println!("{} {}", gettext("Failed to parse retry count:"), e); + log::error!("{} {}", gettext("Failed to parse retry count:"), e); return None; } } @@ -783,7 +783,7 @@ pub fn parse_cmd() -> Option { let s = result.opt_str("retry-interval").unwrap(); let r = parse_retry_interval_from_str(s.as_str()); if r.is_err() { - println!( + log::error!( "{} {}", gettext("Failed to parse retry interval:"), r.unwrap_err() @@ -801,7 +801,7 @@ pub fn parse_cmd() -> Option { let s = result.opt_str("use-progress-bar").unwrap(); let r = UseOrNot::from_str(s.as_str()); if r.is_err() { - println!( + log::error!( "{} {}", gettext("Failed to parse :") .replace("", "use-progress-bar") @@ -815,7 +815,7 @@ pub fn parse_cmd() -> Option { match parse_optional_opt(&result, "download-multiple-files", true, parse_bool) { Ok(b) => re.as_mut().unwrap().download_multiple_files = b, Err(e) => { - println!( + log::error!( "{} {}", gettext("Failed to parse :") .replace("", "download-multiple-files") @@ -830,7 +830,7 @@ pub fn parse_cmd() -> Option { re.as_mut().unwrap().download_retry = r; } Err(e) => { - println!("{} {}", gettext("Failed to parse retry count:"), e); + log::error!("{} {}", gettext("Failed to parse retry count:"), e); return None; } } @@ -838,7 +838,7 @@ pub fn parse_cmd() -> Option { let s = result.opt_str("download-retry-interval").unwrap(); let r = parse_retry_interval_from_str(s.as_str()); if r.is_err() { - println!( + log::error!( "{} {}", gettext("Failed to parse retry interval:"), r.unwrap_err() @@ -852,14 +852,14 @@ pub fn parse_cmd() -> Option { re.as_mut().unwrap().download_part_retry = r; } Err(e) => { - println!("{} {}", gettext("Failed to parse retry count:"), e); + log::error!("{} {}", gettext("Failed to parse retry count:"), e); return None; } } match parse_optional_opt(&result, "multiple-threads-download", true, parse_bool) { Ok(b) => re.as_mut().unwrap().multiple_threads_download = b, Err(e) => { - println!( + log::error!( "{} {}", gettext("Failed to parse :") .replace("", "multiple-threads-download") @@ -874,7 +874,7 @@ pub fn parse_cmd() -> Option { re.as_mut().unwrap().max_threads = r; } Err(e) => { - println!("{} {}", gettext("Failed to parse max threads:"), e); + log::error!("{} {}", gettext("Failed to parse max threads:"), e); return None; } } @@ -883,14 +883,14 @@ pub fn parse_cmd() -> Option { re.as_mut().unwrap().part_size = r; } Err(e) => { - println!("{} {}", gettext("Failed to parse part size:"), e); + log::error!("{} {}", gettext("Failed to parse part size:"), e); return None; } } match parse_optional_opt(&result, "max-download-tasks", 5, parse_nonempty_usize) { Ok(r) => re.as_mut().unwrap().max_download_tasks = r, Err(e) => { - println!( + log::error!( "{} {}", gettext("Failed to parse :").replace("", "max-download-tasks"), e @@ -901,7 +901,7 @@ pub fn parse_cmd() -> Option { match parse_optional_opt(&result, "download-multiple-posts", true, parse_bool) { Ok(b) => re.as_mut().unwrap().download_multiple_posts = b, Err(e) => { - println!( + log::error!( "{} {}", gettext("Failed to parse :") .replace("", "download-multiple-posts") @@ -914,7 +914,7 @@ pub fn parse_cmd() -> Option { match parse_optional_opt(&result, "max-download-post-tasks", 3, parse_nonempty_usize) { Ok(r) => re.as_mut().unwrap().max_download_post_tasks = r, Err(e) => { - println!( + log::error!( "{} {}", gettext("Failed to parse :") .replace("", "max-download-post-tasks") @@ -928,7 +928,7 @@ pub fn parse_cmd() -> Option { match parse_optional_opt(&result, "force-yuv420p", true, parse_bool) { Ok(b) => re.as_mut().unwrap().force_yuv420p = b, Err(e) => { - println!( + log::error!( "{} {}", gettext("Failed to parse :") .replace("", "force-yuv420p") @@ -947,7 +947,7 @@ pub fn parse_cmd() -> Option { ) { Ok(r) => re.as_mut().unwrap().x264_profile = r, Err(e) => { - println!( + log::error!( "{} {}", gettext("Failed to parse :") .replace("", "x264-profile") @@ -964,7 +964,7 @@ pub fn parse_cmd() -> Option { Ok(r) => match r { Some(crf) => { if crf < -1f32 { - println!("{}", gettext("x264-crf can not less than -1.")); + log::error!("{}", gettext("x264-crf can not less than -1.")); return None; } else { re.as_mut().unwrap().x264_crf.replace(crf); @@ -973,7 +973,7 @@ pub fn parse_cmd() -> Option { None => {} }, Err(e) => { - println!( + log::error!( "{} {}", ("Failed to parse :") .replace("", "x264-crf") @@ -988,7 +988,7 @@ pub fn parse_cmd() -> Option { Ok(r) => match r { Some(crf) => { if crf <= 0f32 || crf > 1000f32 { - println!( + log::error!( "{}", gettext("ugoira-max-fps can not less than 0 or greater than 1000.") ); @@ -1000,7 +1000,7 @@ pub fn parse_cmd() -> Option { None => {} }, Err(e) => { - println!( + log::error!( "{} {}", ("Failed to parse :") .replace("", "ugoira-max-fps") @@ -1013,7 +1013,7 @@ pub fn parse_cmd() -> Option { match parse_optional_opt(&result, "fanbox-page-number", true, parse_bool) { Ok(b) => re.as_mut().unwrap().fanbox_page_number = b, Err(e) => { - println!( + log::error!( "{} {}", gettext("Failed to parse :") .replace("", "fanbox-page-number") @@ -1027,7 +1027,7 @@ pub fn parse_cmd() -> Option { match parse_optional_opt(&result, "use-app-api", true, parse_bool) { Ok(b) => re.as_mut().unwrap().use_app_api = b, Err(e) => { - println!( + log::error!( "{} {}", gettext("Failed to parse :") .replace("", "use-app-api") @@ -1040,7 +1040,7 @@ pub fn parse_cmd() -> Option { match parse_optional_opt(&result, "use-web-description", true, parse_bool) { Ok(b) => re.as_mut().unwrap().use_web_description = b, Err(e) => { - println!( + log::error!( "{} {}", gettext("Failed to parse :") .replace("", "use-web-description") @@ -1053,7 +1053,7 @@ pub fn parse_cmd() -> Option { match parse_optional_opt(&result, "add-history", true, parse_bool) { Ok(b) => re.as_mut().unwrap().add_history = b, Err(e) => { - println!( + log::error!( "{} {}", gettext("Failed to parse :") .replace("", "add-history") diff --git a/src/pixiv_app.rs b/src/pixiv_app.rs index fa60ed56..9c975240 100644 --- a/src/pixiv_app.rs +++ b/src/pixiv_app.rs @@ -4,7 +4,6 @@ use crate::error::PixivDownloaderError; use crate::ext::atomic::AtomicQuick; use crate::ext::replace::ReplaceWith2; use crate::ext::rw_lock::GetRwLock; -use crate::opthelper::OptHelper; use crate::pixivapp::error::handle_error; use crate::pixivapp::illust::PixivAppIllust; use crate::pixivapp::illusts::PixivAppIllusts; @@ -40,15 +39,11 @@ impl ToString for PixivRestrictType { pub struct PixivAppMiddleware { internal: Arc, - helper: Arc, } impl PixivAppMiddleware { pub fn new(internal: Arc) -> Self { - Self { - internal, - helper: get_helper(), - } + Self { internal } } } @@ -62,10 +57,8 @@ impl ReqMiddleware for PixivAppMiddleware { now, "28c1fdd170a5204386cb1313c7077b34f83e4aaf4aa829ce78c231e05b0bae2c" )) ); - if self.helper.verbose() { - println!("X-Client-Hash: {}", hash); - println!("X-Client-Time: {}", now); - } + log::debug!(target: "pixiv_app", "X-Client-Hash: {}", hash); + log::debug!(target: "pixiv_app", "X-Client-Time: {}", now); let is_app_api = r.url().host_str().unwrap_or("") == "app-api.pixiv.net"; let mut r = RequestBuilder::from_parts(c, r) .header("X-Client-Hash", hash) @@ -274,13 +267,11 @@ impl PixivAppClientInternal { .await .ok_or(gettext("Failed to get follow."))?; let obj = handle_error(re).await?; - if get_helper().verbose() { - println!( - "{}{}", - gettext("Follower's new illusts: "), - obj.pretty(2).as_str() - ); - } + log::debug!( + "{}{}", + gettext("Follower's new illusts: "), + obj.pretty(2).as_str() + ); Ok(obj) } @@ -296,9 +287,7 @@ impl PixivAppClientInternal { .await .ok_or(gettext("Failed to get illust details."))?; let obj = handle_error(re).await?; - if get_helper().verbose() { - println!("{}{}", gettext("Illust details:"), obj.pretty(2).as_str()); - } + log::debug!("{}{}", gettext("Illust details:"), obj.pretty(2).as_str()); Ok(obj) } @@ -333,9 +322,7 @@ impl PixivAppClientInternal { self.auto_handle().await?; let re = self.client.get(url, None).await.ok_or(err)?; let obj = handle_error(re).await?; - if get_helper().verbose() { - println!("{}{}", info, obj.pretty(2).as_str()); - } + log::debug!("{}{}", info, obj.pretty(2).as_str()); Ok(obj) } } diff --git a/src/pixiv_web.rs b/src/pixiv_web.rs index e9d2defc..b0436d41 100644 --- a/src/pixiv_web.rs +++ b/src/pixiv_web.rs @@ -85,12 +85,13 @@ impl PixivWebClient { let status = r.status(); let code = status.as_u16(); if code >= 400 { - println!("{} {}", gettext("Failed to get main page:"), status); + log::error!(target: "pixiv_web","{} {}", gettext("Failed to get main page:"), status); return false; } let data = r.text_with_charset("UTF-8").await; if data.is_err() { - println!( + log::error!( + target: "pixiv_web", "{} {}", gettext("Failed to get main page:"), data.unwrap_err() @@ -100,16 +101,15 @@ impl PixivWebClient { let data = data.unwrap(); let mut p = MetaDataParser::default(); if !p.parse(data.as_str()) { - println!("{}", gettext("Failed to parse main page.")); + log::error!(target: "pixiv_web","{}", gettext("Failed to parse main page.")); return false; } - if get_helper().verbose() { - println!( - "{}\n{}", - gettext("Main page's data:"), - p.value.as_ref().unwrap().pretty(2).as_str() - ); - } + log::debug!( + target: "pixiv_web", + "{}\n{}", + gettext("Main page's data:"), + p.value.as_ref().unwrap().pretty(2).as_str() + ); self.data.get_mut().replace(p.value.unwrap()); true } @@ -121,18 +121,18 @@ impl PixivWebClient { let data = r.text_with_charset("UTF-8").await; if data.is_err() { if is_status_err { - println!("HTTP ERROR {}", status); + log::error!(target: "pixiv_web", "HTTP ERROR {}", status); } - println!("{} {}", gettext("Network error:"), data.unwrap_err()); + log::error!(target: "pixiv_web","{} {}", gettext("Network error:"), data.unwrap_err()); return None; } let data = data.unwrap(); let re = json::parse(data.as_str()); if re.is_err() { if is_status_err { - println!("HTTP ERROR {}", status); + log::error!(target: "pixiv_web","HTTP ERROR {}", status); } else { - println!("{} {}", gettext("Failed to parse JSON:"), re.unwrap_err()); + log::error!(target: "pixiv_web","{} {}", gettext("Failed to parse JSON:"), re.unwrap_err()); } return None; } @@ -140,19 +140,19 @@ impl PixivWebClient { let error = (&value["error"]).as_bool(); if error.is_none() { if is_status_err { - println!("HTTP ERROR {}", status); + log::error!(target: "pixiv_web","HTTP ERROR {}", status); } - println!("{}", gettext("Failed to detect error.")); + log::error!(target: "pixiv_web","{}", gettext("Failed to detect error.")); return None; } let error = error.unwrap(); if error { let message = (&value["message"]).as_str(); if is_status_err { - println!("HTTP ERROR {}", status); + log::error!(target: "pixiv_web","HTTP ERROR {}", status); } if message.is_some() { - println!("{}", message.unwrap()); + log::error!(target: "pixiv_web","{}", message.unwrap()); } return None; } @@ -178,8 +178,9 @@ impl PixivWebClient { } let r = r.unwrap(); let v = self.deal_json(r).await; - if get_helper().verbose() && v.is_some() { - println!( + if v.is_some() { + log::debug!( + target: "pixiv_web", "{} {}", gettext("Artwork's data:"), v.as_ref().unwrap().pretty(2) @@ -205,12 +206,13 @@ impl PixivWebClient { let status = r.status(); let code = status.as_u16(); if code >= 400 { - println!("{} {}", gettext("Failed to get artwork page:"), status); + log::error!(target: "pixiv_web","{} {}", gettext("Failed to get artwork page:"), status); return None; } let data = r.text_with_charset("UTF-8").await; if data.is_err() { - println!( + log::error!( + target: "pixiv_web", "{} {}", gettext("Failed to get artwork page:"), data.unwrap_err() @@ -220,16 +222,15 @@ impl PixivWebClient { let data = data.unwrap(); let mut p = MetaDataParser::new("preload-data"); if !p.parse(data.as_str()) { - println!("{}", gettext("Failed to parse artwork page.")); + log::error!(target: "pixiv_web","{}", gettext("Failed to parse artwork page.")); return None; } - if get_helper().verbose() { - println!( - "{} {}", - gettext("Artwork's data:"), - p.value.as_ref().unwrap().pretty(2) - ); - } + log::debug!( + target: "pixiv_web", + "{} {}", + gettext("Artwork's data:"), + p.value.as_ref().unwrap().pretty(2) + ); Some(p.value.unwrap()) } @@ -248,8 +249,9 @@ impl PixivWebClient { } let r = r.unwrap(); let v = self.deal_json(r).await; - if get_helper().verbose() && v.is_some() { - println!( + if v.is_some() { + log::debug!( + target: "pixiv_web", "{} {}", gettext("Artwork's page data:"), v.as_ref().unwrap().pretty(2) @@ -277,8 +279,9 @@ impl PixivWebClient { } let r = r.unwrap(); let v = self.deal_json(r).await; - if get_helper().verbose() && v.is_some() { - println!( + if v.is_some() { + log::debug!( + target: "pixiv_web", "{} {}", gettext("Ugoira's data:"), v.as_ref().unwrap().pretty(2) diff --git a/src/webclient.rs b/src/webclient.rs index f696811e..c1200a48 100644 --- a/src/webclient.rs +++ b/src/webclient.rs @@ -13,7 +13,6 @@ use reqwest::{Client, ClientBuilder, IntoUrl, Request, Response}; use serde::ser::Serialize; use std::collections::HashMap; use std::default::Default; -use std::sync::atomic::AtomicBool; use std::sync::atomic::AtomicI64; use std::sync::Arc; use std::sync::RwLock; @@ -96,8 +95,6 @@ pub struct WebClient { headers: RwLock>, /// Cookies cookies: RwLock, - /// Verbose logging - verbose: Arc, /// Retry times, 0 means disable, < 0 means always retry retry: Arc, /// Retry interval @@ -115,7 +112,6 @@ impl WebClient { client, headers: RwLock::new(HashMap::new()), cookies: RwLock::new(ManagedCookieJar::new()), - verbose: Arc::new(AtomicBool::new(false)), retry: Arc::new(AtomicI64::new(3)), retry_interval: RwLock::new(None), req_middlewares: RwLock::new(Vec::new()), @@ -165,10 +161,6 @@ impl WebClient { self.retry_interval.get_ref() } - pub fn get_verbose(&self) -> bool { - self.verbose.qload() - } - /// Used to handle Set-Cookie header in an [Response] /// * `r` - reference to an [Response] pub fn handle_set_cookie(&self, r: &Response) { @@ -226,10 +218,6 @@ impl WebClient { self.retry.qstore(retry) } - pub fn set_verbose(&self, verbose: bool) { - self.verbose.qstore(verbose) - } - /// Send GET requests with parameters /// * `param` - GET parameters. Should be a JSON object/array. If value in map is not a string, will dump it /// # Examples @@ -357,9 +345,7 @@ impl WebClient { ); let r = print_error!(gettext("Error when request:"), self.client.execute(r).await); self.handle_set_cookie(&r); - if self.get_verbose() { - println!("{}", r.status()); - } + log::debug!(target: "webclient", "{}", r.status()); Some(r) } @@ -370,9 +356,7 @@ impl WebClient { headers: H, ) -> Result { let s = url.as_str(); - if self.get_verbose() { - println!("GET {}", s); - } + log::debug!(target: "webclient", "GET {}", s); let mut r = self.client.get(s); for (k, v) in self.get_headers().iter() { r = r.header(k, v); @@ -442,9 +426,7 @@ impl WebClient { ); let r = print_error!(gettext("Error when request:"), self.client.execute(r).await); self.handle_set_cookie(&r); - if self.get_verbose() { - println!("{}", r.status()); - } + log::debug!(target: "webclient","{}", r.status()); Some(r) } @@ -456,9 +438,7 @@ impl WebClient { form: Option, ) -> Result { let s = url.as_str(); - if self.get_verbose() { - println!("POST {}", s); - } + log::debug!(target: "webclient", "POST {}", s); let mut r = self.client.post(s); for (k, v) in self.get_headers().iter() { r = r.header(k, v); @@ -494,7 +474,6 @@ impl Default for WebClient { } let c = c.build().unwrap(); let c = Self::new(c); - c.set_verbose(opt.verbose()); match opt.retry() { Some(retry) => c.set_retry(retry), None => {}