From ed7c3a83f837ac00a6da3cd4246fa96166f48810 Mon Sep 17 00:00:00 2001 From: Asit Kumar Dhal Date: Sat, 17 Jun 2017 02:45:24 +0200 Subject: [PATCH 1/7] conditional logging implementation --- include/spdlog/details/logger_impl.h | 18 ++++++++++++++++++ include/spdlog/logger.h | 4 ++++ 2 files changed, 22 insertions(+) diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index c6aa125b..2b0c2d8f 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -146,6 +146,15 @@ inline void spdlog::logger::warn(const char* fmt, const Arg1 &arg1, const Args&. log(level::warn, 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(const char* fmt, const Arg1 &arg1, const Args&... args) { @@ -184,6 +193,15 @@ inline void spdlog::logger::warn(const T& msg) log(level::warn, 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(const T& msg) { diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index fce99f9a..9f38332c 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -43,6 +43,8 @@ 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 warn_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); @@ -62,6 +64,8 @@ public: template void error(const T&); template void critical(const T&); + template void warn_if(const bool flag, const T&); + bool should_log(level::level_enum) const; void set_level(level::level_enum); level::level_enum level() const; From 9a189badbd7bc3011037b38aad26e140cc979f3e Mon Sep 17 00:00:00 2001 From: Asit Kumar Dhal Date: Sat, 17 Jun 2017 02:45:24 +0200 Subject: [PATCH 2/7] conditional logging implementation --- include/spdlog/details/logger_impl.h | 18 ++++++++++++++++++ include/spdlog/logger.h | 4 ++++ 2 files changed, 22 insertions(+) diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index c6aa125b..2b0c2d8f 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -146,6 +146,15 @@ inline void spdlog::logger::warn(const char* fmt, const Arg1 &arg1, const Args&. log(level::warn, 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(const char* fmt, const Arg1 &arg1, const Args&... args) { @@ -184,6 +193,15 @@ inline void spdlog::logger::warn(const T& msg) log(level::warn, 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(const T& msg) { diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index fce99f9a..9f38332c 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -43,6 +43,8 @@ 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 warn_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); @@ -62,6 +64,8 @@ public: template void error(const T&); template void critical(const T&); + template void warn_if(const bool flag, const T&); + bool should_log(level::level_enum) const; void set_level(level::level_enum); level::level_enum level() const; From 380233b727215ca0ffe026f8f938f47b096f60fa Mon Sep 17 00:00:00 2001 From: Asit Kumar Dhal Date: Sat, 17 Jun 2017 17:24:16 +0200 Subject: [PATCH 3/7] mend --- include/spdlog/details/logger_impl.h | 78 ++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index 9ea78eb4..2619ccf8 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -380,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 From de0154c525779ac1692aad13e7006be839dc44d8 Mon Sep 17 00:00:00 2001 From: Asit Kumar Dhal Date: Sat, 17 Jun 2017 17:50:46 +0200 Subject: [PATCH 4/7] 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 + From a767f07ba3d5531e7feb74d58f7a8140dc9120b5 Mon Sep 17 00:00:00 2001 From: Asit Kumar Dhal Date: Sat, 17 Jun 2017 19:10:52 +0200 Subject: [PATCH 5/7] Conditional logging in the example --- example/example.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/example/example.cpp b/example/example.cpp index 4482e9d7..b62c559a 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -27,6 +27,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); From 868c0cedb0dd6ceba48939329d99e77f26f22c3f Mon Sep 17 00:00:00 2001 From: Asit Kumar Dhal Date: Sat, 17 Jun 2017 20:26:07 +0200 Subject: [PATCH 6/7] Update README file --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) 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); From 97be4532ccc6f6ce493d0009defaa1add42cc007 Mon Sep 17 00:00:00 2001 From: Asit Kumar Dhal Date: Sat, 17 Jun 2017 23:42:31 +0200 Subject: [PATCH 7/7] trace_if and debug_if macro added --- example/example.cpp | 8 ++++++++ include/spdlog/spdlog.h | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/example/example.cpp b/example/example.cpp index b62c559a..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 @@ -40,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"); @@ -74,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/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 - }