Skip to content

Commit

Permalink
Refactor: tracing_lib has to be with "::", so that it can be used to …
Browse files Browse the repository at this point in the history
…specify a root crate
  • Loading branch information
drmingdrmer committed Jun 1, 2022
1 parent b3a0d42 commit 9fbfa83
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 9 deletions.
2 changes: 1 addition & 1 deletion async-entry/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "async-entry"
version = "0.3.0"
version = "0.3.1"
edition = "2021"

authors = ["drdr.xp <[email protected]>"]
Expand Down
12 changes: 6 additions & 6 deletions async-entry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ struct Configuration {
init: Option<(String, Span)>,
tracing_span: Option<(String, Span)>,

/// Import `tracing` and `tracing_future` in another crate or mod, e.g. `tracing_lib::tracing`, instead of using `::tracing`.
/// Import `tracing` and `tracing_future` in another crate or mod, e.g. `tracing_lib::tracing`, instead of using `tracing`.
tracing_lib: Option<(String, Span)>,
}

Expand Down Expand Up @@ -312,7 +312,7 @@ fn build_config(args: AttributeArgs, rt_multi_thread: bool) -> Result<FinalConfi
}
"tracing_lib" => {
format!(
"The `{}` attribute requires an argument of level of the span, e.g. \"my_lib\" or \"\".",
"The `{}` attribute requires an argument of level of the span, e.g. \"my_lib::\" or \"::\" or \"\".",
name
)
}
Expand Down Expand Up @@ -414,7 +414,7 @@ type AttributeArgs = syn::punctuated::Punctuated<syn::NestedMeta, syn::Token![,]
/// ### Use other lib to import `tracing` and `tracing_future`
///
/// ```no_run
/// #[async_entry::test(tracing_span = "info" ,tracing_lib="my_lib")]
/// #[async_entry::test(tracing_span = "info" ,tracing_lib="my_lib::")]
/// async fn my_test() {
/// assert!(true);
/// }
Expand Down Expand Up @@ -516,16 +516,16 @@ fn build_test_fn(mut item_fn: ItemFn, config: FinalConfig) -> Result<TokenStream

let body_tracing_span = if let Some(tspan) = config.tracing_span {
let tracing_lib = if let Some(l) = config.tracing_lib {
format!("{}::", l.0)
l.0.clone()
} else {
"".to_string()
};

let level = tspan.0;
let add_tracing_span = format!(
r#"
use {}tracing::Instrument;
let body_span = {}tracing::{}_span!("{}");
use {} tracing::Instrument;
let body_span = {} tracing::{}_span!("{}");
let body = body.instrument(body_span);
"#,
tracing_lib, tracing_lib, level, fn_name
Expand Down
2 changes: 1 addition & 1 deletion async-entry/want
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ pub const empty_trace_lib: test::TestDescAndFn = test::TestDescAndFn {
};
fn empty_trace_lib() {
let body = async {};
use ::tracing::Instrument;
use tracing::Instrument;
let body_span = {
use ::tracing::__macro_support::Callsite as _;
static CALLSITE: ::tracing::__macro_support::MacroCallsite = {
Expand Down
3 changes: 3 additions & 0 deletions test-async-entry/tests/it-external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ async fn empty_return_expr() -> anyhow::Result<()> {
#[async_entry::test(tracing_span = "info")]
async fn empty_trace_span() {}

#[async_entry::test(tracing_span = "info", tracing_lib = "::")]
async fn specify_trace_lib_to_root() {}

fn g() {}

#[async_entry::test(init = "g()")]
Expand Down
2 changes: 1 addition & 1 deletion test-async-entry/tests/it-tracing-lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ use lib_crate::tokio;

/// Specify where to load tracing and tracing_future.
/// Do not need to import
#[async_entry::test(tracing_span = "info", tracing_lib = "lib_crate")]
#[async_entry::test(tracing_span = "info", tracing_lib = "lib_crate::")]
async fn specify_trace_lib() {}
65 changes: 65 additions & 0 deletions test-async-entry/want-external
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,70 @@ fn empty_trace_span() {
#[allow(clippy::expect_used)]
rt.block_on(body);
}
extern crate test;
#[cfg(test)]
#[rustc_test_marker]
pub const specify_trace_lib_to_root: test::TestDescAndFn = test::TestDescAndFn {
desc: test::TestDesc {
name: test::StaticTestName("specify_trace_lib_to_root"),
ignore: false,
ignore_message: ::core::option::Option::None,
compile_fail: false,
no_run: false,
should_panic: test::ShouldPanic::No,
test_type: test::TestType::IntegrationTest,
},
testfn: test::StaticTestFn(|| test::assert_test_result(specify_trace_lib_to_root())),
};
fn specify_trace_lib_to_root() {
let body = async {};
use ::tracing::Instrument;
let body_span = {
use ::tracing::__macro_support::Callsite as _;
static CALLSITE: ::tracing::__macro_support::MacroCallsite = {
use ::tracing::__macro_support::MacroCallsite;
static META: ::tracing::Metadata<'static> = {
::tracing_core::metadata::Metadata::new(
"specify_trace_lib_to_root",
"it_external",
::tracing::Level::INFO,
Some("test-async-entry/tests/it-external.rs"),
Some(25u32),
Some("it_external"),
::tracing_core::field::FieldSet::new(
&[],
::tracing_core::callsite::Identifier(&CALLSITE),
),
::tracing::metadata::Kind::SPAN,
)
};
MacroCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::INFO <= ::tracing::level_filters::STATIC_MAX_LEVEL
&& ::tracing::Level::INFO <= ::tracing::level_filters::LevelFilter::current()
&& {
interest = CALLSITE.interest();
!interest.is_never()
}
&& CALLSITE.is_enabled(interest)
{
let meta = CALLSITE.metadata();
::tracing::Span::new(meta, &{ meta.fields().value_set(&[]) })
} else {
let span = CALLSITE.disabled_span();
{};
span
}
};
let body = body.instrument(body_span);
let rt = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("Failed building the Runtime");
#[allow(clippy::expect_used)]
rt.block_on(body);
}
fn g() {}
extern crate test;
#[cfg(test)]
Expand Down Expand Up @@ -349,6 +413,7 @@ pub fn main() -> () {
&empty_tail_expr,
&empty_return_expr,
&empty_trace_span,
&specify_trace_lib_to_root,
&with_init,
&current_thread,
&multi_thread,
Expand Down

0 comments on commit 9fbfa83

Please sign in to comment.