]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rust: function macro now returns the function name
authorJason Ish <jason.ish@oisf.net>
Tue, 25 Aug 2020 19:15:21 +0000 (13:15 -0600)
committerVictor Julien <victor@inliniac.net>
Thu, 3 Sep 2020 11:04:14 +0000 (13:04 +0200)
Borrow a macro from https://github.com/popzxc/stdext-rs that
will give us the Rust function name in SCLog messages in Rust.

As this trick only works on Rust 1.38 and newer, keep the old
macro around and set a feature based on a Rust version test
done during ./configure.

configure.ac
rust/Cargo.toml.in
rust/src/log.rs

index a0fb7e2247e441f6040462a00458586419c55885..3783bc10820a4e31cbe5f4e18f6c549b2e877696 100644 (file)
@@ -2517,6 +2517,12 @@ fi
        [])
     AC_MSG_RESULT(yes)
 
+    RUST_FEATURES=""
+    AS_VERSION_COMPARE([$rustc_version], [1.38.0],
+        [],
+       [RUST_FEATURES="$RUST_FEATURES function-macro"],
+       [RUST_FEATURES="$RUST_FEATURES function-macro"])
+
     rust_vendor_comment="# "
     have_rust_vendor="no"
 
@@ -2765,6 +2771,7 @@ AC_SUBST(CONFIGURE_SYSCONDIR)
 AC_SUBST(CONFIGURE_LOCALSTATEDIR)
 AC_SUBST(CONFIGURE_DATAROOTDIR)
 AC_SUBST(PACKAGE_VERSION)
+AC_SUBST(RUST_FEATURES)
 
 AC_CONFIG_FILES(Makefile src/Makefile rust/Makefile rust/Cargo.toml rust/.cargo/config)
 AC_CONFIG_FILES(qa/Makefile qa/coccinelle/Makefile)
index 7e7e1b105fa19b18ec0b0d603b816d14cdc61bfc..b098c90cc3a1cd4de8e57b2dab78dabec66c1f4b 100644 (file)
@@ -16,6 +16,7 @@ lua_int8 = ["lua"]
 strict = []
 debug = []
 debug-validate = []
+function-macro = []
 
 [dependencies]
 nom = "= 5.1.1"
index aeec3cbe8d05a31ec5ae86d6cd0e834c4f7d9411..ff958592ddf17c4e8fad4a7cc51bc35b2a183bd1 100644 (file)
@@ -68,9 +68,28 @@ pub fn sclog(level: Level, file: &str, line: u32, function: &str,
                    message);
 }
 
-/// Return the function name, but for now just return <rust> as Rust
-/// has no macro to return the function name, but may in the future,
-/// see: https://github.com/rust-lang/rfcs/pull/1719
+// This macro returns the function name.
+//
+// This macro has been borrowed from https://github.com/popzxc/stdext-rs, which
+// is released under the MIT license as there is currently no macro in Rust
+// to provide the function name.
+#[cfg(feature = "function-macro")]
+#[macro_export(local_inner_macros)]
+macro_rules!function {
+    () => {{
+         // Okay, this is ugly, I get it. However, this is the best we can get on a stable rust.
+         fn __f() {}
+         fn type_name_of<T>(_: T) -> &'static str {
+             std::any::type_name::<T>()
+         }
+         let name = type_name_of(__f);
+         &name[..name.len() - 5]
+    }}
+}
+
+// Rust versions less than 1.38 can not use the above macro, so keep the old
+// macro around for a while.
+#[cfg(not(feature = "function-macro"))]
 #[macro_export(local_inner_macros)]
 macro_rules!function {
     () => {{ "<rust>" }}