Merge pull request #460 from asit-dhal/master

conditional logging
This commit is contained in:
Gabi Melman 2017-06-18 03:13:53 +03:00 committed by GitHub
commit c0f0fd713d
8 changed files with 409 additions and 1 deletions

View File

@ -30,6 +30,7 @@ Very fast, header only, C++ logging library. [![Build Status](https://travis-ci.
* Feature rich [call style](#usage-example) using the excellent [fmt](https://github.com/fmtlib/fmt) library.
* Extremely fast asynchronous mode (optional) - using lockfree queues and other tricks to reach millions of calls/sec.
* [Custom](https://github.com/gabime/spdlog/wiki/3.-Custom-formatting) formatting.
* Conditional Logging
* Multi/Single threaded loggers.
* Various log targets:
* Rotating log files.
@ -91,6 +92,12 @@ int main(int, char*[])
console->info("Welcome to spdlog!");
console->error("Some error message with arg{}..", 1);
// Conditional logging example
auto i = 2;
console->info_if(i < 20, "Welcome to spdlog conditional logging!");
console->warn_if(i != 0, "an important message");
console->critical_if(i != 2, "a false warning which won't show up");
// Formatting examples
console->warn("Easy padding in numbers like {:08d}", 12);
console->critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42);

View File

@ -6,6 +6,10 @@
// spdlog usage example
//
//
#define SPDLOG_TRACE_ON
#define SPDLOG_DEBUG_ON
#include "spdlog/spdlog.h"
#include <iostream>
@ -27,6 +31,12 @@ int main(int, char*[])
console->info("Welcome to spdlog!");
console->error("Some error message with arg{}..", 1);
// Conditional logging example
auto i = 2;
console->info_if(i < 20, "Welcome to spdlog conditional logging!");
console->warn_if(i != 0, "an important message");
console->critical_if(i != 2, "a false warning which won't show up");
// Formatting examples
console->warn("Easy padding in numbers like {:08d}", 12);
console->critical("Support for int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}", 42);
@ -34,6 +44,8 @@ int main(int, char*[])
console->info("Positional args are {1} {0}..", "too", "supported");
console->info("{:<30}", "left aligned");
SPDLOG_DEBUG_IF(console, true, "This is a debug log");
spd::get("console")->info("loggers can be retrieved from a global registry using the spdlog::get(logger_name) function");
@ -68,6 +80,8 @@ int main(int, char*[])
// define SPDLOG_DEBUG_ON or SPDLOG_TRACE_ON
SPDLOG_TRACE(console, "Enabled only #ifdef SPDLOG_TRACE_ON..{} ,{}", 1, 3.23);
SPDLOG_DEBUG(console, "Enabled only #ifdef SPDLOG_DEBUG_ON.. {} ,{}", 1, 3.23);
SPDLOG_DEBUG_IF(console, true, "This is a debug log");
// Asynchronous logging is very fast..
// Just call spdlog::set_async_mode(q_size) and all created loggers from now on will be asynchronous..

View File

@ -158,6 +158,79 @@ inline void spdlog::logger::critical(const char* fmt, const Arg1 &arg1, const Ar
log(level::critical, fmt, arg1, args...);
}
template <typename... Args>
inline void spdlog::logger::log_if(const bool flag, level::level_enum lvl, const char* msg)
{
if (flag)
{
log(lvl, msg);
}
}
template<typename T>
inline void spdlog::logger::log_if(const bool flag, level::level_enum lvl, const T& msg)
{
if (flag)
{
log(lvl, msg);
}
}
template <typename Arg1, typename... Args>
inline void spdlog::logger::trace_if(const bool flag, const char* fmt, const Arg1 &arg1, const Args&... args)
{
if (flag)
{
log(level::trace, fmt, arg1, args...);
}
}
template <typename Arg1, typename... Args>
inline void spdlog::logger::debug_if(const bool flag, const char* fmt, const Arg1 &arg1, const Args&... args)
{
if (flag)
{
log(level::debug, fmt, arg1, args...);
}
}
template <typename Arg1, typename... Args>
inline void spdlog::logger::info_if(const bool flag, const char* fmt, const Arg1 &arg1, const Args&... args)
{
if (flag)
{
log(level::info, fmt, arg1, args...);
}
}
template <typename Arg1, typename... Args>
inline void spdlog::logger::warn_if(const bool flag, const char* fmt, const Arg1& arg1, const Args&... args)
{
if (flag)
{
log(level::warn, fmt, arg1, args...);
}
}
template <typename Arg1, typename... Args>
inline void spdlog::logger::error_if(const bool flag, const char* fmt, const Arg1 &arg1, const Args&... args)
{
if (flag)
{
log(level::err, fmt, arg1, args...);
}
}
template <typename Arg1, typename... Args>
inline void spdlog::logger::critical_if(const bool flag, const char* fmt, const Arg1 &arg1, const Args&... args)
{
if (flag)
{
log(level::critical, fmt, arg1, args...);
}
}
template<typename T>
inline void spdlog::logger::trace(const T& msg)
{
@ -196,6 +269,61 @@ inline void spdlog::logger::critical(const T& msg)
log(level::critical, msg);
}
template<typename T>
inline void spdlog::logger::trace_if(const bool flag, const T& msg)
{
if (flag)
{
log(level::trace, msg);
}
}
template<typename T>
inline void spdlog::logger::debug_if(const bool flag, const T& msg)
{
if (flag)
{
log(level::debug, msg);
}
}
template<typename T>
inline void spdlog::logger::info_if(const bool flag, const T& msg)
{
if (flag)
{
log(level::info, msg);
}
}
template<typename T>
inline void spdlog::logger::warn_if(const bool flag, const T& msg)
{
if (flag)
{
log(level::warn, msg);
}
}
template<typename T>
inline void spdlog::logger::error_if(const bool flag, const T& msg)
{
if (flag)
{
log(level::err, msg);
}
}
template<typename T>
inline void spdlog::logger::critical_if(const bool flag, const T& msg)
{
if (flag)
{
log(level::critical, msg);
}
}
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
#include <codecvt>
@ -252,6 +380,84 @@ inline void spdlog::logger::critical(const wchar_t* fmt, const Args&... args)
{
log(level::critical, fmt, args...);
}
//
// conditional logging
//
template <typename... Args>
inline void spdlog::logger::log_if(const bool flag, level::level_enum lvl, const wchar_t* msg)
{
if (flag)
{
log(lvl, msg);
}
}
template <typename... Args>
inline void spdlog::logger::log_if(const bool flag, level::level_enum lvl, const wchar_t* fmt, const Args&... args)
{
if (flag)
{
log(lvl, fmt, args);
}
}
template <typename... Args>
inline void spdlog::logger::trace_if(const bool flag, const wchar_t* fmt, const Args&... args)
{
if (flag)
{
log(level::trace, fmt, args...);
}
}
template <typename... Args>
inline void spdlog::logger::debug_if(const bool flag, const wchar_t* fmt, const Args&... args)
{
if (flag)
{
log(level::debug, fmt, args...);
}
}
template <typename... Args>
inline void spdlog::logger::info_if(const bool flag, const wchar_t* fmt, const Args&... args)
{
if (flag)
{
log(level::info, fmt, args...);
}
}
template <typename... Args>
inline void spdlog::logger::warn_if(const bool flag, const wchar_t* fmt, const Args&... args)
{
if (flag)
{
log(level::warn, fmt, args...);
}
}
template <typename... Args>
inline void spdlog::logger::error_if(const bool flag, const wchar_t* fmt, const Args&... args)
{
if (flag)
{
log(level::err, fmt, args...);
}
}
template <typename... Args>
inline void spdlog::logger::critical_if(const bool flag, const wchar_t* fmt, const Args&... args)
{
if (flag)
{
log(level::critical, fmt, args...);
}
}
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT

View File

@ -43,6 +43,16 @@ public:
template <typename Arg1, typename... Args> void warn(const char* fmt, const Arg1&, const Args&... args);
template <typename Arg1, typename... Args> void error(const char* fmt, const Arg1&, const Args&... args);
template <typename Arg1, typename... Args> void critical(const char* fmt, const Arg1&, const Args&... args);
template <typename... Args> void log_if(const bool flag, level::level_enum lvl, const char* fmt, const Args&... args);
template <typename... Args> void log_if(const bool flag, level::level_enum lvl, const char* msg);
template <typename Arg1, typename... Args> void trace_if(const bool flag, const char* fmt, const Arg1&, const Args&... args);
template <typename Arg1, typename... Args> void debug_if(const bool flag, const char* fmt, const Arg1&, const Args&... args);
template <typename Arg1, typename... Args> void info_if(const bool flag, const char* fmt, const Arg1&, const Args&... args);
template <typename Arg1, typename... Args> void warn_if(const bool flag, const char* fmt, const Arg1&, const Args&... args);
template <typename Arg1, typename... Args> void error_if(const bool flag, const char* fmt, const Arg1&, const Args&... args);
template <typename Arg1, typename... Args> void critical_if(const bool flag, const char* fmt, const Arg1&, const Args&... args);
#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
template <typename... Args> void log(level::level_enum lvl, const wchar_t* msg);
template <typename... Args> void log(level::level_enum lvl, const wchar_t* fmt, const Args&... args);
@ -52,6 +62,15 @@ public:
template <typename... Args> void warn(const wchar_t* fmt, const Args&... args);
template <typename... Args> void error(const wchar_t* fmt, const Args&... args);
template <typename... Args> void critical(const wchar_t* fmt, const Args&... args);
template <typename... Args> void log_if(const bool flag, level::level_enum lvl, const wchar_t* msg);
template <typename... Args> void log_if(const bool flag, level::level_enum lvl, const wchar_t* fmt, const Args&... args);
template <typename... Args> void trace_if(const bool flag, const wchar_t* fmt, const Args&... args);
template <typename... Args> void debug_if(const bool flag, const wchar_t* fmt, const Args&... args);
template <typename... Args> void info_if(const bool flag, const wchar_t* fmt, const Args&... args);
template <typename... Args> void warn_if(const bool flag, const wchar_t* fmt, const Args&... args);
template <typename... Args> void error_if(const bool flag, const wchar_t* fmt, const Args&... args);
template <typename... Args> void critical_if(const bool flag, const wchar_t* fmt, const Args&... args);
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT
template <typename T> void log(level::level_enum lvl, const T&);
@ -62,6 +81,14 @@ public:
template <typename T> void error(const T&);
template <typename T> void critical(const T&);
template <typename T> void log_if(const bool flag, level::level_enum lvl, const T&);
template <typename T> void trace_if(const bool flag, const T&);
template <typename T> void debug_if(const bool flag, const T&);
template <typename T> void info_if(const bool flag, const T&);
template <typename T> void warn_if(const bool flag, const T&);
template <typename T> void error_if(const bool flag, const T&);
template <typename T> void critical_if(const bool flag, const T&);
bool should_log(level::level_enum) const;
void set_level(level::level_enum);
level::level_enum level() const;

View File

@ -162,23 +162,25 @@ void drop_all();
// SPDLOG_TRACE(my_logger, "some trace message");
// SPDLOG_TRACE(my_logger, "another trace message {} {}", 1, 2);
// SPDLOG_DEBUG(my_logger, "some debug message {} {}", 3, 4);
// SPDLOG_DEBUG_IF(my_logger, true, "some debug message {} {}", 3, 4);
///////////////////////////////////////////////////////////////////////////////
#ifdef SPDLOG_TRACE_ON
#define SPDLOG_STR_H(x) #x
#define SPDLOG_STR_HELPER(x) SPDLOG_STR_H(x)
#define SPDLOG_TRACE(logger, ...) logger->trace("[" __FILE__ " line #" SPDLOG_STR_HELPER(__LINE__) "] " __VA_ARGS__)
#define SPDLOG_TRACE_IF(logger, flag, ...) logger->trace_if(flag, "[" __FILE__ " line #" SPDLOG_STR_HELPER(__LINE__) "] " __VA_ARGS__)
#else
#define SPDLOG_TRACE(logger, ...)
#endif
#ifdef SPDLOG_DEBUG_ON
#define SPDLOG_DEBUG(logger, ...) logger->debug(__VA_ARGS__)
#define SPDLOG_DEBUG_IF(logger, flag, ...) logger->debug_if(flag, __VA_ARGS__)
#else
#define SPDLOG_DEBUG(logger, ...)
#endif
}

148
tests/cond_logging.cpp Normal file
View 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
}
}

View 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" />

View File

@ -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">