Skip to content

Commit dfa2fb3

Browse files
committed
Emit an error in macro runtime_pattern if crate feature is not enabled
1 parent 5e20c0e commit dfa2fb3

File tree

5 files changed

+27
-0
lines changed

5 files changed

+27
-0
lines changed

spdlog-macros/src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ pub fn runtime_pattern(input: TokenStream) -> TokenStream {
2626
into_or_error(pattern::runtime_pattern_impl(runtime_pattern))
2727
}
2828

29+
#[proc_macro]
30+
pub fn runtime_pattern_disabled(_: TokenStream) -> TokenStream {
31+
panic!(
32+
"macro `runtime_pattern` required to enable crate feature `runtime-pattern` for spdlog-rs"
33+
);
34+
}
35+
2936
fn into_or_error(result: Result<TokenStream2>) -> TokenStream {
3037
match result {
3138
Ok(stream) => stream.into(),

spdlog/src/formatter/pattern_formatter/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,10 @@ use crate::{
342342
/// [`FullFormatter`]: crate::formatter::FullFormatter
343343
pub use ::spdlog_macros::pattern;
344344

345+
// Emit a compile error if the feature is not enabled.
346+
#[cfg(not(feature = "runtime-pattern"))]
347+
pub use ::spdlog_macros::runtime_pattern_disabled as runtime_pattern;
348+
345349
/// Formats logs according to a specified pattern.
346350
#[derive(Clone)]
347351
pub struct PatternFormatter<P> {

spdlog/tests/compile_fail.rs

+2
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ fn compile_fail() {
55
t.compile_fail("tests/compile_fail/pattern_macro_*.rs");
66
#[cfg(feature = "runtime-pattern")]
77
t.compile_fail("tests/compile_fail/pattern_runtime_macro_*.rs");
8+
#[cfg(not(feature = "runtime-pattern"))]
9+
t.compile_fail("tests/compile_fail/pattern_runtime_disabled.rs");
810
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use spdlog::formatter::runtime_pattern;
2+
3+
fn runtime_pattern() {
4+
runtime_pattern!("{logger}");
5+
}
6+
7+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
error: proc macro panicked
2+
--> tests/compile_fail/pattern_runtime_disabled.rs:4:5
3+
|
4+
4 | runtime_pattern!("{logger}");
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: message: macro `runtime_pattern` required to enable crate feature `runtime-pattern` for spdlog-rs

0 commit comments

Comments
 (0)