From dcc7b347caadca6d191c40c8ae59a3320ab277ac Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 25 Nov 2017 15:14:36 +0200 Subject: [PATCH] Removed all *_if functions (trace_if, debug_if, info_if,..) because they are redundant and confusing way to preform if --- example/example.cpp | 6 - include/spdlog/details/logger_impl.h | 200 --------------------------- include/spdlog/logger.h | 17 --- include/spdlog/spdlog.h | 6 - tests/cond_logging.cpp | 174 ----------------------- 5 files changed, 403 deletions(-) delete mode 100644 tests/cond_logging.cpp diff --git a/example/example.cpp b/example/example.cpp index 98231ff5..01bf5796 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -31,8 +31,6 @@ int main(int, char*[]) console->info("Welcome to spdlog!"); console->error("Some error message with arg{}..", 1); - // Conditional logging example - console->info_if(true, "Welcome to spdlog conditional logging!"); // Formatting examples console->warn("Easy padding in numbers like {:08d}", 12); @@ -41,9 +39,6 @@ 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"); @@ -77,7 +72,6 @@ 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.. diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index a0da380d..4fbaa86f 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -153,78 +153,6 @@ 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) @@ -264,59 +192,6 @@ 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 @@ -377,81 +252,6 @@ 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 c6837d03..a3c4a9fd 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -44,14 +44,6 @@ public: 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); @@ -62,15 +54,6 @@ 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&); diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 0b92d8cd..0454226b 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -162,7 +162,6 @@ 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 @@ -170,22 +169,17 @@ void drop_all(); #define SPDLOG_STR_HELPER(x) SPDLOG_STR_H(x) #ifdef _MSC_VER #define SPDLOG_TRACE(logger, ...) logger->trace("[ " __FILE__ "(" SPDLOG_STR_HELPER(__LINE__) ") ] " __VA_ARGS__) -#define SPDLOG_TRACE_IF(logger, flag, ...) logger->trace_if(flag, "[ " __FILE__ "(" SPDLOG_STR_HELPER(__LINE__) ") ] " __VA_ARGS__) #else #define SPDLOG_TRACE(logger, ...) logger->trace("[ " __FILE__ ":" SPDLOG_STR_HELPER(__LINE__) " ] " __VA_ARGS__) -#define SPDLOG_TRACE_IF(logger, flag, ...) logger->trace_if(flag, "[ " __FILE__ ":" SPDLOG_STR_HELPER(__LINE__) " ] " __VA_ARGS__) #endif #else #define SPDLOG_TRACE(logger, ...) -#define SPDLOG_TRACE_IF(logger, flag, ...) #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, ...) -#define SPDLOG_DEBUG_IF(logger, flag, ...) #endif } diff --git a/tests/cond_logging.cpp b/tests/cond_logging.cpp deleted file mode 100644 index d501664a..00000000 --- a/tests/cond_logging.cpp +++ /dev/null @@ -1,174 +0,0 @@ - -#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; - default: - 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; - default: - 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; - default: - 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++) - { -#if !defined(SPDLOG_FMT_PRINTF) - 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 - -#else - REQUIRE(conditional_log_varags(spdlog::level::trace, (i % 2 == 0), "Hello %d", i) == (i % 2 == 0 ? "Hello " + std::to_string(i) : "")); - REQUIRE(conditional_log_varags(spdlog::level::debug, (i % 2 == 0), "Hello %d", i) == (i % 2 == 0 ? "Hello " + std::to_string(i) : "")); - REQUIRE(conditional_log_varags(spdlog::level::info, (i % 2 == 0), "Hello %d", i) == (i % 2 == 0 ? "Hello " + std::to_string(i) : "")); - REQUIRE(conditional_log_varags(spdlog::level::warn, (i % 2 == 0), "Hello %d", i) == (i % 2 == 0 ? "Hello " + std::to_string(i) : "")); - REQUIRE(conditional_log_varags(spdlog::level::err, (i % 2 == 0), "Hello %d", i) == (i % 2 == 0 ? "Hello " + std::to_string(i) : "")); - REQUIRE(conditional_log_varags(spdlog::level::critical, (i % 2 == 0), "Hello %d", 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 %d", i) == (i % 2 == 0 ? L"Hello " + std::to_wstring(i) : L"")); - REQUIRE(conditional_log_varags(spdlog::level::debug, (i % 2 == 0), L"Hello %d", i) == (i % 2 == 0 ? L"Hello " + std::to_wstring(i) : L"")); - REQUIRE(conditional_log_varags(spdlog::level::info, (i % 2 == 0), L"Hello %d", i) == (i % 2 == 0 ? L"Hello " + std::to_wstring(i) : L"")); - REQUIRE(conditional_log_varags(spdlog::level::warn, (i % 2 == 0), L"Hello %d", i) == (i % 2 == 0 ? L"Hello " + std::to_wstring(i) : L"")); - REQUIRE(conditional_log_varags(spdlog::level::err, (i % 2 == 0), L"Hello %d", i) == (i % 2 == 0 ? L"Hello " + std::to_wstring(i) : L"")); - REQUIRE(conditional_log_varags(spdlog::level::critical, (i % 2 == 0), L"Hello %d", i) == (i % 2 == 0 ? L"Hello " + std::to_wstring(i) : L"")); -#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT - -#endif // !defined(SPDLOG_FMT_PRINTF) - } -} \ No newline at end of file