From 605e9ed7ef6df9d99b1edfcbe73eeceb94ae0010 Mon Sep 17 00:00:00 2001 From: Eric Frias Date: Wed, 6 Apr 2016 10:19:28 -0400 Subject: [PATCH] Change the variable substitution used in ilog(), FC_THROW() and similar to make providing a name for the values to be logged optional; if not specified, the variable name will be generated. In other words, you can now say ilog("${foo}", (foo)) instead of ilog("${foo}", ("foo", foo)). --- include/fc/exception/exception.hpp | 2 +- include/fc/log/log_message.hpp | 21 +++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/include/fc/exception/exception.hpp b/include/fc/exception/exception.hpp index 028b172..35d9f27 100644 --- a/include/fc/exception/exception.hpp +++ b/include/fc/exception/exception.hpp @@ -449,7 +449,7 @@ namespace fc FC_RETHROW_EXCEPTION( er, LOG_LEVEL, FORMAT, __VA_ARGS__ ); \ } catch( const std::exception& e ) { \ fc::exception fce( \ - FC_LOG_MESSAGE( LOG_LEVEL, "${what}: " FORMAT,__VA_ARGS__("what",e.what())), \ + BOOST_PP_EXPAND(FC_LOG_MESSAGE( LOG_LEVEL, "${what}: " FORMAT,__VA_ARGS__("what",e.what()))), \ fc::std_exception_code,\ typeid(e).name(), \ e.what() ) ; throw fce;\ diff --git a/include/fc/log/log_message.hpp b/include/fc/log/log_message.hpp index 1928a98..c17c0e6 100644 --- a/include/fc/log/log_message.hpp +++ b/include/fc/log/log_message.hpp @@ -8,6 +8,14 @@ #include #include +#include +#include +#include +#include +#include +#include +#include + namespace fc { namespace detail @@ -157,6 +165,15 @@ FC_REFLECT_TYPENAME( fc::log_message ); * @param FORMAT A const char* string containing zero or more references to keys as "${key}" * @param ... A set of key/value pairs denoted as ("key",val)("key2",val2)... */ -#define FC_LOG_MESSAGE( LOG_LEVEL, FORMAT, ... ) \ - fc::log_message( FC_LOG_CONTEXT(LOG_LEVEL), FORMAT, fc::mutable_variant_object()__VA_ARGS__ ) +#define FC_LOG_MESSAGE_GENERATE_PARAMETER_NAME(VALUE) BOOST_PP_LPAREN() BOOST_PP_STRINGIZE(VALUE), fc::variant(VALUE) BOOST_PP_RPAREN() +#define FC_LOG_MESSAGE_DONT_GENERATE_PARAMETER_NAME(NAME, VALUE) BOOST_PP_LPAREN() NAME, fc::variant(VALUE) BOOST_PP_RPAREN() +#define FC_LOG_MESSAGE_GENERATE_PARAMETER_NAMES_IF_NEEDED(r, data, PARAMETER_AND_MAYBE_NAME) BOOST_PP_IF(BOOST_PP_EQUAL(BOOST_PP_VARIADIC_SIZE PARAMETER_AND_MAYBE_NAME,1),FC_LOG_MESSAGE_GENERATE_PARAMETER_NAME,FC_LOG_MESSAGE_DONT_GENERATE_PARAMETER_NAME)PARAMETER_AND_MAYBE_NAME +#define FC_LOG_MESSAGE_STRING_ONLY(LOG_LEVEL, FORMAT) \ + fc::log_message(FC_LOG_CONTEXT(LOG_LEVEL), FORMAT, fc::variant_object()) +#define FC_LOG_MESSAGE_WITH_SUBSTITUTIONS(LOG_LEVEL, FORMAT, ...) \ + fc::log_message(FC_LOG_CONTEXT(LOG_LEVEL), FORMAT, fc::mutable_variant_object() BOOST_PP_SEQ_FOR_EACH(FC_LOG_MESSAGE_GENERATE_PARAMETER_NAMES_IF_NEEDED, _, BOOST_PP_VARIADIC_SEQ_TO_SEQ(__VA_ARGS__))) + + +#define FC_LOG_MESSAGE(LOG_LEVEL, ...) \ + BOOST_PP_EXPAND(BOOST_PP_IF(BOOST_PP_EQUAL(BOOST_PP_VARIADIC_SIZE(__VA_ARGS__),1),FC_LOG_MESSAGE_STRING_ONLY,FC_LOG_MESSAGE_WITH_SUBSTITUTIONS)(LOG_LEVEL,__VA_ARGS__))