diff --git a/README.md b/README.md index a6174bfb..2d77a0f4 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,8 @@ int main(int, char* []) spd::get("console")->info("loggers can be retrieved from a global registry using the spdlog::get(logger_name) function"); - SPDLOG_TRACE(file_logger, "This is a trace message (only #ifdef _DEBUG)", 123); + SPDLOG_TRACE(console, "Trace message - enabled only #ifdef SPDLOG_TRACE_ON..{} ,{}", 1, 3.23); + SPDLOG_DEBUG(console, "Debug message - enabled only #ifdef SPDLOG_DEBUG_ON.. {} ,{}", 1, 3.23); // // Asynchronous logging is very fast.. diff --git a/example/example.cpp b/example/example.cpp index 0cfa326f..9a631419 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -69,14 +69,15 @@ int main(int, char* []) spd::get("console")->info("loggers can be retrieved from a global registry using the spdlog::get(logger_name) function"); - SPDLOG_TRACE(file_logger, "This is a trace message (only #ifdef _DEBUG)", 123); + SPDLOG_TRACE(console, "Trace message - enabled only #ifdef SPDLOG_TRACE_ON..{} ,{}", 1, 3.23); + SPDLOG_DEBUG(console, "Debug message - enabled only #ifdef SPDLOG_DEBUG_ON.. {} ,{}", 1, 3.23); // // Asynchronous logging is easy.. // Just call spdlog::set_async_mode(q_size) and all created loggers from now on will be asynchronous.. // Note: queue size must be power of 2! // - size_t max_q_size = 1048576; + size_t max_q_size = 1048576; spdlog::set_async_mode(max_q_size); auto async_file= spd::daily_logger_st("async_file_logger", "logs/async_log.txt"); async_file->info() << "This is async log.." << "Should be very fast!"; diff --git a/include/spdlog/common.h b/include/spdlog/common.h index 1be17d0d..0569f4b3 100644 --- a/include/spdlog/common.h +++ b/include/spdlog/common.h @@ -55,13 +55,11 @@ typedef enum CRITICAL = 6, ALERT = 7, EMERG = 8, - ALWAYS = 9, - OFF = 10 + OFF = 9 } level_enum; -static const char* level_names[] { "trace", "debug", "info", "notice", "warning", "error", "critical", - "alert", "emerg", "", "" - }; +static const char* level_names[] { "trace", "debug", "info", "notice", "warning", "error", "critical", "alert", "emerg", "off"}; + inline const char* to_str(spdlog::level::level_enum l) { return level_names[l]; diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index 3f2360b0..11afa1f6 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -30,7 +30,8 @@ #include "./line_logger.h" -/* public functions */ +// create logger with given name, sinks and the default pattern formatter +// all other ctors will call this one template inline spdlog::logger::logger(const std::string& logger_name, const It& begin, const It& end) : _name(logger_name), @@ -42,16 +43,18 @@ inline spdlog::logger::logger(const std::string& logger_name, const It& begin, c _level = level::INFO; } +// ctor with sinks as init list inline spdlog::logger::logger(const std::string& logger_name, sinks_init_list sinks_list) : logger(logger_name, sinks_list.begin(), sinks_list.end()) {} - -inline spdlog::logger::logger(const std::string& logger_name, spdlog::sink_ptr single_sink) :logger(logger_name, { single_sink }) -{} +// ctor with single sink +inline spdlog::logger::logger(const std::string& logger_name, spdlog::sink_ptr single_sink) : + logger(logger_name, { single_sink }) {} -inline spdlog::logger::~logger() {} +inline spdlog::logger::~logger() = default; + inline void spdlog::logger::set_formatter(spdlog::formatter_ptr msg_formatter) { @@ -64,10 +67,12 @@ inline void spdlog::logger::set_pattern(const std::string& pattern) } // -// cppformat API of the form logger.info("hello {} {}", "world", 1); +// log only if given level>=logger's log level // + + template -inline spdlog::details::line_logger spdlog::logger::_log(level::level_enum lvl, const char* fmt, const Args&... args) +inline spdlog::details::line_logger spdlog::logger::_log_if_enabled(level::level_enum lvl, const char* fmt, const Args&... args) { bool msg_enabled = should_log(lvl); details::line_logger l(this, lvl, msg_enabled); @@ -75,130 +80,131 @@ inline spdlog::details::line_logger spdlog::logger::_log(level::level_enum lvl, return l; } -template -inline spdlog::details::line_logger spdlog::logger::log(const char* fmt, const Args&... args) +inline spdlog::details::line_logger spdlog::logger::_log_if_enabled(level::level_enum lvl) { - return _log(level::ALWAYS, fmt, args...); + return details::line_logger(this, lvl, should_log(lvl)); } + +// +// following functions will log only if at the right level +// template inline spdlog::details::line_logger spdlog::logger::trace(const char* fmt, const Args&... args) { - return _log(level::TRACE, fmt, args...); + return _log_if_enabled(level::TRACE, fmt, args...); } template inline spdlog::details::line_logger spdlog::logger::debug(const char* fmt, const Args&... args) { - return _log(level::DEBUG, fmt, args...); + return _log_if_enabled(level::DEBUG, fmt, args...); } template inline spdlog::details::line_logger spdlog::logger::info(const char* fmt, const Args&... args) { - return _log(level::INFO, fmt, args...); + return _log_if_enabled(level::INFO, fmt, args...); } template inline spdlog::details::line_logger spdlog::logger::notice(const char* fmt, const Args&... args) { - return _log(level::NOTICE, fmt, args...); + return _log_if_enabled(level::NOTICE, fmt, args...); } template inline spdlog::details::line_logger spdlog::logger::warn(const char* fmt, const Args&... args) { - return _log(level::WARN, fmt, args...); + return _log_if_enabled(level::WARN, fmt, args...); } template inline spdlog::details::line_logger spdlog::logger::error(const char* fmt, const Args&... args) { - return _log(level::ERR, fmt, args...); + return _log_if_enabled(level::ERR, fmt, args...); } template inline spdlog::details::line_logger spdlog::logger::critical(const char* fmt, const Args&... args) { - return _log(level::CRITICAL, fmt, args...); + return _log_if_enabled(level::CRITICAL, fmt, args...); } template inline spdlog::details::line_logger spdlog::logger::alert(const char* fmt, const Args&... args) { - return _log(level::ALERT, fmt, args...); + return _log_if_enabled(level::ALERT, fmt, args...); } template inline spdlog::details::line_logger spdlog::logger::emerg(const char* fmt, const Args&... args) { - return _log(level::EMERG, fmt, args...); + return _log_if_enabled(level::EMERG, fmt, args...); } // -// //API to support logger.info() << ".." calls +// support logger.info() << ".." calls // -inline spdlog::details::line_logger spdlog::logger::_log(level::level_enum lvl) -{ - bool msg_enabled = should_log(lvl); - details::line_logger l(this, lvl, msg_enabled); - return l; -} -inline spdlog::details::line_logger spdlog::logger::log() -{ - return _log(level::ALWAYS); -} inline spdlog::details::line_logger spdlog::logger::trace() { - return _log(level::TRACE); + return _log_if_enabled(level::TRACE); } inline spdlog::details::line_logger spdlog::logger::debug() { - return _log(level::DEBUG); + return _log_if_enabled(level::DEBUG); } inline spdlog::details::line_logger spdlog::logger::info() { - return _log(level::INFO); + return _log_if_enabled(level::INFO); } inline spdlog::details::line_logger spdlog::logger::notice() { - return _log(level::NOTICE); + return _log_if_enabled(level::NOTICE); } inline spdlog::details::line_logger spdlog::logger::warn() { - return _log(level::WARN); + return _log_if_enabled(level::WARN); } inline spdlog::details::line_logger spdlog::logger::error() { - return _log(level::ERR); + return _log_if_enabled(level::ERR); } inline spdlog::details::line_logger spdlog::logger::critical() { - return _log(level::CRITICAL); + return _log_if_enabled(level::CRITICAL); } inline spdlog::details::line_logger spdlog::logger::alert() { - return _log(level::ALERT); + return _log_if_enabled(level::ALERT); } inline spdlog::details::line_logger spdlog::logger::emerg() { - return _log(level::EMERG); + return _log_if_enabled(level::EMERG); } +// always log, no matter what is the actual logger's log level +template +inline spdlog::details::line_logger spdlog::logger::force_log(level::level_enum lvl, const char* fmt, const Args&... args) +{ + details::line_logger l(this, lvl, true); + l.write(fmt, args...); + return l; +} // // name and level diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 095c34a9..6137e81f 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -65,20 +65,35 @@ public: //Stop logging void stop(); - template details::line_logger log(const char* fmt, const Args&... args); - template details::line_logger trace(const char* fmt, const Args&... args); - template details::line_logger debug(const char* fmt, const Args&... args); - template details::line_logger info(const char* fmt, const Args&... args); - template details::line_logger notice(const char* fmt, const Args&... args); - template details::line_logger warn(const char* fmt, const Args&... args); - template details::line_logger error(const char* fmt, const Args&... args); - template details::line_logger critical(const char* fmt, const Args&... args); - template details::line_logger alert(const char* fmt, const Args&... args); - template details::line_logger emerg(const char* fmt, const Args&... args); - //API to support logger.info() << ".." calls + template + details::line_logger trace(const char* fmt, const Args&... args); - details::line_logger log(); + template + details::line_logger debug(const char* fmt, const Args&... args); + + template + details::line_logger info(const char* fmt, const Args&... args); + + template + details::line_logger notice(const char* fmt, const Args&... args); + + template + details::line_logger warn(const char* fmt, const Args&... args); + + template details::line_logger error(const char* fmt, const Args&... args); + + template + details::line_logger critical(const char* fmt, const Args&... args); + + template + details::line_logger alert(const char* fmt, const Args&... args); + + template + details::line_logger emerg(const char* fmt, const Args&... args); + + + //API to support logger.info() << ".." call style details::line_logger trace(); details::line_logger debug(); details::line_logger info(); @@ -90,6 +105,11 @@ public: details::line_logger emerg(); + // Create log message with the given level, no matter what is the actual logger's level + template + details::line_logger force_log(level::level_enum lvl, const char* fmt, const Args&... args); + + // Set the format of the log messages from this logger void set_pattern(const std::string&); void set_formatter(formatter_ptr); @@ -99,8 +119,9 @@ protected: virtual void _set_pattern(const std::string&); virtual void _set_formatter(formatter_ptr); virtual void _stop(); - details::line_logger _log(level::level_enum lvl); - template details::line_logger _log(level::level_enum lvl, const char* fmt, const Args&... args); + details::line_logger _log_if_enabled(level::level_enum lvl); + template + details::line_logger _log_if_enabled(level::level_enum lvl, const char* fmt, const Args&... args); friend details::line_logger; diff --git a/include/spdlog/spdlog.h b/include/spdlog/spdlog.h index 4841854e..15180963 100644 --- a/include/spdlog/spdlog.h +++ b/include/spdlog/spdlog.h @@ -114,23 +114,35 @@ template std::shared_ptr create(const std::string& logger_name, const Args&...); - - // Stop logging by setting all the loggers to log level OFF void stop(); // -// Trace macro enabled only at debug compile -// Example: SPDLOG_TRACE(my_logger, "Some trace message"); +// Trace & debug macros to be switched on/off at compile time for zero cost debug statements. +// Note: using these mactors overrides the runtime log threshold of the logger. // -#ifdef _DEBUG -#define SPDLOG_TRACE(logger, ...) logger->log(__FILE__, " #", __LINE__,": " __VA_ARGS__) +// Example: +// +// Enable debug macro, must be defined before including spdlog.h +// #define SPDLOG_DEBUG_ON +// include "spdlog/spdlog.h" +// SPDLOG_DEBUG(my_logger, "Some debug message {} {}", 1, 3.2); +// + +#ifdef SPDLOG_TRACE_ON +#define SPDLOG_TRACE(logger, ...) logger->force_log(level::TRACE, __FILE__, " #", __LINE__,": " __VA_ARGS__) #else -#define SPDLOG_TRACE(logger, ...) {} +#define SPDLOG_TRACE(logger, ...) #endif +#ifdef SPDLOG_DEBUG_ON +#define SPDLOG_DEBUG(logger, ...) logger->force_log(level::DEBUG, __VA_ARGS__) +#else +#define SPDLOG_DEBUG(logger, ...) +#endif + } #include "details/spdlog_impl.h"