mirror of
https://github.com/gabime/spdlog.git
synced 2024-11-15 08:25:43 +08:00
Test Case for conditional logging
This commit is contained in:
parent
380233b727
commit
de0154c525
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
148
tests/cond_logging.cpp
Normal file
148
tests/cond_logging.cpp
Normal file
@ -0,0 +1,148 @@
|
||||
|
||||
#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
|
||||
}
|
||||
}
|
@ -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">
|
||||
|
Loading…
Reference in New Issue
Block a user