Improved compile time level support

This commit is contained in:
gabime 2018-11-12 12:01:10 +02:00
parent 971c1f46b0
commit c5069135d7
6 changed files with 15 additions and 10 deletions

View File

@ -62,8 +62,8 @@ void bench_logger(benchmark::State &state, std::shared_ptr<spdlog::logger> logge
void bench_disabled_macro(benchmark::State &state, std::shared_ptr<spdlog::logger> logger) void bench_disabled_macro(benchmark::State &state, std::shared_ptr<spdlog::logger> logger)
{ {
int i = 0; int i = 0;
benchmark::DoNotOptimize(i); //prevent unused warnings benchmark::DoNotOptimize(i); // prevent unused warnings
benchmark::DoNotOptimize(logger); //prevent unused warnings benchmark::DoNotOptimize(logger); // prevent unused warnings
for (auto _ : state) for (auto _ : state)
{ {
SPDLOG_LOGGER_DEBUG(logger, "Hello logger: msg number {}...............", i++); SPDLOG_LOGGER_DEBUG(logger, "Hello logger: msg number {}...............", i++);

View File

@ -22,9 +22,6 @@ void err_handler_example();
void syslog_example(); void syslog_example();
void clone_example(); void clone_example();
// activate compile time macros to be debug or above
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
int main(int, char *[]) int main(int, char *[])

View File

@ -327,6 +327,11 @@ inline bool spdlog::logger::should_flush_(const details::log_msg &msg)
return (msg.level >= flush_level) && (msg.level != level::off); return (msg.level >= flush_level) && (msg.level != level::off);
} }
inline spdlog::level::level_enum spdlog::logger::default_level()
{
return static_cast<spdlog::level::level_enum>(SPDLOG_ACTIVE_LEVEL);
}
inline spdlog::level::level_enum spdlog::logger::level() const inline spdlog::level::level_enum spdlog::logger::level() const
{ {
return static_cast<spdlog::level::level_enum>(level_.load(std::memory_order_relaxed)); return static_cast<spdlog::level::level_enum>(level_.load(std::memory_order_relaxed));

View File

@ -272,7 +272,7 @@ private:
std::recursive_mutex tp_mutex_; std::recursive_mutex tp_mutex_;
std::unordered_map<std::string, std::shared_ptr<logger>> loggers_; std::unordered_map<std::string, std::shared_ptr<logger>> loggers_;
std::unique_ptr<formatter> formatter_; std::unique_ptr<formatter> formatter_;
level::level_enum level_ = level::info; level::level_enum level_ = spdlog::logger::default_level();
level::level_enum flush_level_ = level::off; level::level_enum flush_level_ = level::off;
log_err_handler err_handler_; log_err_handler err_handler_;
std::shared_ptr<thread_pool> tp_; std::shared_ptr<thread_pool> tp_;

View File

@ -121,6 +121,8 @@ public:
bool should_log(level::level_enum msg_level) const; bool should_log(level::level_enum msg_level) const;
void set_level(level::level_enum log_level); void set_level(level::level_enum log_level);
static level::level_enum default_level();
level::level_enum level() const; level::level_enum level() const;
const std::string &name() const; const std::string &name() const;
@ -158,11 +160,9 @@ protected:
// increment the message count (only if defined(SPDLOG_ENABLE_MESSAGE_COUNTER)) // increment the message count (only if defined(SPDLOG_ENABLE_MESSAGE_COUNTER))
void incr_msg_counter_(details::log_msg &msg); void incr_msg_counter_(details::log_msg &msg);
// const spdlog::level_t default_level_{static_cast<level::level_enum>(SPDLOG_ACTIVE_LEVEL)};
const std::string name_; const std::string name_;
std::vector<sink_ptr> sinks_; std::vector<sink_ptr> sinks_;
spdlog::level_t level_{static_cast<level::level_enum>(SPDLOG_ACTIVE_LEVEL)}; spdlog::level_t level_{spdlog::logger::default_level()};
spdlog::level_t flush_level_{level::off}; spdlog::level_t flush_level_{level::off};
log_err_handler err_handler_{[this](const std::string &msg) { this->default_err_handler_(msg); }}; log_err_handler err_handler_{[this](const std::string &msg) { this->default_err_handler_(msg); }};
std::atomic<time_t> last_err_time_{0}; std::atomic<time_t> last_err_time_{0};

View File

@ -290,7 +290,10 @@ inline void critical(const wchar_t *fmt, const Args &... args)
#endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT #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 #if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_TRACE
#define SPDLOG_LOGGER_TRACE(logger, ...) logger->trace(__VA_ARGS__) #define SPDLOG_LOGGER_TRACE(logger, ...) logger->trace(__VA_ARGS__)