Skip to content

Commit

Permalink
fix: if both tracing and logging is enabled, funnel logs through tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron committed Apr 29, 2024
1 parent c258213 commit f746a54
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ required-features = ["log", "unstable"]
default = ["log"]
# TODO: use "dep:{tracing-subscriber,evn_logger}" once our MSRV is 1.60 or higher.
trace = ["tracing-subscriber", "tracing-subscriber/ansi", "test-log-macros/trace"]
log = ["env_logger", "test-log-macros/log"]
log = ["env_logger", "test-log-macros/log", "tracing-subscriber?/tracing-log"]
# Enable unstable features. These are generally exempt from any semantic
# versioning guarantees.
unstable = ["test-log-macros/unstable"]
Expand Down
17 changes: 7 additions & 10 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use syn::ItemFn;
use syn::Lit;
use syn::Meta;


#[proc_macro_attribute]
pub fn test(attr: TokenStream, item: TokenStream) -> TokenStream {
let item = parse_macro_input!(item as ItemFn);
Expand Down Expand Up @@ -90,7 +89,6 @@ fn try_test(attr: TokenStream, input: ItemFn) -> syn::Result<Tokens> {
Ok(result)
}


#[derive(Debug, Default)]
struct AttributeArgs {
default_log_filter: Option<String>,
Expand All @@ -99,7 +97,7 @@ struct AttributeArgs {
impl AttributeArgs {
fn try_parse_attr_single(&mut self, attr: &Attribute) -> syn::Result<bool> {
if !attr.path().is_ident("test_log") {
return Ok(false)
return Ok(false);
}

let nested_meta = attr.parse_args_with(Meta::parse)?;
Expand All @@ -109,7 +107,7 @@ impl AttributeArgs {
return Err(syn::Error::new_spanned(
&nested_meta,
"Expected NameValue syntax, e.g. 'default_log_filter = \"debug\"'.",
))
));
};

let ident = if let Some(ident) = name_value.path.get_ident() {
Expand All @@ -118,7 +116,7 @@ impl AttributeArgs {
return Err(syn::Error::new_spanned(
&name_value.path,
"Expected NameValue syntax, e.g. 'default_log_filter = \"debug\"'.",
))
));
};

let arg_ref = if ident == "default_log_filter" {
Expand All @@ -127,7 +125,7 @@ impl AttributeArgs {
return Err(syn::Error::new_spanned(
&name_value.path,
"Unrecognized attribute, see documentation for details.",
))
));
};

if let Expr::Lit(lit) = &name_value.value {
Expand All @@ -142,16 +140,15 @@ impl AttributeArgs {
return Err(syn::Error::new_spanned(
&name_value.value,
"Failed to parse value, expected a string",
))
));
}

Ok(true)
}
}


/// Expand the initialization code for the `log` crate.
#[cfg(feature = "log")]
#[cfg(all(feature = "log", not(feature = "trace")))]
fn expand_logging_init(attribute_args: &AttributeArgs) -> Tokens {
let add_default_log_filter = if let Some(default_log_filter) = &attribute_args.default_log_filter
{
Expand All @@ -172,7 +169,7 @@ fn expand_logging_init(attribute_args: &AttributeArgs) -> Tokens {
}
}

#[cfg(not(feature = "log"))]
#[cfg(not(all(feature = "log", not(feature = "trace"))))]
fn expand_logging_init(_attribute_args: &AttributeArgs) -> Tokens {
quote! {}
}
Expand Down

0 comments on commit f746a54

Please sign in to comment.