Skip to content

Commit 5b046e4

Browse files
authored
make macros hygienic (#100)
1 parent 6ba43ca commit 5b046e4

File tree

4 files changed

+40
-20
lines changed

4 files changed

+40
-20
lines changed

src/bridges/log.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,15 @@ mod tests {
102102
log::info!(logger: lf_logger, "root event");
103103
log::info!(logger: lf_logger, target: "custom_target", "root event with target");
104104

105-
let _root = tracing::span!(tracing::Level::INFO, "root span").entered();
105+
let root = tracing::span!(tracing::Level::INFO, "root span").entered();
106106
log::info!(logger: lf_logger, "hello world log");
107107
log::warn!(logger: lf_logger, "warning log");
108108
log::error!(logger: lf_logger, "error log");
109109
log::debug!(logger: lf_logger, "debug log");
110110
log::trace!(logger: lf_logger, "trace log");
111111

112+
drop(root);
113+
112114
let logs = log_exporter.get_emitted_logs().unwrap();
113115

114116
guard.shutdown().unwrap();

src/macros/impl_.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ use tracing_opentelemetry::OpenTelemetrySpanExt;
1616
// them at the crate root.
1717
pub use crate::{__log as log, __tracing_span as tracing_span};
1818

19+
// Private re-export of `tracing` components, because the macros depend upon it.
20+
pub use tracing::{Level, Span, span as __tracing_span_impl};
21+
1922
#[macro_export]
2023
#[doc(hidden)]
2124
macro_rules! __tracing_span {
2225
(parent: $parent:expr, $level:expr, $format:expr, $($($path:ident).+ $(= $value:expr)?),*) => {{
2326
// bind args early to avoid multiple evaluation
2427
$crate::__bind_single_ident_args!($($($path).+ $(= $value)?),*);
25-
tracing::span!(
28+
$crate::__macros_impl::__tracing_span_impl!(
2629
parent: $parent,
2730
$level,
2831
$format,
@@ -34,7 +37,7 @@ macro_rules! __tracing_span {
3437
($level:expr, $format:expr, $($($path:ident).+ $(= $value:expr)?),*) => {{
3538
// bind args early to avoid multiple evaluation
3639
$crate::__bind_single_ident_args!($($($path).+ $(= $value)?),*);
37-
tracing::span!(
40+
$crate::__macros_impl::__tracing_span_impl!(
3841
$level,
3942
$format,
4043
$($($path).+ = $crate::__evaluate_arg!($($path).+ $(= $value)?),)*
@@ -261,12 +264,12 @@ macro_rules! __log {
261264
$crate::__macros_impl::export_log(
262265
$format,
263266
&$parent,
264-
format!($format),
267+
::std::format!($format),
265268
$level,
266269
$crate::__json_schema!($($($path).+),*),
267-
Some(::std::borrow::Cow::Borrowed(file!())),
268-
Some(line!()),
269-
Some(module_path!()),
270+
::std::option::Option::Some(::std::borrow::Cow::Borrowed(file!())),
271+
::std::option::Option::Some(line!()),
272+
::std::option::Option::Some(module_path!()),
270273
[
271274
$({
272275
let arg_value = $crate::__evaluate_arg!($($path).+ $(= $value)?);

src/macros/mod.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ macro_rules! span {
9090
$crate::__macros_impl::tracing_span!(parent: $parent, $level, $format, $($($path).+ $(= $value)?),*)
9191
};
9292
(parent: $parent:expr, $format:expr $(, $($path:ident).+ $(= $value:expr)?)* $(,)?) => {
93-
$crate::__macros_impl::tracing_span!(parent: $parent, tracing::Level::INFO, $format, $($($path).+ $(= $value)?),*)
93+
$crate::__macros_impl::tracing_span!(parent: $parent, $crate::__macros_impl::Level::INFO, $format, $($($path).+ $(= $value)?),*)
9494
};
9595
(level: $level:expr, $format:expr $(, $($path:ident).+ $(= $value:expr)?)* $(,)?) => {
9696
$crate::__macros_impl::tracing_span!($level, $format, $($($path).+ $(= $value)?),*)
9797
};
9898
($format:expr $(, $($path:ident).+ $(= $value:expr)?)* $(,)?) => {
99-
$crate::__macros_impl::tracing_span!(tracing::Level::INFO, $format, $($($path).+ $(= $value)?),*)
99+
$crate::__macros_impl::tracing_span!($crate::__macros_impl::Level::INFO, $format, $($($path).+ $(= $value)?),*)
100100
};
101101
}
102102

@@ -106,10 +106,10 @@ macro_rules! span {
106106
#[macro_export]
107107
macro_rules! error {
108108
(parent: $parent:expr, $format:expr $(, $($path:ident).+ $(= $value:expr)?)* $(,)?) => {
109-
$crate::log!(parent: $parent, tracing::Level::ERROR, $format, $($($path).+ $(= $value)?),*)
109+
$crate::log!(parent: $parent, $crate::__macros_impl::Level::ERROR, $format, $($($path).+ $(= $value)?),*)
110110
};
111111
($format:expr $(, $($path:ident).+ $(= $value:expr)?)* $(,)?) => {
112-
$crate::log!(tracing::Level::ERROR, $format, $($($path).+ $(= $value)?),*)
112+
$crate::log!($crate::__macros_impl::Level::ERROR, $format, $($($path).+ $(= $value)?),*)
113113
};
114114
}
115115

@@ -119,10 +119,10 @@ macro_rules! error {
119119
#[macro_export]
120120
macro_rules! warn {
121121
(parent: $parent:expr, $format:expr $(, $($path:ident).+ $(= $value:expr)?)* $(,)?) => {
122-
$crate::log!(parent: $parent, tracing::Level::WARN, $format, $($($path).+ $(= $value)?),*)
122+
$crate::log!(parent: $parent, $crate::__macros_impl::Level::WARN, $format, $($($path).+ $(= $value)?),*)
123123
};
124124
($format:expr $(, $($path:ident).+ $(= $value:expr)?)* $(,)?) => {
125-
$crate::log!(tracing::Level::WARN, $format, $($($path).+ $(= $value)?),*)
125+
$crate::log!($crate::__macros_impl::Level::WARN, $format, $($($path).+ $(= $value)?),*)
126126
};
127127
}
128128

@@ -132,10 +132,10 @@ macro_rules! warn {
132132
#[macro_export]
133133
macro_rules! info {
134134
(parent: $parent:expr, $format:expr $(, $($path:ident).+ $(= $value:expr)?)* $(,)?) => {
135-
$crate::log!(parent: $parent, tracing::Level::INFO, $format, $($($path).+ $(= $value)?),*)
135+
$crate::log!(parent: $parent, $crate::__macros_impl::Level::INFO, $format, $($($path).+ $(= $value)?),*)
136136
};
137137
($format:expr $(, $($path:ident).+ $(= $value:expr)?)* $(,)?) => {
138-
$crate::log!(tracing::Level::INFO, $format, $($($path).+ $(= $value)?),*)
138+
$crate::log!($crate::__macros_impl::Level::INFO, $format, $($($path).+ $(= $value)?),*)
139139
};
140140
}
141141

@@ -145,10 +145,10 @@ macro_rules! info {
145145
#[macro_export]
146146
macro_rules! debug {
147147
(parent: $parent:expr, $format:expr $(, $($path:ident).+ $(= $value:expr)?)* $(,)?) => {
148-
$crate::log!(parent: $parent, tracing::Level::DEBUG, $format, $($($path).+ $(= $value)?),*)
148+
$crate::log!(parent: $parent, $crate::__macros_impl::Level::DEBUG, $format, $($($path).+ $(= $value)?),*)
149149
};
150150
($format:expr $(, $($path:ident).+ $(= $value:expr)?)* $(,)?) => {
151-
$crate::log!(tracing::Level::DEBUG, $format, $($($path).+ $(= $value)?),*)
151+
$crate::log!($crate::__macros_impl::Level::DEBUG, $format, $($($path).+ $(= $value)?),*)
152152
};
153153
}
154154

@@ -158,10 +158,10 @@ macro_rules! debug {
158158
#[macro_export]
159159
macro_rules! trace {
160160
(parent: $parent:expr, $format:expr $(, $($path:ident).+ $(= $value:expr)?)* $(,)?) => {
161-
$crate::log!(parent: $parent, tracing::Level::TRACE, $format, $($($path).+ $(= $value)?),*)
161+
$crate::log!(parent: $parent, $crate::__macros_impl::Level::TRACE, $format, $($($path).+ $(= $value)?),*)
162162
};
163163
($format:expr $(, $($path:ident).+ $(= $value:expr)?)* $(,)?) => {
164-
$crate::log!(tracing::Level::TRACE, $format, $($($path).+ $(= $value)?),*)
164+
$crate::log!($crate::__macros_impl::Level::TRACE, $format, $($($path).+ $(= $value)?),*)
165165
};
166166
}
167167

@@ -265,7 +265,7 @@ macro_rules! log {
265265
$crate::__macros_impl::log!(parent: $parent, $level, $format, $($($path).+ $(= $value)?),*)
266266
};
267267
($level:expr, $format:expr $(, $($path:ident).+ $(= $value:expr)?)* $(,)?) => {
268-
$crate::__macros_impl::log!(parent: tracing::Span::current(), $level, $format, $($($path).+ $(= $value)?),*)
268+
$crate::__macros_impl::log!(parent: $crate::__macros_impl::Span::current(), $level, $format, $($($path).+ $(= $value)?),*)
269269
};
270270
}
271271

tests/test_macro_hygiene.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![no_implicit_prelude]
2+
//! Tests that logfire macros compile without external dependencies.
3+
4+
#[test]
5+
fn test_macros() {
6+
// If the macros are not hygienic, they will fail to compile due to the
7+
// use of `no_implicit_prelude` above.
8+
::logfire::span!("Test span");
9+
10+
::logfire::trace!("Test trace");
11+
::logfire::debug!("Test debug");
12+
::logfire::info!("Test info");
13+
::logfire::warn!("Test warn");
14+
::logfire::error!("Test error");
15+
}

0 commit comments

Comments
 (0)