Skip to content

Commit

Permalink
Emit an error in macro runtime_pattern if crate feature is not enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
SpriteOvO committed Nov 20, 2024
1 parent 5e20c0e commit dfa2fb3
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions spdlog-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TokenStream2>) -> TokenStream {
match result {
Ok(stream) => stream.into(),
Expand Down
4 changes: 4 additions & 0 deletions spdlog/src/formatter/pattern_formatter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<P> {
Expand Down
2 changes: 2 additions & 0 deletions spdlog/tests/compile_fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
7 changes: 7 additions & 0 deletions spdlog/tests/compile_fail/pattern_runtime_disabled.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use spdlog::formatter::runtime_pattern;

fn runtime_pattern() {
runtime_pattern!("{logger}");
}

fn main() {}
7 changes: 7 additions & 0 deletions spdlog/tests/compile_fail/pattern_runtime_disabled.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit dfa2fb3

Please sign in to comment.