From 033a002f0d88ead237cd3aede95cac95c27ea07f Mon Sep 17 00:00:00 2001 From: Vo Van Nghia Date: Tue, 10 Dec 2024 16:56:55 +0100 Subject: [PATCH 1/2] chore: upgrade typed-path to 0.10.0 --- Cargo.lock | 3 ++- nghe-backend/Cargo.toml | 2 +- nghe-backend/src/config/cover_art.rs | 18 +++++++++++++----- nghe-backend/src/config/integration/mod.rs | 14 +++++++++++--- nghe-backend/src/config/server.rs | 13 ++++++++++--- nghe-backend/src/config/transcode.rs | 18 +++++++++++++----- nghe-backend/src/file/audio/information.rs | 4 ++-- nghe-backend/src/file/mod.rs | 12 ++++++------ nghe-backend/src/file/picture/mod.rs | 8 ++++---- nghe-backend/src/filesystem/local.rs | 12 +++++++----- nghe-backend/src/filesystem/path/serde.rs | 17 ++++++++++------- nghe-backend/src/http/binary/mod.rs | 6 +++--- nghe-backend/src/integration/informant.rs | 4 ++-- nghe-backend/src/test/filesystem/mod.rs | 12 +++++++----- nghe-backend/src/test/mock_impl/mod.rs | 4 ++-- nghe-backend/src/transcode/lock.rs | 12 +++++++----- nghe-backend/src/transcode/sink.rs | 4 ++-- nghe-backend/src/transcode/transcoder.rs | 8 ++++---- 18 files changed, 106 insertions(+), 65 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 269eec2a9..e21c04433 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4316,7 +4316,8 @@ checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" [[package]] name = "typed-path" version = "0.10.0" -source = "git+https://github.com/vnghia/typed-path?rev=bd796e64b3cee53181a3fc2f15245f5bf731bd8c#bd796e64b3cee53181a3fc2f15245f5bf731bd8c" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41713888c5ccfd99979fcd1afd47b71652e331b3d4a0e19d30769e80fec76cce" [[package]] name = "typenum" diff --git a/nghe-backend/Cargo.toml b/nghe-backend/Cargo.toml index 95c751c7f..7b7aec285 100644 --- a/nghe-backend/Cargo.toml +++ b/nghe-backend/Cargo.toml @@ -93,7 +93,7 @@ tower-http = { version = "0.6.2", features = [ ] } tracing-error = { version = "0.2.1" } tracing-subscriber = { version = "0.3.18", features = ["env-filter"] } -typed-path = { git = "https://github.com/vnghia/typed-path", rev = "bd796e64b3cee53181a3fc2f15245f5bf731bd8c" } +typed-path = { version = "0.10.0" } unicode-normalization = { version = "0.1.24" } xxhash-rust = { version = "0.8.12", features = ["xxh3"] } diff --git a/nghe-backend/src/config/cover_art.rs b/nghe-backend/src/config/cover_art.rs index d6e550114..ed8238bb1 100644 --- a/nghe-backend/src/config/cover_art.rs +++ b/nghe-backend/src/config/cover_art.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; use serde_with::formats::SpaceSeparator; use serde_with::{serde_as, StringWithSeparator}; use typed_path::utils::utf8_temp_dir; -use typed_path::Utf8NativePathBuf; +use typed_path::Utf8PlatformPathBuf; #[serde_as] #[derive(Debug, Clone, Serialize, Deserialize, Educe)] @@ -11,9 +11,17 @@ use typed_path::Utf8NativePathBuf; pub struct CoverArt { #[serde(with = "crate::filesystem::path::serde::option")] #[educe(Default( - expression = Some(utf8_temp_dir().unwrap().join("nghe").join("cache").join("cover_art")) + expression = Some( + utf8_temp_dir() + .unwrap() + .join("nghe") + .join("cache") + .join("cover_art") + .with_platform_encoding_checked() + .unwrap() + ) ))] - pub dir: Option, + pub dir: Option, #[serde_as(as = "StringWithSeparator::")] #[educe(Default(expression = vec!["cover.jpg".to_owned(), "cover.png".to_owned()]))] pub names: Vec, @@ -23,13 +31,13 @@ pub struct CoverArt { #[coverage(off)] mod test { use strum::IntoEnumIterator; - use typed_path::Utf8NativePath; + use typed_path::Utf8PlatformPath; use super::*; use crate::file::picture; impl CoverArt { - pub fn with_prefix(self, prefix: impl AsRef) -> Self { + pub fn with_prefix(self, prefix: impl AsRef) -> Self { Self { dir: self.dir.map(|_| prefix.as_ref().join("cache").join("cover_art")), names: picture::Format::iter().map(picture::Format::name).collect(), diff --git a/nghe-backend/src/config/integration/mod.rs b/nghe-backend/src/config/integration/mod.rs index 482a96a91..5ffca592f 100644 --- a/nghe-backend/src/config/integration/mod.rs +++ b/nghe-backend/src/config/integration/mod.rs @@ -2,7 +2,7 @@ use educe::Educe; use serde::{Deserialize, Serialize}; use serde_with::serde_as; use typed_path::utils::utf8_temp_dir; -use typed_path::Utf8NativePathBuf; +use typed_path::Utf8PlatformPathBuf; #[serde_as] #[derive(Clone, Serialize, Deserialize, Educe)] @@ -14,9 +14,17 @@ pub struct Spotify { pub secret: Option, #[serde(with = "crate::filesystem::path::serde::option")] #[educe(Default( - expression = Some(utf8_temp_dir().unwrap().join("nghe").join("spotify").join("token.json")) + expression = Some( + utf8_temp_dir() + .unwrap() + .join("nghe") + .join("spotify") + .join("token.json") + .with_platform_encoding_checked() + .unwrap() + ) ))] - pub token_path: Option, + pub token_path: Option, } #[derive(Debug, Clone, Default, Serialize, Deserialize)] diff --git a/nghe-backend/src/config/server.rs b/nghe-backend/src/config/server.rs index 19e144a7a..c9938b7d5 100644 --- a/nghe-backend/src/config/server.rs +++ b/nghe-backend/src/config/server.rs @@ -3,7 +3,7 @@ use std::net::{IpAddr, SocketAddr}; use educe::Educe; use serde::{Deserialize, Serialize}; use typed_path::utils::utf8_current_dir; -use typed_path::Utf8NativePathBuf; +use typed_path::Utf8PlatformPathBuf; #[derive(Debug, Serialize, Deserialize, Educe)] #[educe(Default)] @@ -13,8 +13,15 @@ pub struct Server { #[educe(Default(expression = 3000))] pub port: u16, #[serde(with = "crate::filesystem::path::serde")] - #[educe(Default(expression = utf8_current_dir().unwrap().join("frontend").join("dist")))] - pub frontend_dir: Utf8NativePathBuf, + #[educe(Default(expression = + utf8_current_dir() + .unwrap() + .join("frontend") + .join("dist") + .with_platform_encoding_checked() + .unwrap() + ))] + pub frontend_dir: Utf8PlatformPathBuf, } impl Server { diff --git a/nghe-backend/src/config/transcode.rs b/nghe-backend/src/config/transcode.rs index cf68f2767..9b5807508 100644 --- a/nghe-backend/src/config/transcode.rs +++ b/nghe-backend/src/config/transcode.rs @@ -2,7 +2,7 @@ use educe::Educe; use serde::{Deserialize, Serialize}; use serde_with::serde_as; use typed_path::utils::utf8_temp_dir; -use typed_path::Utf8NativePathBuf; +use typed_path::Utf8PlatformPathBuf; #[serde_as] #[derive(Debug, Clone, Serialize, Deserialize, Educe)] @@ -15,20 +15,28 @@ pub struct Transcode { pub channel_size: Option, #[serde(with = "crate::filesystem::path::serde::option")] #[educe(Default( - expression = Some(utf8_temp_dir().unwrap().join("nghe").join("cache").join("transcode")) + expression = Some( + utf8_temp_dir() + .unwrap() + .join("nghe") + .join("cache") + .join("transcode") + .with_platform_encoding_checked() + .unwrap() + ) ))] - pub cache_dir: Option, + pub cache_dir: Option, } #[cfg(test)] #[coverage(off)] mod test { - use typed_path::Utf8NativePath; + use typed_path::Utf8PlatformPath; use super::*; impl Transcode { - pub fn with_prefix(self, prefix: impl AsRef) -> Self { + pub fn with_prefix(self, prefix: impl AsRef) -> Self { Self { cache_dir: self.cache_dir.map(|_| prefix.as_ref().join("cache").join("transcode")), ..self diff --git a/nghe-backend/src/file/audio/information.rs b/nghe-backend/src/file/audio/information.rs index c99acef97..6aabbe9e3 100644 --- a/nghe-backend/src/file/audio/information.rs +++ b/nghe-backend/src/file/audio/information.rs @@ -3,7 +3,7 @@ use std::borrow::Cow; use diesel::ExpressionMethods; use diesel_async::RunQueryDsl; use o2o::o2o; -use typed_path::Utf8NativePath; +use typed_path::Utf8PlatformPath; use uuid::Uuid; use super::{Album, Artists, Genres}; @@ -51,7 +51,7 @@ impl Information<'_> { pub async fn upsert_cover_art( &self, database: &Database, - dir: Option<&impl AsRef>, + dir: Option<&impl AsRef>, ) -> Result, Error> { Ok( if let Some(ref picture) = self.metadata.picture diff --git a/nghe-backend/src/file/mod.rs b/nghe-backend/src/file/mod.rs index 516fa737c..4388884cd 100644 --- a/nghe-backend/src/file/mod.rs +++ b/nghe-backend/src/file/mod.rs @@ -6,7 +6,7 @@ use std::time::Duration; use axum_extra::headers::{CacheControl, ETag}; use nghe_api::common::format; -use typed_path::{Utf8NativePath, Utf8NativePathBuf}; +use typed_path::{Utf8PlatformPath, Utf8PlatformPathBuf}; use xxhash_rust::xxh3::xxh3_64; use crate::http::binary::property; @@ -47,7 +47,7 @@ impl Property { Property { hash: self.hash, size: self.size, format } } - fn path_dir(&self, base: impl AsRef) -> Utf8NativePathBuf { + fn path_dir(&self, base: impl AsRef) -> Utf8PlatformPathBuf { let hash = self.hash.to_le_bytes(); // Avoid putting too many files in a single directory @@ -59,9 +59,9 @@ impl Property { pub fn path( &self, - base: impl AsRef, + base: impl AsRef, name: impl Into>, - ) -> Utf8NativePathBuf { + ) -> Utf8PlatformPathBuf { let path = self.path_dir(base); if let Some(name) = name.into() { path.join(name).with_extension(self.format.extension()) @@ -72,9 +72,9 @@ impl Property { pub async fn path_create_dir( &self, - base: impl AsRef, + base: impl AsRef, name: &str, - ) -> Result { + ) -> Result { let path = self.path_dir(base); tokio::fs::create_dir_all(&path).await?; Ok(path.join(name).with_extension(self.format.extension())) diff --git a/nghe-backend/src/file/picture/mod.rs b/nghe-backend/src/file/picture/mod.rs index f3e0dceec..5b4481776 100644 --- a/nghe-backend/src/file/picture/mod.rs +++ b/nghe-backend/src/file/picture/mod.rs @@ -7,7 +7,7 @@ use lofty::picture::{MimeType, Picture as LoftyPicture}; use nghe_api::common::format; use o2o::o2o; use strum::{EnumString, IntoStaticStr}; -use typed_path::{Utf8NativePath, Utf8TypedPath, Utf8TypedPathBuf}; +use typed_path::{Utf8PlatformPath, Utf8TypedPath, Utf8TypedPathBuf}; use uuid::Uuid; use super::Property; @@ -104,7 +104,7 @@ impl<'s, 'd> Picture<'s, 'd> { Ok(Self { source, property, data }) } - pub async fn dump(&self, dir: impl AsRef) -> Result<(), Error> { + pub async fn dump(&self, dir: impl AsRef) -> Result<(), Error> { let path = self.property.path_create_dir(dir, Self::FILENAME).await?; tokio::fs::write(path, &self.data).await?; Ok(()) @@ -113,7 +113,7 @@ impl<'s, 'd> Picture<'s, 'd> { pub async fn upsert( &self, database: &Database, - dir: impl AsRef, + dir: impl AsRef, ) -> Result { // TODO: Checking for its existence before dump. self.dump(dir).await?; @@ -251,7 +251,7 @@ mod test { impl<'s> Picture<'s, '_> { async fn load_cache( - dir: impl AsRef, + dir: impl AsRef, upsert: cover_arts::Upsert<'s>, ) -> Self { let property: file::Property = upsert.property.try_into().unwrap(); diff --git a/nghe-backend/src/filesystem/local.rs b/nghe-backend/src/filesystem/local.rs index 5e3073fa7..01685b3c0 100644 --- a/nghe-backend/src/filesystem/local.rs +++ b/nghe-backend/src/filesystem/local.rs @@ -3,7 +3,7 @@ use std::fs::Metadata; use async_walkdir::WalkDir; use futures_lite::stream::StreamExt; use time::OffsetDateTime; -use typed_path::{TryAsRef as _, Utf8NativePath, Utf8TypedPath}; +use typed_path::Utf8TypedPath; use super::{entry, path}; use crate::file::{self, audio}; @@ -81,10 +81,12 @@ impl super::Trait for Filesystem { source: &binary::Source>, offset: Option, ) -> Result { - let path = source.path.to_path(); - let path: &Utf8NativePath = path - .try_as_ref() - .ok_or_else(|| error::Kind::InvalidTypedPathPlatform(path.to_path_buf()))?; + let path = match source.path.to_path() { + Utf8TypedPath::Unix(path) => path.with_platform_encoding_checked(), + Utf8TypedPath::Windows(path) => path.with_platform_encoding_checked(), + } + .map_err(|_| error::Kind::InvalidTypedPathPlatform(source.path.clone()))?; + binary::Response::from_path_property( path, &source.property, diff --git a/nghe-backend/src/filesystem/path/serde.rs b/nghe-backend/src/filesystem/path/serde.rs index 8dc737f8a..adee23eb0 100644 --- a/nghe-backend/src/filesystem/path/serde.rs +++ b/nghe-backend/src/filesystem/path/serde.rs @@ -1,18 +1,18 @@ use ::serde::{Deserialize, Deserializer, Serializer}; -use typed_path::Utf8NativePathBuf; +use typed_path::Utf8PlatformPathBuf; -pub fn serialize(path: &Utf8NativePathBuf, serializer: S) -> Result +pub fn serialize(path: &Utf8PlatformPathBuf, serializer: S) -> Result where S: Serializer, { serializer.serialize_str(path.as_str()) } -pub fn deserialize<'de, D>(deserializer: D) -> Result +pub fn deserialize<'de, D>(deserializer: D) -> Result where D: Deserializer<'de>, { - ::deserialize(deserializer).map(Utf8NativePathBuf::from) + ::deserialize(deserializer).map(Utf8PlatformPathBuf::from) } pub mod option { @@ -20,7 +20,10 @@ pub mod option { use super::*; - pub fn serialize(path: &Option, serializer: S) -> Result + pub fn serialize( + path: &Option, + serializer: S, + ) -> Result where S: Serializer, { @@ -31,12 +34,12 @@ pub mod option { } } - pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> + pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> where D: Deserializer<'de>, { ::deserialize(deserializer).map(|path| { - let path = Utf8NativePathBuf::from(path); + let path = Utf8PlatformPathBuf::from(path); if path.is_absolute() { Some(path) } else { None } }) } diff --git a/nghe-backend/src/http/binary/mod.rs b/nghe-backend/src/http/binary/mod.rs index 264b253ea..86a5d3894 100644 --- a/nghe-backend/src/http/binary/mod.rs +++ b/nghe-backend/src/http/binary/mod.rs @@ -16,7 +16,7 @@ use nghe_api::common::format; pub use source::Source; use tokio::io::{AsyncRead, AsyncSeekExt, SeekFrom}; use tokio_util::io::ReaderStream; -use typed_path::Utf8NativePath; +use typed_path::Utf8PlatformPath; #[cfg(test)] use crate::test::transcode; @@ -97,7 +97,7 @@ impl Response { } pub async fn from_path( - path: impl AsRef, + path: impl AsRef, format: impl format::Trait, offset: impl Into> + Copy, #[cfg(test)] transcode_status: impl Into>, @@ -116,7 +116,7 @@ impl Response { } pub async fn from_path_property( - path: impl AsRef, + path: impl AsRef, property: &impl property::Trait, offset: impl Into> + Copy, #[cfg(test)] transcode_status: impl Into>, diff --git a/nghe-backend/src/integration/informant.rs b/nghe-backend/src/integration/informant.rs index 88185b5e4..1860b55cb 100644 --- a/nghe-backend/src/integration/informant.rs +++ b/nghe-backend/src/integration/informant.rs @@ -5,7 +5,7 @@ use diesel::{ExpressionMethods, QueryDsl, SelectableHelper}; use diesel_async::RunQueryDsl; use nghe_api::media_annotation::update_artist_information::Request; use rspotify::model::Id; -use typed_path::Utf8NativePath; +use typed_path::Utf8PlatformPath; use uuid::Uuid; use super::spotify; @@ -37,7 +37,7 @@ impl Informant { async fn upsert_artist_picture( &self, database: &Database, - dir: Option<&impl AsRef>, + dir: Option<&impl AsRef>, source: Option>>, ) -> Result, Error> { Ok( diff --git a/nghe-backend/src/test/filesystem/mod.rs b/nghe-backend/src/test/filesystem/mod.rs index e53522c07..02e1abc12 100644 --- a/nghe-backend/src/test/filesystem/mod.rs +++ b/nghe-backend/src/test/filesystem/mod.rs @@ -4,7 +4,7 @@ mod s3; pub use common::{Impl, Trait}; use nghe_api::common::filesystem; -use typed_path::{TryAsRef as _, Utf8NativePath, Utf8NativePathBuf}; +use typed_path::{Utf8PlatformPathBuf, Utf8TypedPath}; use crate::filesystem::Filesystem; @@ -34,9 +34,11 @@ impl Mock { } } - pub fn prefix(&self) -> Utf8NativePathBuf { - let prefix = self.local.prefix(); - let prefix: &Utf8NativePath = prefix.try_as_ref().unwrap(); - prefix.into() + pub fn prefix(&self) -> Utf8PlatformPathBuf { + match self.local.prefix() { + Utf8TypedPath::Unix(path) => path.with_platform_encoding_checked(), + Utf8TypedPath::Windows(path) => path.with_platform_encoding_checked(), + } + .unwrap() } } diff --git a/nghe-backend/src/test/mock_impl/mod.rs b/nghe-backend/src/test/mock_impl/mod.rs index 83ed7a563..d1391fc29 100644 --- a/nghe-backend/src/test/mock_impl/mod.rs +++ b/nghe-backend/src/test/mock_impl/mod.rs @@ -12,7 +12,7 @@ pub use information::Mock as Information; use lofty::config::{ParseOptions, WriteOptions}; use nghe_api::common; use rstest::fixture; -use typed_path::Utf8NativePath; +use typed_path::Utf8PlatformPath; use uuid::Uuid; use super::filesystem::Trait; @@ -50,7 +50,7 @@ pub struct Mock { } impl Config { - fn with_prefix(self, prefix: impl AsRef + Copy) -> Self { + fn with_prefix(self, prefix: impl AsRef + Copy) -> Self { Self { transcode: self.transcode.with_prefix(prefix), cover_art: self.cover_art.with_prefix(prefix), diff --git a/nghe-backend/src/transcode/lock.rs b/nghe-backend/src/transcode/lock.rs index 65c4d403a..a5439cfce 100644 --- a/nghe-backend/src/transcode/lock.rs +++ b/nghe-backend/src/transcode/lock.rs @@ -2,14 +2,14 @@ use std::fmt::Display; use std::io::Seek; use tracing::instrument; -use typed_path::Utf8NativePath; +use typed_path::Utf8PlatformPath; use crate::{error, Error}; pub struct Lock; impl Lock { - fn open_read(path: impl AsRef) -> Result { + fn open_read(path: impl AsRef) -> Result { if cfg!(windows) { // On Windows, the file must be open with write permissions to lock it. std::fs::OpenOptions::new().read(true).write(true).open(path.as_ref()) @@ -20,7 +20,7 @@ impl Lock { } #[instrument(skip_all, fields(%path), err(Debug, level = "trace"))] - pub fn lock_read(path: impl AsRef + Display) -> Result { + pub fn lock_read(path: impl AsRef + Display) -> Result { let mut file = Self::open_read(path)?; // The read lock might be acquired with an empty file since creating and locking exclusively // a file are two separate operations. We need to check if the file is empty before trying @@ -38,7 +38,9 @@ impl Lock { } #[instrument(skip_all, fields(%path), err(Debug, level = "trace"))] - pub fn lock_write(path: impl AsRef + Display) -> Result { + pub fn lock_write( + path: impl AsRef + Display, + ) -> Result { let file = std::fs::OpenOptions::new().write(true).create_new(true).open(path.as_ref())?; if file.try_lock()? { Ok(file) } else { error::Kind::FileAlreadyLocked.into() } } @@ -51,7 +53,7 @@ mod test { impl Lock { pub fn lock_read_blocking( - path: impl AsRef, + path: impl AsRef, ) -> Result { let file = Self::open_read(path)?; file.lock_shared()?; diff --git a/nghe-backend/src/transcode/sink.rs b/nghe-backend/src/transcode/sink.rs index d444ef8da..522c586b7 100644 --- a/nghe-backend/src/transcode/sink.rs +++ b/nghe-backend/src/transcode/sink.rs @@ -8,7 +8,7 @@ use nghe_api::common::format; use rsmpeg::avformat::{AVIOContextContainer, AVIOContextCustom}; use rsmpeg::avutil::AVMem; use rsmpeg::ffi; -use typed_path::Utf8NativePath; +use typed_path::Utf8PlatformPath; use super::Lock; use crate::{config, Error}; @@ -27,7 +27,7 @@ impl Sink { pub async fn new( config: &config::Transcode, format: format::Transcode, - output: Option + Display + Send + 'static>, + output: Option + Display + Send + 'static>, ) -> Result<(Self, Receiver>), Error> { let (tx, rx) = crate::sync::channel(config.channel_size); // It will fail in two cases: diff --git a/nghe-backend/src/transcode/transcoder.rs b/nghe-backend/src/transcode/transcoder.rs index a26141788..db3f6b7e6 100644 --- a/nghe-backend/src/transcode/transcoder.rs +++ b/nghe-backend/src/transcode/transcoder.rs @@ -289,7 +289,7 @@ impl Transcoder { mod test { use futures_lite::{stream, StreamExt}; use nghe_api::common::format; - use typed_path::Utf8NativePathBuf; + use typed_path::Utf8PlatformPathBuf; use super::*; use crate::config; @@ -302,7 +302,7 @@ mod test { bitrate: u32, offset: u32, ) -> Vec { - let (sink, rx) = Sink::new(config, format, None::).await.unwrap(); + let (sink, rx) = Sink::new(config, format, None::).await.unwrap(); let handle = Transcoder::spawn(input, sink, bitrate, offset).unwrap(); let data = rx.into_stream().map(stream::iter).flatten().collect().await; handle.await.unwrap().unwrap(); @@ -316,7 +316,7 @@ mod test { mod tests { use nghe_api::common::format; use rstest::rstest; - use typed_path::Utf8NativePath; + use typed_path::Utf8PlatformPath; use super::*; use crate::config; @@ -336,7 +336,7 @@ mod tests { let data = Transcoder::spawn_collect(input, &config, format, bitrate, offset).await; tokio::fs::write( - Utf8NativePath::new(env!("NGHE_HEARING_TEST_OUTPUT")) + Utf8PlatformPath::new(env!("NGHE_HEARING_TEST_OUTPUT")) .join(concat_string!(bitrate.to_string(), "-", offset.to_string())) .with_extension(format.as_ref()), &data, From 7baef238d5a58c7a41b115806940d02cedc451ff Mon Sep 17 00:00:00 2001 From: Vo Van Nghia Date: Wed, 11 Dec 2024 07:46:31 +0100 Subject: [PATCH 2/2] backend/tracing: add fields to span again for error spantrace --- nghe-backend/src/integration/informant.rs | 1 - nghe-backend/src/integration/spotify.rs | 1 + nghe-backend/src/lib.rs | 2 +- nghe-backend/src/scan/scanner.rs | 8 +++--- nghe-backend/src/transcode/transcoder.rs | 9 +++---- nghe-proc-macro/src/backend/handler.rs | 33 +++++++++-------------- 6 files changed, 23 insertions(+), 31 deletions(-) diff --git a/nghe-backend/src/integration/informant.rs b/nghe-backend/src/integration/informant.rs index 1860b55cb..6aca33dc8 100644 --- a/nghe-backend/src/integration/informant.rs +++ b/nghe-backend/src/integration/informant.rs @@ -89,7 +89,6 @@ impl Informant { None }; - tracing::debug!(?spotify); self.upsert_artist(database, config, id, spotify.as_ref()).await } diff --git a/nghe-backend/src/integration/spotify.rs b/nghe-backend/src/integration/spotify.rs index fe7d1ecd9..b723cf7b6 100644 --- a/nghe-backend/src/integration/spotify.rs +++ b/nghe-backend/src/integration/spotify.rs @@ -40,6 +40,7 @@ impl Client { } } + #[tracing::instrument(skip_all, name = "spotify:search_artist", ret(level = "debug"))] pub async fn search_artist(&self, name: &str) -> Result, Error> { Ok( if let SearchResult::Artists(artists) = diff --git a/nghe-backend/src/lib.rs b/nghe-backend/src/lib.rs index b8bbf8097..56ad2c27d 100644 --- a/nghe-backend/src/lib.rs +++ b/nghe-backend/src/lib.rs @@ -63,7 +63,7 @@ pub fn init_tracing() -> Result<(), Error> { if cfg!(test) { tracing.with(tracing_subscriber::fmt::layer().with_test_writer()).try_init()?; } else { - tracing.with(tracing_subscriber::fmt::layer().with_target(false).compact()).try_init()?; + tracing.with(tracing_subscriber::fmt::layer().with_target(false)).try_init()?; } Ok(()) diff --git a/nghe-backend/src/scan/scanner.rs b/nghe-backend/src/scan/scanner.rs index 37a333492..13443692c 100644 --- a/nghe-backend/src/scan/scanner.rs +++ b/nghe-backend/src/scan/scanner.rs @@ -237,16 +237,16 @@ impl<'db, 'fs, 'mf> Scanner<'db, 'fs, 'mf> { Ok(()) } - #[instrument(skip_all, err(Debug))] + #[instrument(skip_all, fields(started_at), err(Debug))] pub async fn run(&self) -> Result<(), Error> { - tracing::info!(music_folder = ?self.music_folder); + let span = tracing::Span::current(); let started_at = crate::time::now().await; - tracing::info!(?started_at); + span.record("started_at", tracing::field::display(&started_at)); + tracing::info!(music_folder = ?self.music_folder); let (scan_handle, permit, rx) = self.init(); let mut join_set = tokio::task::JoinSet::new(); - let span = tracing::Span::current(); while let Ok(entry) = rx.recv_async().await { let permit = permit.clone().acquire_owned().await?; let scan = self.clone().into_owned(); diff --git a/nghe-backend/src/transcode/transcoder.rs b/nghe-backend/src/transcode/transcoder.rs index db3f6b7e6..b621709cf 100644 --- a/nghe-backend/src/transcode/transcoder.rs +++ b/nghe-backend/src/transcode/transcoder.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; use std::ffi::{CStr, CString}; -use std::fmt::Display; +use std::fmt::Debug; use concat_string::concat_string; use rsmpeg::avcodec::{AVCodec, AVCodecContext}; @@ -219,14 +219,13 @@ impl<'graph> Filter<'graph> { } impl Transcoder { - #[instrument(skip_all, err(Debug))] + #[instrument(err(Debug))] pub fn spawn( - input: impl Into + Display, + input: impl Into + Debug, sink: Sink, bitrate: u32, offset: u32, ) -> Result>, Error> { - tracing::debug!(%input, ?sink, %bitrate, %offset); let mut transcoder = Self::new(&CString::new(input.into())?, sink, bitrate, offset)?; let span = tracing::Span::current(); @@ -296,7 +295,7 @@ mod test { impl Transcoder { pub async fn spawn_collect( - input: impl Into + Display, + input: impl Into + Debug, config: &config::Transcode, format: format::Transcode, bitrate: u32, diff --git a/nghe-proc-macro/src/backend/handler.rs b/nghe-proc-macro/src/backend/handler.rs index 5c401af0e..79db9f643 100644 --- a/nghe-proc-macro/src/backend/handler.rs +++ b/nghe-proc-macro/src/backend/handler.rs @@ -106,14 +106,16 @@ impl Arg { } } - fn to_tracing(&self) -> Option { + fn to_skip_debug(&self) -> Option<&syn::Ident> { match self { - Arg::User(ident) => { - let field = format_ident!("user_{ident}"); - Some(parse_quote!(user.#ident = ?#field)) + Arg::Database { ident, use_database } => { + if *use_database { + Some(ident) + } else { + None + } } - Arg::Header { ident, .. } => Some(parse_quote!(#ident = ?#ident)), - Arg::Request => Some(parse_quote!(request = ?request)), + Arg::Extension { ident, .. } => Some(ident), _ => None, } } @@ -345,24 +347,18 @@ impl Handler { }) .try_collect()?; + let skip_debugs: Punctuated<&syn::Ident, syn::Token![,]> = + self.args.value.iter().filter_map(Arg::to_skip_debug).collect(); let mut tracing_args = Punctuated::::default(); tracing_args.push(parse_quote!(name = #tracing_name)); - tracing_args.push(parse_quote!(skip_all)); + tracing_args.push(parse_quote!(skip(#skip_debugs))); if self.is_result_binary.is_some() { tracing_args.push(parse_quote!(ret(level = "trace"))); tracing_args.push(parse_quote!(err(Debug))); } - let traced_args: Punctuated = - self.args.value.iter().filter_map(Arg::to_tracing).collect(); - let traced_expr_macro: Option = if traced_args.is_empty() { - None - } else { - Some(quote!(tracing::debug!(#traced_args);)) - }; - let handler_ident = &self.item.sig.ident; - let handler_expr_call: syn::Expr = if sig.asyncness.is_some() { + let source: syn::Expr = if sig.asyncness.is_some() { parse_quote!(#handler_ident(#args).await) } else { parse_quote!(#handler_ident(#args)) @@ -372,10 +368,7 @@ impl Handler { #[coverage(off)] #[inline(always)] #[tracing::instrument(#tracing_args)] - #sig { - #traced_expr_macro - #handler_expr_call - } + #sig { #source } }) }