From: Jason Ish Date: Tue, 25 Aug 2020 19:15:21 +0000 (-0600) Subject: rust: function macro now returns the function name X-Git-Tag: suricata-6.0.0-rc1~75 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ea1338b4647b825e3ffb48fcfeba4a890c3e7830;p=thirdparty%2Fsuricata.git rust: function macro now returns the function name 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. --- diff --git a/configure.ac b/configure.ac index a0fb7e2247..3783bc1082 100644 --- a/configure.ac +++ b/configure.ac @@ -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) diff --git a/rust/Cargo.toml.in b/rust/Cargo.toml.in index 7e7e1b105f..b098c90cc3 100644 --- a/rust/Cargo.toml.in +++ b/rust/Cargo.toml.in @@ -16,6 +16,7 @@ lua_int8 = ["lua"] strict = [] debug = [] debug-validate = [] +function-macro = [] [dependencies] nom = "= 5.1.1" diff --git a/rust/src/log.rs b/rust/src/log.rs index aeec3cbe8d..ff958592dd 100644 --- a/rust/src/log.rs +++ b/rust/src/log.rs @@ -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 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) -> &'static str { + std::any::type_name::() + } + 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 { () => {{ "" }}