From dfa2fb3fc44946df7c317ba199454cbbdbb9bfaf Mon Sep 17 00:00:00 2001 From: Asuna Date: Wed, 20 Nov 2024 05:32:57 +0800 Subject: [PATCH] Emit an error in macro `runtime_pattern` if crate feature is not enabled --- spdlog-macros/src/lib.rs | 7 +++++++ spdlog/src/formatter/pattern_formatter/mod.rs | 4 ++++ spdlog/tests/compile_fail.rs | 2 ++ spdlog/tests/compile_fail/pattern_runtime_disabled.rs | 7 +++++++ spdlog/tests/compile_fail/pattern_runtime_disabled.stderr | 7 +++++++ 5 files changed, 27 insertions(+) create mode 100644 spdlog/tests/compile_fail/pattern_runtime_disabled.rs create mode 100644 spdlog/tests/compile_fail/pattern_runtime_disabled.stderr diff --git a/spdlog-macros/src/lib.rs b/spdlog-macros/src/lib.rs index cc1eec0d..c9d60fd6 100644 --- a/spdlog-macros/src/lib.rs +++ b/spdlog-macros/src/lib.rs @@ -26,6 +26,13 @@ pub fn runtime_pattern(input: TokenStream) -> TokenStream { into_or_error(pattern::runtime_pattern_impl(runtime_pattern)) } +#[proc_macro] +pub fn runtime_pattern_disabled(_: TokenStream) -> TokenStream { + panic!( + "macro `runtime_pattern` required to enable crate feature `runtime-pattern` for spdlog-rs" + ); +} + fn into_or_error(result: Result) -> TokenStream { match result { Ok(stream) => stream.into(), diff --git a/spdlog/src/formatter/pattern_formatter/mod.rs b/spdlog/src/formatter/pattern_formatter/mod.rs index 3f82ddf6..5cd607a0 100644 --- a/spdlog/src/formatter/pattern_formatter/mod.rs +++ b/spdlog/src/formatter/pattern_formatter/mod.rs @@ -342,6 +342,10 @@ use crate::{ /// [`FullFormatter`]: crate::formatter::FullFormatter pub use ::spdlog_macros::pattern; +// Emit a compile error if the feature is not enabled. +#[cfg(not(feature = "runtime-pattern"))] +pub use ::spdlog_macros::runtime_pattern_disabled as runtime_pattern; + /// Formats logs according to a specified pattern. #[derive(Clone)] pub struct PatternFormatter

{ diff --git a/spdlog/tests/compile_fail.rs b/spdlog/tests/compile_fail.rs index 3abdc250..17c691e4 100644 --- a/spdlog/tests/compile_fail.rs +++ b/spdlog/tests/compile_fail.rs @@ -5,4 +5,6 @@ fn compile_fail() { t.compile_fail("tests/compile_fail/pattern_macro_*.rs"); #[cfg(feature = "runtime-pattern")] t.compile_fail("tests/compile_fail/pattern_runtime_macro_*.rs"); + #[cfg(not(feature = "runtime-pattern"))] + t.compile_fail("tests/compile_fail/pattern_runtime_disabled.rs"); } diff --git a/spdlog/tests/compile_fail/pattern_runtime_disabled.rs b/spdlog/tests/compile_fail/pattern_runtime_disabled.rs new file mode 100644 index 00000000..261bae95 --- /dev/null +++ b/spdlog/tests/compile_fail/pattern_runtime_disabled.rs @@ -0,0 +1,7 @@ +use spdlog::formatter::runtime_pattern; + +fn runtime_pattern() { + runtime_pattern!("{logger}"); +} + +fn main() {} diff --git a/spdlog/tests/compile_fail/pattern_runtime_disabled.stderr b/spdlog/tests/compile_fail/pattern_runtime_disabled.stderr new file mode 100644 index 00000000..46fee96e --- /dev/null +++ b/spdlog/tests/compile_fail/pattern_runtime_disabled.stderr @@ -0,0 +1,7 @@ +error: proc macro panicked + --> tests/compile_fail/pattern_runtime_disabled.rs:4:5 + | +4 | runtime_pattern!("{logger}"); + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = help: message: macro `runtime_pattern` required to enable crate feature `runtime-pattern` for spdlog-rs