From 8f6f7fc3ccea4a1a32d17f4c335cbe7040fde068 Mon Sep 17 00:00:00 2001 From: Joseph Reynolds Date: Fri, 4 May 2018 10:11:00 -0500 Subject: static_assert typeof log message entries Please wait to commit this review until all of the code that causes static_assert failures is committed. Resolves openbmc/openbmc#2905 Tested: static_assert only phosphor-logging now static_asserts that each item to be converted by the printf-like function has integral, enum, floating_point, or pointer type and each format argument has a type that decays to a char * type. Specifically, std::string fails the assertion because its address would be logged and not the string buffer. You probably want your strings to use s.c_str(). Note that we considered automatically applying c_str() to string objects, but the underlying entry function is constexpr which makes that impossible. Change-Id: I88f6c626d58254eaad9b1a44e6a9c693d2fc6cd9 Signed-off-by: Joseph Reynolds --- phosphor-logging/log.hpp | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/phosphor-logging/log.hpp b/phosphor-logging/log.hpp index a5ba738..a933591 100644 --- a/phosphor-logging/log.hpp +++ b/phosphor-logging/log.hpp @@ -17,6 +17,7 @@ #pragma once #include +#include #include #include @@ -135,11 +136,43 @@ void log(Msg msg, Entry... e) details::log(log_tuple); } +template +struct is_printf_argtype + : std::integral_constant< + bool, + (std::is_integral::type>::value || + std::is_enum::type>::value || + std::is_floating_point::type>::value || + std::is_pointer::type>::value)> +{}; + +template +struct is_char_ptr_argtype + : std::integral_constant< + bool, + (std::is_pointer::type>::value && + std::is_same::type>::type>::type, + char>::value)> +{}; + +template +struct bool_pack; + +template +using all_true = std::is_same, bool_pack>; + template constexpr auto entry(Arg&& arg, Args&&... args) { + static_assert(is_char_ptr_argtype::value, + "bad argument type: use char*"); + static_assert(all_true::value...>::value, + "bad argument type: use string.c_str() if needed"); const auto entry_tuple = std::make_tuple(std::forward(arg), - std::forward(args)...); + std::forward(args)...); return entry_tuple; } -- cgit v1.2.1