From de0154c525779ac1692aad13e7006be839dc44d8 Mon Sep 17 00:00:00 2001 From: Asit Kumar Dhal Date: Sat, 17 Jun 2017 17:50:46 +0200 Subject: [PATCH] Test Case for conditional logging --- include/spdlog/details/logger_impl.h | 2 +- tests/cond_logging.cpp | 148 +++++++++++++++++++++++++++ tests/tests.vcxproj | 1 + tests/tests.vcxproj.filters | 3 + 4 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 tests/cond_logging.cpp diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index 2619ccf8..044b2d8f 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -310,7 +310,7 @@ inline void spdlog::logger::error_if(const bool flag, const T& msg) { if (flag) { - log(level::error, msg); + log(level::err, msg); } } diff --git a/tests/cond_logging.cpp b/tests/cond_logging.cpp new file mode 100644 index 00000000..8af3dcc2 --- /dev/null +++ b/tests/cond_logging.cpp @@ -0,0 +1,148 @@ + +#include "includes.h" + +template +std::string conditional_log(const bool flag, const T& what, spdlog::level::level_enum logger_level) +{ + std::ostringstream oss; + auto oss_sink = std::make_shared(oss); + + spdlog::logger oss_logger("oss", oss_sink); + oss_logger.set_level(logger_level); + oss_logger.set_pattern("%v"); + + switch (logger_level) + { + case spdlog::level::trace: + oss_logger.trace_if(flag, what); + break; + case spdlog::level::debug: + oss_logger.debug_if(flag, what); + break; + case spdlog::level::info: + oss_logger.info_if(flag, what); + break; + case spdlog::level::warn: + oss_logger.warn_if(flag, what); + break; + case spdlog::level::err: + oss_logger.error_if(flag, what); + break; + case spdlog::level::critical: + oss_logger.critical_if(flag, what); + break; + } + + return oss.str().substr(0, oss.str().length() - spdlog::details::os::eol_size); +} + +template +std::string conditional_log_varags(spdlog::level::level_enum logger_level, const bool flag, const char* fmt, const Arg1& arg1, const Args&... args) +{ + std::ostringstream oss; + auto oss_sink = std::make_shared(oss); + + spdlog::logger oss_logger("oss", oss_sink); + oss_logger.set_level(logger_level); + oss_logger.set_pattern("%v"); + + switch (logger_level) + { + case spdlog::level::trace: + oss_logger.trace_if(flag, fmt, arg1, args...); + break; + case spdlog::level::debug: + oss_logger.debug_if(flag, fmt, arg1, args...); + break; + case spdlog::level::info: + oss_logger.info_if(flag, fmt, arg1, args...); + break; + case spdlog::level::warn: + oss_logger.warn_if(flag, fmt, arg1, args...); + break; + case spdlog::level::err: + oss_logger.error_if(flag, fmt, arg1, args...); + break; + case spdlog::level::critical: + oss_logger.critical_if(flag, fmt, arg1, args...); + break; + } + + return oss.str().substr(0, oss.str().length() - spdlog::details::os::eol_size); +} + +#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT + +template +std::wstring conditional_log_varags(spdlog::level::level_enum logger_level, const bool flag, const wchar_t* fmt, const Arg1& arg1, const Args&... args) +{ + std::wstringstream oss; + auto oss_sink = std::make_shared(oss); + + spdlog::logger oss_logger("oss", oss_sink); + oss_logger.set_level(logger_level); + oss_logger.set_pattern("%v"); + + switch (logger_level) + { + case spdlog::level::trace: + oss_logger.trace_if(flag, fmt, arg1, args...); + break; + case spdlog::level::debug: + oss_logger.debug_if(flag, fmt, arg1, args...); + break; + case spdlog::level::info: + oss_logger.info_if(flag, fmt, arg1, args...); + break; + case spdlog::level::warn: + oss_logger.warn_if(flag, fmt, arg1, args...); + break; + case spdlog::level::err: + oss_logger.error_if(flag, fmt, arg1, args...); + break; + case spdlog::level::critical: + oss_logger.critical_if(flag, fmt, arg1, args...); + break; + } + + return oss.str().substr(0, oss.str().length() - spdlog::details::os::eol_size); +} + +#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT + +TEST_CASE("conditional_trace_simple", "[conditional_trace_simple]") +{ + //const char + for (auto i = 0; i < 2; i++) + { + REQUIRE(conditional_log((i % 2 == 0), "Hello", spdlog::level::trace) == ( i % 2 == 0 ? "Hello" : "")); + REQUIRE(conditional_log((i % 2 == 0), "Hello", spdlog::level::debug) == (i % 2 == 0 ? "Hello" : "")); + REQUIRE(conditional_log((i % 2 == 0), "Hello", spdlog::level::info) == (i % 2 == 0 ? "Hello" : "")); + REQUIRE(conditional_log((i % 2 == 0), "Hello", spdlog::level::warn) == (i % 2 == 0 ? "Hello" : "")); + REQUIRE(conditional_log((i % 2 == 0), "Hello", spdlog::level::err) == (i % 2 == 0 ? "Hello" : "")); + REQUIRE(conditional_log((i % 2 == 0), "Hello", spdlog::level::critical) == (i % 2 == 0 ? "Hello" : "")); + } +} + +TEST_CASE("conditional_trace_varargs", "[conditional_trace_varargs]") +{ + //const char + for (auto i = 0; i < 2; i++) + { + REQUIRE(conditional_log_varags(spdlog::level::trace, (i % 2 == 0), "Hello {}", i) == (i % 2 == 0 ? "Hello " + std::to_string(i) : "")); + REQUIRE(conditional_log_varags(spdlog::level::debug, (i % 2 == 0), "Hello {}", i) == (i % 2 == 0 ? "Hello " + std::to_string(i) : "")); + REQUIRE(conditional_log_varags(spdlog::level::info, (i % 2 == 0), "Hello {}", i) == (i % 2 == 0 ? "Hello " + std::to_string(i) : "")); + REQUIRE(conditional_log_varags(spdlog::level::warn, (i % 2 == 0), "Hello {}", i) == (i % 2 == 0 ? "Hello " + std::to_string(i) : "")); + REQUIRE(conditional_log_varags(spdlog::level::err, (i % 2 == 0), "Hello {}", i) == (i % 2 == 0 ? "Hello " + std::to_string(i) : "")); + REQUIRE(conditional_log_varags(spdlog::level::critical, (i % 2 == 0), "Hello {}", i) == (i % 2 == 0 ? "Hello " + std::to_string(i) : "")); + +#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT + REQUIRE(conditional_log_varags(spdlog::level::trace, (i % 2 == 0), L"Hello {}", i) == (i % 2 == 0 ? L"Hello " + std::to_wstring(i) : L"")); + REQUIRE(conditional_log_varags(spdlog::level::debug, (i % 2 == 0), L"Hello {}", i) == (i % 2 == 0 ? L"Hello " + std::to_wstring(i) : L"")); + REQUIRE(conditional_log_varags(spdlog::level::info, (i % 2 == 0), L"Hello {}", i) == (i % 2 == 0 ? L"Hello " + std::to_wstring(i) : L"")); + REQUIRE(conditional_log_varags(spdlog::level::warn, (i % 2 == 0), L"Hello {}", i) == (i % 2 == 0 ? L"Hello " + std::to_wstring(i) : L"")); + REQUIRE(conditional_log_varags(spdlog::level::err, (i % 2 == 0), L"Hello {}", i) == (i % 2 == 0 ? L"Hello " + std::to_wstring(i) : L"")); + REQUIRE(conditional_log_varags(spdlog::level::critical, (i % 2 == 0), L"Hello {}", i) == (i % 2 == 0 ? L"Hello " + std::to_wstring(i) : L"")); +#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT + } +} \ No newline at end of file diff --git a/tests/tests.vcxproj b/tests/tests.vcxproj index be667a68..f5c854db 100644 --- a/tests/tests.vcxproj +++ b/tests/tests.vcxproj @@ -125,6 +125,7 @@ + diff --git a/tests/tests.vcxproj.filters b/tests/tests.vcxproj.filters index b9ede4e7..b5612d0c 100644 --- a/tests/tests.vcxproj.filters +++ b/tests/tests.vcxproj.filters @@ -36,6 +36,9 @@ Source Files + + Source Files +