Commit de0154c5 authored by Asit Kumar Dhal's avatar Asit Kumar Dhal

Test Case for conditional logging

parent 380233b7
......@@ -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);
}
}
......
#include "includes.h"
template<class T>
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<spdlog::sinks::ostream_sink_mt>(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 <typename Arg1, typename... Args>
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<spdlog::sinks::ostream_sink_mt>(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 <typename Arg1, typename... Args>
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<spdlog::sinks::ostream_sink_mt>(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
......@@ -125,6 +125,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="cond_logging.cpp" />
<ClCompile Include="errors.cpp" />
<ClCompile Include="file_helper.cpp" />
<ClCompile Include="file_log.cpp" />
......
......@@ -36,6 +36,9 @@
<ClCompile Include="errors.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="cond_logging.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="includes.h">
......
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