diff options
author | HampusM <hampus@hampusmat.com> | 2023-08-13 11:26:37 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-08-13 11:26:37 +0200 |
commit | 8f990a4477aa126e6bc79b98ba5f6685b0658fe7 (patch) | |
tree | 9ea852d71f3afa201f5633eb789ff488c6df1358 | |
parent | 89c238f9c82ade2d7656e2bee76838a391609a88 (diff) |
feat: add internal logging for macros
-rw-r--r-- | Cargo.lock | 135 | ||||
-rw-r--r-- | macros/Cargo.toml | 4 | ||||
-rw-r--r-- | macros/src/caster.rs | 11 | ||||
-rw-r--r-- | macros/src/lib.rs | 18 | ||||
-rw-r--r-- | macros/src/util/mod.rs | 1 | ||||
-rw-r--r-- | macros/src/util/tokens.rs | 89 |
6 files changed, 258 insertions, 0 deletions
@@ -215,6 +215,12 @@ dependencies = [ ] [[package]] +name = "log" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4" + +[[package]] name = "memchr" version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -263,6 +269,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61807f77802ff30975e01f4f071c8ba10c022052f98b3294119f3e615d13e5be" [[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + +[[package]] name = "num-traits" version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -297,6 +313,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" [[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + +[[package]] name = "paste" version = "1.0.14" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -432,6 +454,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4" [[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 = "smallvec" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62bb4feee49fdd9f707ef802e22365a35de4b7b299de4763d44bfea899442ff9" + +[[package]] name = "strum" version = "0.24.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -506,6 +543,8 @@ dependencies = [ "syn 1.0.109", "syrette", "thiserror", + "tracing", + "tracing-subscriber", "utility-macros", "uuid", ] @@ -541,6 +580,16 @@ dependencies = [ ] [[package]] +name = "thread_local" +version = "1.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152" +dependencies = [ + "cfg-if", + "once_cell", +] + +[[package]] name = "tokio" version = "1.29.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -565,6 +614,64 @@ dependencies = [ ] [[package]] +name = "tracing" +version = "0.1.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" +dependencies = [ + "cfg-if", + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.28", +] + +[[package]] +name = "tracing-core" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0955b8137a1df6f1a2e9a37d8a6656291ff0297c1a97c24e0d8425fe2312f79a" +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.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77" +dependencies = [ + "nu-ansi-term", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", +] + +[[package]] name = "unicode-ident" version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -589,6 +696,12 @@ dependencies = [ ] [[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + +[[package]] name = "version_check" version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -601,6 +714,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] name = "yansi" version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/macros/Cargo.toml b/macros/Cargo.toml index ccf10cd..4764221 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -30,6 +30,10 @@ once_cell = "1.13.1" thiserror = "1.0.37" proc-macro-error = "1.0.4" +[target.'cfg(syrette_macros_logging)'.dependencies] +tracing = "0.1.37" +tracing-subscriber = "0.3.17" + [dev_dependencies] syrette = { version = "0.4.2", path = ".." } mockall = "0.11.4" diff --git a/macros/src/caster.rs b/macros/src/caster.rs index c36aa1f..417a881 100644 --- a/macros/src/caster.rs +++ b/macros/src/caster.rs @@ -18,16 +18,27 @@ use quote::{format_ident, quote, ToTokens}; use uuid::adapter::Simple; use uuid::Uuid; +#[cfg(syrette_macros_logging)] +use crate::util::tokens::ToTokensExt; + const CASTER_FN_NAME_PREFIX: &[u8] = b"__"; const FN_BUF_LEN: usize = CASTER_FN_NAME_PREFIX.len() + Simple::LENGTH; +#[cfg_attr(syrette_macros_logging, tracing::instrument(skip(ty, dst_trait)))] pub fn generate_caster( ty: &impl ToTokens, dst_trait: &impl ToTokens, sync: bool, ) -> TokenStream { + #[cfg(syrette_macros_logging)] + tracing::debug!( + source = %ty.to_str_pretty(), + destination = %ty.to_str_pretty(), + "Generating caster", + ); + let fn_ident = create_caster_fn_ident(Uuid::new_v4()); let new_caster = if sync { diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 2fc3c21..41001ae 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -482,6 +482,9 @@ pub fn declare_interface(input: TokenStream) -> TokenStream .map_or_else(|| Ok(false), MacroFlag::get_bool) .unwrap_or_abort(); + #[cfg(syrette_macros_logging)] + init_logging(); + let interface_type = if interface == implementation { Type::Path(interface) } else { @@ -545,3 +548,18 @@ pub fn named(_: TokenStream, _: TokenStream) -> TokenStream { TokenStream::new() } + +#[cfg(syrette_macros_logging)] +fn init_logging() +{ + use tracing::Level; + use tracing_subscriber::FmtSubscriber; + + let subscriber = FmtSubscriber::builder() + .with_max_level(Level::DEBUG) + .finish(); + + // The error can be ignored because it doesn't matter if the global default + // has already been set + tracing::subscriber::set_global_default(subscriber).ok(); +} diff --git a/macros/src/util/mod.rs b/macros/src/util/mod.rs index d3edb67..2244229 100644 --- a/macros/src/util/mod.rs +++ b/macros/src/util/mod.rs @@ -3,6 +3,7 @@ pub mod item_impl; pub mod iterator_ext; pub mod string; pub mod syn_path; +pub mod tokens; macro_rules! to_option { ($($tokens: tt)+) => { diff --git a/macros/src/util/tokens.rs b/macros/src/util/tokens.rs new file mode 100644 index 0000000..c614ea0 --- /dev/null +++ b/macros/src/util/tokens.rs @@ -0,0 +1,89 @@ +use std::fmt::Write; + +use proc_macro2::{Delimiter, Spacing, TokenTree}; +use quote::ToTokens; + +pub trait ToTokensExt +{ + fn to_str_pretty(&self) -> String; +} + +impl<T: ToTokens> ToTokensExt for T +{ + fn to_str_pretty(&self) -> String + { + let mut spaceable = Spaceable::None; + + self.to_token_stream() + .into_iter() + .fold(String::new(), |mut acc, token_tree| { + let prev_spaceable = spaceable; + + spaceable = get_tt_spaceable(&token_tree); + + if matches!(prev_spaceable, Spaceable::Left | Spaceable::LeftRight) + && matches!(spaceable, Spaceable::Right | Spaceable::LeftRight) + { + write!(acc, " ").ok(); + } + + match token_tree { + TokenTree::Group(group) => match group.delimiter() { + Delimiter::Parenthesis => { + write!(acc, "({})", group.stream().to_str_pretty()).ok(); + } + Delimiter::Brace => { + write!(acc, "{{{}}}", group.stream().to_str_pretty()).ok(); + } + Delimiter::Bracket => { + write!(acc, "[{}]", group.stream().to_str_pretty()).ok(); + } + Delimiter::None => { + write!(acc, "{}", group.stream().to_str_pretty()).ok(); + } + }, + tt => { + write!(acc, "{tt}").ok(); + } + } + + acc + }) + } +} + +fn get_tt_spaceable(token_tree: &TokenTree) -> Spaceable +{ + match &token_tree { + TokenTree::Ident(_) => Spaceable::LeftRight, + TokenTree::Punct(punct) + if punct.spacing() == Spacing::Alone && (punct.as_char() == '+') => + { + Spaceable::LeftRight + } + TokenTree::Punct(punct) + if punct.spacing() == Spacing::Alone + && (punct.as_char() == '>' || punct.as_char() == ',') => + { + Spaceable::Left + } + TokenTree::Punct(punct) + if punct.spacing() == Spacing::Joint && punct.as_char() == '-' => + { + // Is part of -> + Spaceable::Right + } + TokenTree::Punct(punct) if punct.as_char() == '&' => Spaceable::Right, + TokenTree::Group(_) => Spaceable::Left, + _ => Spaceable::None, + } +} + +#[derive(Debug, Clone, Copy)] +enum Spaceable +{ + Left, + Right, + LeftRight, + None, +} |