Commit 30ee6904 authored by Joe Burzinski's avatar Joe Burzinski

Add support for FMT_STRING compile time checking. Add negative compilation...

Add support for FMT_STRING compile time checking. Add negative compilation unit test for compilers and c++ standard that support relaxed constexpr.
parent 22a169bc
......@@ -71,6 +71,7 @@ option(SPDLOG_BUILD_EXAMPLE_HO "Build header only example" OFF)
# testing options
option(SPDLOG_BUILD_TESTS "Build tests" OFF)
option(SPDLOG_BUILD_TESTS_HO "Build tests using the header only version" OFF)
option(SPDLOG_BUILD_FAILING_TESTS "Add unit test targets that test for compilation failures" OFF)
# bench options
option(SPDLOG_BUILD_BENCH "Build benchmarks (Requires https://github.com/google/benchmark.git to be installed)" OFF)
......
This diff is collapsed.
......@@ -121,153 +121,102 @@ SPDLOG_API spdlog::logger *default_logger_raw();
SPDLOG_API void set_default_logger(std::shared_ptr<spdlog::logger> default_logger);
template<typename... Args>
inline void log(source_loc source, level::level_enum lvl, string_view_t fmt, const Args &... args)
template<typename FormatString, typename... Args>
inline void log(source_loc source, level::level_enum lvl, FormatString &&fmt, Args &&... args)
{
default_logger_raw()->log(source, lvl, fmt, args...);
default_logger_raw()->log(source, lvl, std::forward<FormatString>(fmt), std::forward<Args>(args)...);
}
template<typename... Args>
inline void log(level::level_enum lvl, string_view_t fmt, const Args &... args)
template<typename FormatString, typename... Args>
inline void log(level::level_enum lvl, FormatString &&fmt, Args &&... args)
{
default_logger_raw()->log(source_loc{}, lvl, fmt, args...);
default_logger_raw()->log(source_loc{}, lvl, std::forward<FormatString>(fmt), std::forward<Args>(args)...);
}
template<typename... Args>
inline void trace(string_view_t fmt, const Args &... args)
template<typename FormatString, typename... Args>
inline void trace(FormatString &&fmt, Args &&... args)
{
default_logger_raw()->trace(fmt, args...);
default_logger_raw()->trace(std::forward<FormatString>(fmt), std::forward<Args>(args)...);
}
template<typename... Args>
inline void debug(string_view_t fmt, const Args &... args)
template<typename FormatString, typename... Args>
inline void debug(FormatString &&fmt, Args &&... args)
{
default_logger_raw()->debug(fmt, args...);
default_logger_raw()->debug(std::forward<FormatString>(fmt), std::forward<Args>(args)...);
}
template<typename... Args>
inline void info(string_view_t fmt, const Args &... args)
template<typename FormatString, typename... Args>
inline void info(FormatString &&fmt, Args &&... args)
{
default_logger_raw()->info(fmt, args...);
default_logger_raw()->info(std::forward<FormatString>(fmt), std::forward<Args>(args)...);
}
template<typename... Args>
inline void warn(string_view_t fmt, const Args &... args)
template<typename FormatString, typename... Args>
inline void warn(FormatString &&fmt, Args &&... args)
{
default_logger_raw()->warn(fmt, args...);
default_logger_raw()->warn(std::forward<FormatString>(fmt), std::forward<Args>(args)...);
}
template<typename... Args>
inline void error(string_view_t fmt, const Args &... args)
template<typename FormatString, typename... Args>
inline void error(FormatString &&fmt, Args &&... args)
{
default_logger_raw()->error(fmt, args...);
default_logger_raw()->error(std::forward<FormatString>(fmt), std::forward<Args>(args)...);
}
template<typename... Args>
inline void critical(string_view_t fmt, const Args &... args)
template<typename FormatString, typename... Args>
inline void critical(FormatString &&fmt, const Args &... args)
{
default_logger_raw()->critical(fmt, args...);
default_logger_raw()->critical(std::forward<FormatString>(fmt), args...);
}
template<typename T>
inline void log(source_loc source, level::level_enum lvl, const T &msg)
inline void log(source_loc source, level::level_enum lvl, T &&msg)
{
default_logger_raw()->log(source, lvl, msg);
default_logger_raw()->log(source, lvl, std::forward<T>(msg));
}
template<typename T>
inline void log(level::level_enum lvl, const T &msg)
inline void log(level::level_enum lvl, T &&msg)
{
default_logger_raw()->log(lvl, msg);
default_logger_raw()->log(lvl, std::forward<T>(msg));
}
template<typename T>
inline void trace(const T &msg)
inline void trace(T &&msg)
{
default_logger_raw()->trace(msg);
default_logger_raw()->trace(std::forward<T>(msg));
}
template<typename T>
inline void debug(const T &msg)
inline void debug(T &&msg)
{
default_logger_raw()->debug(msg);
default_logger_raw()->debug(std::forward<T>(msg));
}
template<typename T>
inline void info(const T &msg)
inline void info(T &&msg)
{
default_logger_raw()->info(msg);
default_logger_raw()->info(std::forward<T>(msg));
}
template<typename T>
inline void warn(const T &msg)
inline void warn(T &&msg)
{
default_logger_raw()->warn(msg);
default_logger_raw()->warn(std::forward<T>(msg));
}
template<typename T>
inline void error(const T &msg)
inline void error(T &&msg)
{
default_logger_raw()->error(msg);
default_logger_raw()->error(std::forward<T>(msg));
}
template<typename T>
inline void critical(const T &msg)
inline void critical(T &&msg)
{
default_logger_raw()->critical(msg);
default_logger_raw()->critical(std::forward<T>(msg));
}
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
template<typename... Args>
inline void log(source_loc source, level::level_enum lvl, wstring_view_t fmt, const Args &... args)
{
default_logger_raw()->log(source, lvl, fmt, args...);
}
template<typename... Args>
inline void log(level::level_enum lvl, wstring_view_t fmt, const Args &... args)
{
default_logger_raw()->log(lvl, fmt, args...);
}
template<typename... Args>
inline void trace(wstring_view_t fmt, const Args &... args)
{
default_logger_raw()->trace(fmt, args...);
}
template<typename... Args>
inline void debug(wstring_view_t fmt, const Args &... args)
{
default_logger_raw()->debug(fmt, args...);
}
template<typename... Args>
inline void info(wstring_view_t fmt, const Args &... args)
{
default_logger_raw()->info(fmt, args...);
}
template<typename... Args>
inline void warn(wstring_view_t fmt, const Args &... args)
{
default_logger_raw()->warn(fmt, args...);
}
template<typename... Args>
inline void error(wstring_view_t fmt, const Args &... args)
{
default_logger_raw()->error(fmt, args...);
}
template<typename... Args>
inline void critical(wstring_view_t fmt, const Args &... args)
{
default_logger_raw()->critical(fmt, args...);
}
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
} // namespace spdlog
//
......
......@@ -68,3 +68,29 @@ endif()
if(SPDLOG_BUILD_TESTS_HO OR SPDLOG_BUILD_ALL)
spdlog_prepare_test(spdlog-utests-ho spdlog::spdlog_header_only)
endif()
# Set up compilation failure test case (only available if compiler supports relaxed constexpr and c++ standard is > 11)
set(HAVE_CXX_RELAXED_CONSTEXPR)
if(CMAKE_CXX_STANDARD GREATER 11) # If we're in c++11 mode we don't have relaxed constexpr, even if our compiler is new enough.
list(FIND CMAKE_CXX_COMPILE_FEATURES "cxx_relaxed_constexpr" HAVE_CXX_RELAXED_CONSTEXPR)
endif()
if(HAVE_CXX_RELAXED_CONSTEXPR AND (SPDLOG_BUILD_FAILING_TESTS OR SPDLOG_BUILD_ALL))
message(STATUS "Enabling negative compilation unit test target")
set(SPDLOG_FAIL_COMPILATION_TARGET spdlog_fail_compilation_utests)
add_executable(${SPDLOG_FAIL_COMPILATION_TARGET}
test_compilation_failures.cpp
main.cpp)
spdlog_enable_warnings(${SPDLOG_FAIL_COMPILATION_TARGET})
target_link_libraries(${SPDLOG_FAIL_COMPILATION_TARGET} PRIVATE spdlog::spdlog)
set_target_properties(${SPDLOG_FAIL_COMPILATION_TARGET} PROPERTIES
EXCLUDE_FROM_ALL TRUE
EXCLUDE_FROM_DEFAULT_BUILD TRUE)
add_test(NAME ${SPDLOG_FAIL_COMPILATION_TARGET}
COMMAND ${CMAKE_COMMAND} --build . --target ${SPDLOG_FAIL_COMPILATION_TARGET} --config $<CONFIG>
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
set_tests_properties(${SPDLOG_FAIL_COMPILATION_TARGET} PROPERTIES PASS_REGULAR_EXPRESSION "invalid type specifier")
add_custom_target(${SPDLOG_FAIL_COMPILATION_TARGET}_run_target
COMMAND ${CMAKE_CTEST_COMMAND} -R ${SPDLOG_FAIL_COMPILATION_TARGET} --output-on-failure
COMMENT "Running tests that fail to compile.")
endif()
#include "includes.h"
TEST_CASE("{fmt} FMT_STRING functionality preserved (negative test)", "[fmt][fail][fail compilation]")
{
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
spdlog::set_default_logger(std::make_shared<spdlog::logger>("oss", oss_sink));
spdlog::default_logger()->set_level(spdlog::level::trace);
spdlog::info(FMT_STRING("The best part of {{fmt}} is the compile time checking: {:d}"), "I shouldn't compile");
// This should never be able to compile, so running is a failure.
FAIL("This test case isn't meant to compile, let alone run.");
}
......@@ -3,7 +3,7 @@
#include "spdlog/fmt/bin_to_hex.h"
template<class T>
std::string log_info(const T &what, spdlog::level::level_enum logger_level = spdlog::level::info)
std::string log_info(T &&what, spdlog::level::level_enum logger_level = spdlog::level::info)
{
std::ostringstream oss;
......@@ -12,7 +12,7 @@ std::string log_info(const T &what, spdlog::level::level_enum logger_level = spd
spdlog::logger oss_logger("oss", oss_sink);
oss_logger.set_level(logger_level);
oss_logger.set_pattern("%v");
oss_logger.info(what);
oss_logger.info(std::forward<T>(what));
return oss.str().substr(0, oss.str().length() - strlen(spdlog::details::os::default_eol));
}
......@@ -269,3 +269,34 @@ TEST_CASE("default logger API", "[default logger]")
spdlog::drop_all();
spdlog::set_pattern("%v");
}
TEST_CASE("{fmt} FMT_STRING functionality preserved (positive test)", "[fmt]")
{
std::ostringstream oss;
auto oss_sink = std::make_shared<spdlog::sinks::ostream_sink_mt>(oss);
spdlog::set_default_logger(std::make_shared<spdlog::logger>("oss", oss_sink));
spdlog::default_logger()->set_level(spdlog::level::trace);
spdlog::set_pattern("%v");
const std::string expected_output_all(
std::string("The best part of {fmt} is the compile time checking: 42") + std::string(spdlog::details::os::default_eol));
spdlog::trace(FMT_STRING("The best part of {{fmt}} is the compile time checking: {:d}"), 42);
REQUIRE(oss.str() == expected_output_all);
oss.str("");
spdlog::debug(FMT_STRING("The best part of {{fmt}} is the compile time checking: {:d}"), 42);
REQUIRE(oss.str() == expected_output_all);
oss.str("");
spdlog::info(FMT_STRING("The best part of {{fmt}} is the compile time checking: {:d}"), 42);
REQUIRE(oss.str() == expected_output_all);
oss.str("");
spdlog::warn(FMT_STRING("The best part of {{fmt}} is the compile time checking: {:d}"), 42);
REQUIRE(oss.str() == expected_output_all);
oss.str("");
spdlog::error(FMT_STRING("The best part of {{fmt}} is the compile time checking: {:d}"), 42);
REQUIRE(oss.str() == expected_output_all);
oss.str("");
spdlog::critical(FMT_STRING("The best part of {{fmt}} is the compile time checking: {:d}"), 42);
REQUIRE(oss.str() == expected_output_all);
oss.str("");
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment