diff --git a/Cargo.lock b/Cargo.lock index fb33c50..1f44ae4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -35,6 +35,15 @@ dependencies = [ "libc", ] +[[package]] +name = "ansi_term" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2" +dependencies = [ + "winapi 0.3.9", +] + [[package]] name = "atty" version = "0.2.14" @@ -329,19 +338,6 @@ dependencies = [ "cfg-if 1.0.0", ] -[[package]] -name = "env_logger" -version = "0.9.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b2cf0344971ee6c64c31be0d530793fba457d322dfec2810c453d0ef228f9c3" -dependencies = [ - "atty", - "humantime", - "log", - "regex", - "termcolor", -] - [[package]] name = "failure" version = "0.1.8" @@ -612,12 +608,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421" -[[package]] -name = "humantime" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" - [[package]] name = "hydrus-api" version = "0.9.3" @@ -640,14 +630,14 @@ name = "hydrus-utils" version = "0.3.0" dependencies = [ "clap", - "env_logger", "hydrus-api", - "log", "pixiv-rs", "rustnao", "tempdir", "thiserror", "tokio 1.20.1", + "tracing", + "tracing-subscriber", ] [[package]] @@ -860,6 +850,15 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "matchers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" +dependencies = [ + "regex-automata", +] + [[package]] name = "matches" version = "0.1.9" @@ -1101,7 +1100,7 @@ dependencies = [ "libc", "redox_syscall 0.1.57", "rustc_version", - "smallvec", + "smallvec 0.6.14", "winapi 0.3.9", ] @@ -1352,6 +1351,15 @@ dependencies = [ "regex-syntax", ] +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax", +] + [[package]] name = "regex-syntax" version = "0.6.27" @@ -1581,6 +1589,15 @@ dependencies = [ "serde", ] +[[package]] +name = "sharded-slab" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31" +dependencies = [ + "lazy_static", +] + [[package]] name = "slab" version = "0.4.7" @@ -1599,6 +1616,12 @@ dependencies = [ "maybe-uninit", ] +[[package]] +name = "smallvec" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2fd0db749597d91ff862fd1d55ea87f7855a744a8425a64695b6fca237d1dad1" + [[package]] name = "socket2" version = "0.4.4" @@ -1706,6 +1729,15 @@ dependencies = [ "syn", ] +[[package]] +name = "thread_local" +version = "1.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5516c27b78311c50bf42c071425c560ac799b11c30b31f87e3081965fe5e0180" +dependencies = [ + "once_cell", +] + [[package]] name = "time" version = "0.1.44" @@ -1954,6 +1986,36 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5aeea4303076558a00714b823f9ad67d58a3bbda1df83d8827d21193156e22f7" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922" +dependencies = [ + "lazy_static", + "log", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60db860322da191b40952ad9affe65ea23e7dd6a5c442c2c42865810c6ab8e6b" +dependencies = [ + "ansi_term", + "matchers", + "once_cell", + "regex", + "sharded-slab", + "smallvec 1.9.0", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", ] [[package]] @@ -2039,6 +2101,12 @@ dependencies = [ "rand 0.6.5", ] +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "vcpkg" version = "0.2.15" diff --git a/Cargo.toml b/Cargo.toml index 3fa0d68..874e56e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,9 +15,9 @@ pixiv-rs = "0.1.0" hydrus-api = "0.9.3" rustnao = "0.2.1" tempdir = "0.3.7" -env_logger = "0.9.0" -log = "0.4.17" thiserror = "1.0.32" +tracing-subscriber = { version = "0.3.15", features = ["env-filter"] } +tracing = "0.1.36" clap = { version = "3.2.17", features = ["derive", "env"] } [dependencies.tokio] diff --git a/src/main.rs b/src/main.rs index 81c9470..7063434 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,14 +12,17 @@ use hydrus_api::wrapper::tag::Tag; use hydrus_api::{Client, Hydrus}; use pixiv_rs::PixivClient; use rustnao::{Handler, HandlerBuilder, Sauce}; +use std::str::FromStr; use tempdir::TempDir; use tokio::time::{Duration, Instant}; +use tracing_subscriber::fmt::format::FmtSpan; +use tracing_subscriber::EnvFilter; #[tokio::main(flavor = "current_thread")] async fn main() { - env_logger::builder().init(); + init_logger(); let args: Args = Args::parse(); - log::debug!("args: {args:?}"); + tracing::debug!("args: {args:?}"); let opt = match &args.subcommand { Command::FindAndSendUrl(opt) => opt.clone(), Command::FindAndSendTags(opt) => opt.clone(), @@ -38,7 +41,7 @@ async fn main() { let service = ServiceName(opt.tag_service); let files = hydrus.search().add_tags(tags).run().await.unwrap(); - log::info!("Found {} files", files.len()); + tracing::info!("Found {} files", files.len()); let tmpdir = TempDir::new("hydrus-files").unwrap(); let sleep_duration = Duration::from_secs(6); @@ -69,6 +72,20 @@ async fn main() { } } +fn init_logger() { + const DEFAULT_ENV_FILTER: &str = "info"; + let filter_string = + std::env::var("RUST_LOG").unwrap_or_else(|_| DEFAULT_ENV_FILTER.to_string()); + let env_filter = + EnvFilter::from_str(&*filter_string).expect("failed to parse env filter string"); + tracing_subscriber::fmt::SubscriberBuilder::default() + .with_env_filter(env_filter) + .with_writer(std::io::stdout) + .with_span_events(FmtSpan::NEW | FmtSpan::CLOSE) + .compact() + .init(); +} + async fn tag_file( finish_tag: Option<&String>, handler: &Handler, @@ -79,7 +96,7 @@ async fn tag_file( ) -> Result<()> { if let Err(e) = search_and_assign_tags(&handler, &pixiv, &service, &tmpdir, &mut file).await { let hash = file.hash().await.unwrap(); - log::error!("Failed to search tags to file {}: {:?}", hash, e); + tracing::error!("Failed to search tags to file {}: {:?}", hash, e); } else if let Some(finish_tag) = finish_tag { file.add_tags(service.clone().into(), vec![finish_tag.into()]) .await @@ -111,7 +128,7 @@ async fn search_and_assign_tags( tmpdir: &TempDir, mut file: &mut HydrusFile, ) -> Result<()> { - log::debug!("Getting tags for hydrus file {:?}", file.id); + tracing::debug!("Getting tags for hydrus file {:?}", file.id); let sauces = get_sauces_for_file(&handler, tmpdir, file).await?; assign_pixiv_tags_and_url(&pixiv, service, &mut file, &sauces).await @@ -122,12 +139,12 @@ async fn get_sauces_for_file( tmpdir: &TempDir, mut file: &mut HydrusFile, ) -> Result> { - log::debug!("Creating tmp file for hydrus file {:?}", file.id); + tracing::debug!("Creating tmp file for hydrus file {:?}", file.id); let path = search::create_tmp_sauce_file(&tmpdir, &mut file).await?; - log::debug!("Getting sauce for hydrus file {:?}", file.id); + tracing::debug!("Getting sauce for hydrus file {:?}", file.id); let sauce = handler.get_sauce(path.to_str().unwrap(), None, None)?; - log::debug!("Getting tags for hydrus file {:?}", file.id); + tracing::debug!("Getting tags for hydrus file {:?}", file.id); Ok(sauce) } @@ -142,14 +159,14 @@ async fn assign_pixiv_tags_and_url( let tags = search::get_tags_for_sauce(&pixiv, url).await?; if tags.len() > 0 { - log::info!("Found {} tags for file {:?}", tags.len(), hash); + tracing::info!("Found {} tags for file {:?}", tags.len(), hash); file.add_tags(service.clone().into(), tags).await?; } else { - log::info!("No tags for file {:?} found", hash); + tracing::info!("No tags for file {:?} found", hash); } file.associate_urls(vec![url.to_string()]).await?; } else { - log::info!("No pixiv post for file {:?} found", hash); + tracing::info!("No pixiv post for file {:?} found", hash); } Ok(()) diff --git a/src/search.rs b/src/search.rs index 65f086a..28361b4 100644 --- a/src/search.rs +++ b/src/search.rs @@ -14,7 +14,7 @@ pub async fn get_tags_for_sauce(pixiv: &PixivClient, url: &String) -> crate::Res let mut tags = Vec::new(); if let Some(pixiv_id) = url.rsplit_once("=").map(|s| s.1) { - log::trace!("Pixiv id is '{}'", pixiv_id); + tracing::trace!("Pixiv id is '{}'", pixiv_id); let illustration = pixiv.illustration(pixiv_id).await?; for tag in illustration.tags.tags {