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.
pull/5356/head
Jason Ish 5 years ago committed by Victor Julien
parent bac8016d17
commit ea1338b464

@ -2517,6 +2517,12 @@ fi
[]) [])
AC_MSG_RESULT(yes) 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="# " rust_vendor_comment="# "
have_rust_vendor="no" have_rust_vendor="no"
@ -2765,6 +2771,7 @@ AC_SUBST(CONFIGURE_SYSCONDIR)
AC_SUBST(CONFIGURE_LOCALSTATEDIR) AC_SUBST(CONFIGURE_LOCALSTATEDIR)
AC_SUBST(CONFIGURE_DATAROOTDIR) AC_SUBST(CONFIGURE_DATAROOTDIR)
AC_SUBST(PACKAGE_VERSION) 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(Makefile src/Makefile rust/Makefile rust/Cargo.toml rust/.cargo/config)
AC_CONFIG_FILES(qa/Makefile qa/coccinelle/Makefile) AC_CONFIG_FILES(qa/Makefile qa/coccinelle/Makefile)

@ -16,6 +16,7 @@ lua_int8 = ["lua"]
strict = [] strict = []
debug = [] debug = []
debug-validate = [] debug-validate = []
function-macro = []
[dependencies] [dependencies]
nom = "= 5.1.1" nom = "= 5.1.1"

@ -68,9 +68,28 @@ pub fn sclog(level: Level, file: &str, line: u32, function: &str,
message); message);
} }
/// Return the function name, but for now just return <rust> as Rust // This macro returns the function name.
/// has no macro to return the function name, but may in the future, //
/// see: https://github.com/rust-lang/rfcs/pull/1719 // 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_export(local_inner_macros)]
macro_rules!function { macro_rules!function {
() => {{ "<rust>" }} () => {{ "<rust>" }}

Loading…
Cancel
Save