mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-24 09:31:34 +08:00
New compile time API using SPDLOG_ACTIVE_LEVEL
This commit is contained in:
parent
f1e79bde2e
commit
0e77c3391b
@ -69,17 +69,29 @@ using level_t = details::null_atomic_int;
|
||||
using level_t = std::atomic<int>;
|
||||
#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)
|
||||
|
@ -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<typename It>
|
||||
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
|
||||
|
@ -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
|
||||
|
@ -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<level::level_enum>(SPDLOG_ACTIVE_LEVEL)};
|
||||
const std::string name_;
|
||||
|
||||
std::vector<sink_ptr> sinks_;
|
||||
spdlog::level_t level_;
|
||||
spdlog::level_t flush_level_;
|
||||
log_err_handler err_handler_;
|
||||
std::atomic<time_t> last_err_time_;
|
||||
std::atomic<size_t> msg_counter_;
|
||||
spdlog::level_t level_{static_cast<level::level_enum>(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<time_t> last_err_time_{0};
|
||||
std::atomic<size_t> msg_counter_{1};
|
||||
};
|
||||
} // namespace spdlog
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
///////////////////////////////////////////////////////////////////////////////
|
@ -10,8 +10,7 @@
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
#define SPDLOG_TRACE_ON
|
||||
#define SPDLOG_DEBUG_ON
|
||||
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
|
||||
#define SPDLOG_ENABLE_MESSAGE_COUNTER
|
||||
|
||||
#include "spdlog/spdlog.h"
|
||||
|
@ -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<spdlog::sinks::basic_file_sink_mt>("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"));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user