diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 3bd30879..0ce54a9b 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -69,17 +69,29 @@ using level_t = details::null_atomic_int; using level_t = std::atomic; #endif +#define SPDLOG_LEVEL_TRACE 0 +#define SPDLOG_LEVEL_DEBUG 1 +#define SPDLOG_LEVEL_INFO 2 +#define SPDLOG_LEVEL_WARN 3 +#define SPDLOG_LEVEL_ERROR 4 +#define SPDLOG_LEVEL_CRITICAL 5 +#define SPDLOG_LEVEL_OFF 6 + +#if !defined(SPDLOG_ACTIVE_LEVEL) +#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO +#endif + // Log level enum namespace level { enum level_enum { - trace = 0, - debug = 1, - info = 2, - warn = 3, - err = 4, - critical = 5, - off = 6 + trace = SPDLOG_LEVEL_TRACE, + debug = SPDLOG_LEVEL_DEBUG, + info = SPDLOG_LEVEL_INFO, + warn = SPDLOG_LEVEL_WARN, + err = SPDLOG_LEVEL_ERROR, + critical = SPDLOG_LEVEL_CRITICAL, + off = SPDLOG_LEVEL_OFF, }; #if !defined(SPDLOG_LEVEL_NAMES) diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index eb5fcdef..6338586c 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -17,7 +17,7 @@ } \ catch (...) \ { \ - err_handler_("Unknown exeption in logger"); \ + err_handler_("Unknown exception in logger"); \ } // create logger with given name, sinks and the default pattern formatter @@ -26,13 +26,7 @@ template inline spdlog::logger::logger(std::string logger_name, It begin, It end) : name_(std::move(logger_name)) , sinks_(begin, end) - , level_(level::info) - , flush_level_(level::off) - , last_err_time_(0) - , msg_counter_(1) // message counter will start from 1. 0-message id will be - // reserved for control messages { - err_handler_ = [this](const std::string &msg) { this->default_err_handler_(msg); }; } // ctor with sinks as init list diff --git a/include/spdlog/details/pattern_formatter.h b/include/spdlog/details/pattern_formatter.h index 672cae25..5e907e92 100644 --- a/include/spdlog/details/pattern_formatter.h +++ b/include/spdlog/details/pattern_formatter.h @@ -98,8 +98,9 @@ private: const padding_info &padinfo_; fmt::memory_buffer &dest_; size_t total_pad_; - string_view_t spaces_ {" " - " ", 128}; + string_view_t spaces_{" " + " ", + 128}; }; class flag_formatter diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 4e08076b..748529cb 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -158,13 +158,15 @@ protected: // increment the message count (only if defined(SPDLOG_ENABLE_MESSAGE_COUNTER)) void incr_msg_counter_(details::log_msg &msg); + // const spdlog::level_t default_level_{static_cast(SPDLOG_ACTIVE_LEVEL)}; const std::string name_; + std::vector sinks_; - spdlog::level_t level_; - spdlog::level_t flush_level_; - log_err_handler err_handler_; - std::atomic last_err_time_; - std::atomic msg_counter_; + spdlog::level_t level_{static_cast(SPDLOG_ACTIVE_LEVEL)}; + spdlog::level_t flush_level_{level::off}; + log_err_handler err_handler_{[this](const std::string &msg) { this->default_err_handler_(msg); }}; + std::atomic last_err_time_{0}; + std::atomic msg_counter_{1}; }; } // namespace spdlog diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 8c07012e..1f3e26ba 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -290,36 +290,54 @@ inline void critical(const wchar_t *fmt, const Args &... args) #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT -// -// Trace & Debug can be switched on/off at compile time with zero cost. -// Uncomment SPDLOG_DEBUG_ON/SPDLOG_TRACE_ON in tweakme.h to enable. -// SPDLOG_TRACE(..) will also print current file and line. -// -// Example: -// spdlog::set_level(spdlog::level::trace); -// SPDLOG_TRACE(my_logger, "another trace message {} {}", 1, 2); -// +// compile time level. defaults to info -#ifdef SPDLOG_TRACE_ON -#define SPDLOG_STR_H(x) #x -#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__) +#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE +#define SPDLOG_LOGGER_TRACE(logger, ...) logger->trace(__VA_ARGS__) +#define SPDLOG_TRACE(...) spdlog::trace(__VA_ARGS__) #else -#define SPDLOG_TRACE(logger, ...) \ - logger->trace("[" __FILE__ ":" SPDLOG_STR_HELPER(__LINE__) "]" \ - " " __VA_ARGS__) -#endif -#else -#define SPDLOG_TRACE(logger, ...) (void)0 +#define SPDLOG_LOGGER_TRACE(logger, ...) (void)0 +#define SPDLOG_TRACE(...) (void)0 #endif -#ifdef SPDLOG_DEBUG_ON -#define SPDLOG_DEBUG(logger, ...) logger->debug(__VA_ARGS__) +#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG +#define SPDLOG_LOGGER_DEBUG(logger, ...) logger->debug(__VA_ARGS__) +#define SPDLOG_DEBUG(...) spdlog::debug(__VA_ARGS__) #else -#define SPDLOG_DEBUG(logger, ...) (void)0 +#define SPDLOG_LOGGER_DEBUG(logger, ...) (void)0 +#define SPDLOG_DEBUG(...) (void)0 +#endif + +#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_INFO +#define SPDLOG_LOGGER_INFO(logger, ...) logger->info(__VA_ARGS__) +#define SPDLOG_INFO(...) spdlog::info(__VA_ARGS__) +#else +#define SPDLOG_LOGGER_INFO(logger, ...) (void)0 +#define SPDLOG_INFO(...) (void)0 +#endif + +#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_WARN +#define SPDLOG_LOGGER_WARN(logger, ...) logger->warn(__VA_ARGS__) +#define SPDLOG_WARN(...) spdlog::warn(__VA_ARGS__) +#else +#define SPDLOG_LOGGER_WARN(logger, ...) (void)0 +#define SPDLOG_WARN(...) (void)0 +#endif + +#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_ERROR +#define SPDLOG_LOGGER_ERROR(logger, ...) logger->error(__VA_ARGS__) +#define SPDLOG_ERROR(...) spdlog::error(__VA_ARGS__) +#else +#define SPDLOG_LOGGER_ERROR(logger, ...) (void)0 +#define SPDLOG_ERROR(...) (void)0 +#endif + +#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_CRITICAL +#define SPDLOG_LOGGER_CRITICAL(logger, ...) logger->critical(__VA_ARGS__) +#define SPDLOG_CRITICAL(...) spdlog::critical(__VA_ARGS__) +#else +#define SPDLOG_LOGGER_CRITICAL(logger, ...) (void)0 +#define SPDLOG_CRITICAL(...) (void)0 #endif } // namespace spdlog diff --git a/include/spdlog/tweakme.h b/include/spdlog/tweakme.h index d91159f7..0d77dadc 100644 --- a/include/spdlog/tweakme.h +++ b/include/spdlog/tweakme.h @@ -128,3 +128,10 @@ // // #define SPDLOG_DISABLE_DEFAULT_LOGGER /////////////////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////////////////// +// Uncomment and set to compile time level with zero cost (default is INFO). +// Macros like SPDLOG_DEBUG(..), SPDLOG_INFO(..) will expand to empty statements if not enabled +// +// #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO +/////////////////////////////////////////////////////////////////////////////// \ No newline at end of file diff --git a/tests/includes.h b/tests/includes.h index 3379f2c2..54601602 100644 --- a/tests/includes.h +++ b/tests/includes.h @@ -10,8 +10,7 @@ #include #include -#define SPDLOG_TRACE_ON -#define SPDLOG_DEBUG_ON +#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG #define SPDLOG_ENABLE_MESSAGE_COUNTER #include "spdlog/spdlog.h" diff --git a/tests/test_macros.cpp b/tests/test_macros.cpp index 0b3e0b7d..cf2bb136 100644 --- a/tests/test_macros.cpp +++ b/tests/test_macros.cpp @@ -4,8 +4,13 @@ #include "includes.h" +#if SPDLOG_ACTIVE_LEVEL != SPDLOG_LEVEL_DEBUG +#error "Invalid SPDLOG_ACTIVE_LEVEL in test. Should be SPDLOG_LEVEL_DEBUG" +#endif + TEST_CASE("debug and trace w/o format string", "[macros]]") { + prepare_logdir(); std::string filename = "logs/simple_log"; @@ -13,28 +18,15 @@ TEST_CASE("debug and trace w/o format string", "[macros]]") logger->set_pattern("%v"); logger->set_level(spdlog::level::trace); - SPDLOG_TRACE(logger, "Test message 1"); - SPDLOG_DEBUG(logger, "Test message 2"); + SPDLOG_LOGGER_TRACE(logger, "Test message 1"); + SPDLOG_LOGGER_DEBUG(logger, "Test message 2"); logger->flush(); REQUIRE(ends_with(file_contents(filename), "Test message 2\n")); - REQUIRE(count_lines(filename) == 2); + REQUIRE(count_lines(filename) == 1); } -TEST_CASE("debug and trace with format strings", "[macros]]") +TEST_CASE("disable param evaluation", "[macros]") { - prepare_logdir(); - std::string filename = "logs/simple_log"; - - auto logger = spdlog::create("logger", filename); - logger->set_pattern("%v"); - logger->set_level(spdlog::level::trace); - - SPDLOG_TRACE(logger, "Test message {}", 1); - // SPDLOG_DEBUG(logger, "Test message 2"); - SPDLOG_DEBUG(logger, "Test message {}", 222); - logger->flush(); - - REQUIRE(ends_with(file_contents(filename), "Test message 222\n")); - REQUIRE(count_lines(filename) == 2); + SPDLOG_TRACE("Test message {}", throw std::runtime_error("Should not be evaluated")); }