From c5069135d7c04ba0a5241888f2ec51f236ccd6aa Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 12 Nov 2018 12:01:10 +0200 Subject: [PATCH] Improved compile time level support --- bench/latency.cpp | 4 ++-- example/example.cpp | 3 --- include/spdlog/details/logger_impl.h | 5 +++++ include/spdlog/details/registry.h | 2 +- include/spdlog/logger.h | 6 +++--- include/spdlog/spdlog.h | 5 ++++- 6 files changed, 15 insertions(+), 10 deletions(-) diff --git a/bench/latency.cpp b/bench/latency.cpp index 78ee1786..25b99f5c 100644 --- a/bench/latency.cpp +++ b/bench/latency.cpp @@ -62,8 +62,8 @@ void bench_logger(benchmark::State &state, std::shared_ptr logge void bench_disabled_macro(benchmark::State &state, std::shared_ptr logger) { int i = 0; - benchmark::DoNotOptimize(i); //prevent unused warnings - benchmark::DoNotOptimize(logger); //prevent unused warnings + benchmark::DoNotOptimize(i); // prevent unused warnings + benchmark::DoNotOptimize(logger); // prevent unused warnings for (auto _ : state) { SPDLOG_LOGGER_DEBUG(logger, "Hello logger: msg number {}...............", i++); diff --git a/example/example.cpp b/example/example.cpp index b54d0a73..8a103fc3 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -22,9 +22,6 @@ void err_handler_example(); void syslog_example(); void clone_example(); -// activate compile time macros to be debug or above -#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG - #include "spdlog/spdlog.h" int main(int, char *[]) diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index 6338586c..efe6a66a 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -327,6 +327,11 @@ inline bool spdlog::logger::should_flush_(const details::log_msg &msg) return (msg.level >= flush_level) && (msg.level != level::off); } +inline spdlog::level::level_enum spdlog::logger::default_level() +{ + return static_cast(SPDLOG_ACTIVE_LEVEL); +} + inline spdlog::level::level_enum spdlog::logger::level() const { return static_cast(level_.load(std::memory_order_relaxed)); diff --git a/include/spdlog/details/registry.h b/include/spdlog/details/registry.h index bc4565f5..ccd53955 100644 --- a/include/spdlog/details/registry.h +++ b/include/spdlog/details/registry.h @@ -272,7 +272,7 @@ private: std::recursive_mutex tp_mutex_; std::unordered_map> loggers_; std::unique_ptr formatter_; - level::level_enum level_ = level::info; + level::level_enum level_ = spdlog::logger::default_level(); level::level_enum flush_level_ = level::off; log_err_handler err_handler_; std::shared_ptr tp_; diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 748529cb..a1757574 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -121,6 +121,8 @@ public: bool should_log(level::level_enum msg_level) const; void set_level(level::level_enum log_level); + + static level::level_enum default_level(); level::level_enum level() const; const std::string &name() const; @@ -158,11 +160,9 @@ 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_{static_cast(SPDLOG_ACTIVE_LEVEL)}; + spdlog::level_t level_{spdlog::logger::default_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}; diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 1f3e26ba..200cbd1d 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -290,7 +290,10 @@ inline void critical(const wchar_t *fmt, const Args &... args) #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT -// compile time level. defaults to info +// +// compile time macros. +// can be enabled/disabled using SPDLOG_ACTIVE_LEVEL (info by default). +// #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE #define SPDLOG_LOGGER_TRACE(logger, ...) logger->trace(__VA_ARGS__)