mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-25 01:51:38 +08:00
Added debug macros
This commit is contained in:
parent
3e51669951
commit
348390f90d
@ -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..
|
||||
|
@ -69,7 +69,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 easy..
|
||||
|
@ -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];
|
||||
|
@ -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<class It>
|
||||
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 <typename... Args>
|
||||
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 <typename... Args>
|
||||
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 <typename... Args>
|
||||
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 <typename... Args>
|
||||
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 <typename... Args>
|
||||
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 <typename... Args>
|
||||
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 <typename... Args>
|
||||
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 <typename... Args>
|
||||
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 <typename... Args>
|
||||
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 <typename... Args>
|
||||
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 <typename... Args>
|
||||
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 <typename... Args>
|
||||
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
|
||||
|
@ -65,20 +65,35 @@ public:
|
||||
//Stop logging
|
||||
void stop();
|
||||
|
||||
template <typename... Args> details::line_logger log(const char* fmt, const Args&... args);
|
||||
template <typename... Args> details::line_logger trace(const char* fmt, const Args&... args);
|
||||
template <typename... Args> details::line_logger debug(const char* fmt, const Args&... args);
|
||||
template <typename... Args> details::line_logger info(const char* fmt, const Args&... args);
|
||||
template <typename... Args> details::line_logger notice(const char* fmt, const Args&... args);
|
||||
template <typename... Args> details::line_logger warn(const char* fmt, const Args&... args);
|
||||
template <typename... Args> details::line_logger error(const char* fmt, const Args&... args);
|
||||
template <typename... Args> details::line_logger critical(const char* fmt, const Args&... args);
|
||||
template <typename... Args> details::line_logger alert(const char* fmt, const Args&... args);
|
||||
template <typename... Args> details::line_logger emerg(const char* fmt, const Args&... args);
|
||||
|
||||
//API to support logger.info() << ".." calls
|
||||
template <typename... Args>
|
||||
details::line_logger trace(const char* fmt, const Args&... args);
|
||||
|
||||
details::line_logger log();
|
||||
template <typename... Args>
|
||||
details::line_logger debug(const char* fmt, const Args&... args);
|
||||
|
||||
template <typename... Args>
|
||||
details::line_logger info(const char* fmt, const Args&... args);
|
||||
|
||||
template <typename... Args>
|
||||
details::line_logger notice(const char* fmt, const Args&... args);
|
||||
|
||||
template <typename... Args>
|
||||
details::line_logger warn(const char* fmt, const Args&... args);
|
||||
|
||||
template <typename... Args>details::line_logger error(const char* fmt, const Args&... args);
|
||||
|
||||
template <typename... Args>
|
||||
details::line_logger critical(const char* fmt, const Args&... args);
|
||||
|
||||
template <typename... Args>
|
||||
details::line_logger alert(const char* fmt, const Args&... args);
|
||||
|
||||
template <typename... Args>
|
||||
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 <typename... Args>
|
||||
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 <typename... Args> 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 <typename... Args>
|
||||
details::line_logger _log_if_enabled(level::level_enum lvl, const char* fmt, const Args&... args);
|
||||
|
||||
|
||||
friend details::line_logger;
|
||||
|
@ -114,23 +114,35 @@ template <typename Sink, typename... Args>
|
||||
std::shared_ptr<spdlog::logger> 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"
|
||||
|
Loading…
Reference in New Issue
Block a user