diff --git a/README.md b/README.md index 6c39596d..4ceaa304 100644 --- a/README.md +++ b/README.md @@ -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); diff --git a/example/example.cpp b/example/example.cpp index 4482e9d7..b8e80849 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -6,6 +6,10 @@ // spdlog usage example // // + +#define SPDLOG_TRACE_ON +#define SPDLOG_DEBUG_ON + #include "spdlog/spdlog.h" #include @@ -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.. diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index c6aa125b..044b2d8f 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -158,6 +158,79 @@ inline void spdlog::logger::critical(const char* fmt, const Arg1 &arg1, const Ar log(level::critical, fmt, arg1, args...); } +template +inline void spdlog::logger::log_if(const bool flag, level::level_enum lvl, const char* msg) +{ + if (flag) + { + log(lvl, msg); + } +} + +template +inline void spdlog::logger::log_if(const bool flag, level::level_enum lvl, const T& msg) +{ + if (flag) + { + log(lvl, msg); + } +} + +template +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 +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 +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 +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 +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 +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 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 +inline void spdlog::logger::trace_if(const bool flag, const T& msg) +{ + if (flag) + { + log(level::trace, msg); + } +} + +template +inline void spdlog::logger::debug_if(const bool flag, const T& msg) +{ + if (flag) + { + log(level::debug, msg); + } +} + +template +inline void spdlog::logger::info_if(const bool flag, const T& msg) +{ + if (flag) + { + log(level::info, msg); + } +} + +template +inline void spdlog::logger::warn_if(const bool flag, const T& msg) +{ + if (flag) + { + log(level::warn, msg); + } +} + +template +inline void spdlog::logger::error_if(const bool flag, const T& msg) +{ + if (flag) + { + log(level::err, msg); + } +} + +template +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 @@ -252,6 +380,84 @@ inline void spdlog::logger::critical(const wchar_t* fmt, const Args&... args) { log(level::critical, fmt, args...); } + +// +// conditional logging +// + +template +inline void spdlog::logger::log_if(const bool flag, level::level_enum lvl, const wchar_t* msg) +{ + if (flag) + { + log(lvl, msg); + } +} + +template +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 +inline void spdlog::logger::trace_if(const bool flag, const wchar_t* fmt, const Args&... args) +{ + if (flag) + { + log(level::trace, fmt, args...); + } +} + +template +inline void spdlog::logger::debug_if(const bool flag, const wchar_t* fmt, const Args&... args) +{ + if (flag) + { + log(level::debug, fmt, args...); + } +} + +template +inline void spdlog::logger::info_if(const bool flag, const wchar_t* fmt, const Args&... args) +{ + if (flag) + { + log(level::info, fmt, args...); + } +} + + +template +inline void spdlog::logger::warn_if(const bool flag, const wchar_t* fmt, const Args&... args) +{ + if (flag) + { + log(level::warn, fmt, args...); + } +} + +template +inline void spdlog::logger::error_if(const bool flag, const wchar_t* fmt, const Args&... args) +{ + if (flag) + { + log(level::err, fmt, args...); + } +} + +template +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 diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index fce99f9a..4e380a7b 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -43,6 +43,16 @@ public: template void warn(const char* fmt, const Arg1&, const Args&... args); template void error(const char* fmt, const Arg1&, const Args&... args); template void critical(const char* fmt, const Arg1&, const Args&... args); + + template void log_if(const bool flag, level::level_enum lvl, const char* fmt, const Args&... args); + template void log_if(const bool flag, level::level_enum lvl, const char* msg); + template void trace_if(const bool flag, const char* fmt, const Arg1&, const Args&... args); + template void debug_if(const bool flag, const char* fmt, const Arg1&, const Args&... args); + template void info_if(const bool flag, const char* fmt, const Arg1&, const Args&... args); + template void warn_if(const bool flag, const char* fmt, const Arg1&, const Args&... args); + template void error_if(const bool flag, const char* fmt, const Arg1&, const Args&... args); + template void critical_if(const bool flag, const char* fmt, const Arg1&, const Args&... args); + #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT template void log(level::level_enum lvl, const wchar_t* msg); template void log(level::level_enum lvl, const wchar_t* fmt, const Args&... args); @@ -52,6 +62,15 @@ public: template void warn(const wchar_t* fmt, const Args&... args); template void error(const wchar_t* fmt, const Args&... args); template void critical(const wchar_t* fmt, const Args&... args); + + template void log_if(const bool flag, level::level_enum lvl, const wchar_t* msg); + template void log_if(const bool flag, level::level_enum lvl, const wchar_t* fmt, const Args&... args); + template void trace_if(const bool flag, const wchar_t* fmt, const Args&... args); + template void debug_if(const bool flag, const wchar_t* fmt, const Args&... args); + template void info_if(const bool flag, const wchar_t* fmt, const Args&... args); + template void warn_if(const bool flag, const wchar_t* fmt, const Args&... args); + template void error_if(const bool flag, const wchar_t* fmt, const Args&... args); + template void critical_if(const bool flag, const wchar_t* fmt, const Args&... args); #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT template void log(level::level_enum lvl, const T&); @@ -62,6 +81,14 @@ public: template void error(const T&); template void critical(const T&); + template void log_if(const bool flag, level::level_enum lvl, const T&); + template void trace_if(const bool flag, const T&); + template void debug_if(const bool flag, const T&); + template void info_if(const bool flag, const T&); + template void warn_if(const bool flag, const T&); + template void error_if(const bool flag, const T&); + template 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; diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 9170f1d2..fb4ea59e 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -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 - } 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 +