aboutsummaryrefslogtreecommitdiff
path: root/macros/src
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2023-08-13 11:26:37 +0200
committerHampusM <hampus@hampusmat.com>2023-08-13 11:26:37 +0200
commit8f990a4477aa126e6bc79b98ba5f6685b0658fe7 (patch)
tree9ea852d71f3afa201f5633eb789ff488c6df1358 /macros/src
parent89c238f9c82ade2d7656e2bee76838a391609a88 (diff)
feat: add internal logging for macros
Diffstat (limited to 'macros/src')
-rw-r--r--macros/src/caster.rs11
-rw-r--r--macros/src/lib.rs18
-rw-r--r--macros/src/util/mod.rs1
-rw-r--r--macros/src/util/tokens.rs89
4 files changed, 119 insertions, 0 deletions
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,
+}