From 4fd1ac8ac50122aa25d19c865ed8b7853a567be0 Mon Sep 17 00:00:00 2001 From: gabi Date: Mon, 5 Jan 2015 23:55:05 +0200 Subject: [PATCH] Added support for logger->info(const T&) call style --- include/spdlog/details/logger_impl.h | 76 +++++++++++++++++++++++++--- include/spdlog/logger.h | 52 +++++++++---------- 2 files changed, 96 insertions(+), 32 deletions(-) diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index 12f8dd36..e624dbb9 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -85,9 +85,17 @@ inline spdlog::details::line_logger spdlog::logger::_log_if_enabled(level::level return details::line_logger(this, lvl, should_log(lvl)); } +template +inline spdlog::details::line_logger spdlog::logger::_log_if_enabled(level::level_enum lvl, const T& msg) +{ + bool msg_enabled = should_log(lvl); + details::line_logger l(this, lvl, msg_enabled); + l << msg; + return l; +} // -// following functions will log only if at the right level +// logger.info(cppformat_string, arg1, arg2, arg3, ...) call style // template inline spdlog::details::line_logger spdlog::logger::trace(const char* fmt, const Args&... args) @@ -143,19 +151,75 @@ inline spdlog::details::line_logger spdlog::logger::emerg(const char* fmt, const return _log_if_enabled(level::emerg, fmt, args...); } +// +// logger.info(msg) << ".." call style +// +template +inline spdlog::details::line_logger spdlog::logger::trace(const T& msg) +{ + return _log_if_enabled(level::trace, msg); +} + +template +inline spdlog::details::line_logger spdlog::logger::debug(const T& msg) +{ + return _log_if_enabled(level::debug, msg); +} + + +template +inline spdlog::details::line_logger spdlog::logger::info(const T& msg) +{ + return _log_if_enabled(level::info, msg); +} + +template +inline spdlog::details::line_logger spdlog::logger::notice(const T& msg) +{ + return _log_if_enabled(level::notice, msg); +} + +template +inline spdlog::details::line_logger spdlog::logger::warn(const T& msg) +{ + return _log_if_enabled(level::warn, msg); +} + +template +inline spdlog::details::line_logger spdlog::logger::error(const T& msg) +{ + return _log_if_enabled(level::err, msg); +} + +template +inline spdlog::details::line_logger spdlog::logger::critical(const T& msg) +{ + return _log_if_enabled(level::critical, msg); +} + +template +inline spdlog::details::line_logger spdlog::logger::alert(const T& msg) +{ + return _log_if_enabled(level::alert, msg); +} + +template +inline spdlog::details::line_logger spdlog::logger::emerg(const T& msg) +{ + return _log_if_enabled(level::emerg, msg); +} + + + // -// support logger.info() << ".." calls +// logger.info() << ".." call style // - - - inline spdlog::details::line_logger spdlog::logger::trace() { return _log_if_enabled(level::trace); } - inline spdlog::details::line_logger spdlog::logger::debug() { return _log_if_enabled(level::debug); diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 4fa39a54..cf97fc80 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -62,34 +62,31 @@ public: const std::string& name() const; bool should_log(level::level_enum) const; - 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); + // logger.info(cppformat_string, arg1, arg2, arg3, ...) call style + 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() << ".." call style + // logger.info(msg) << ".." call style + template details::line_logger trace(const T&); + template details::line_logger debug(const T&); + template details::line_logger info(const T&); + template details::line_logger notice(const T&); + template details::line_logger warn(const T&); + template details::line_logger error(const T&); + template details::line_logger critical(const T&); + template details::line_logger alert(const T&); + template details::line_logger emerg(const T&); + + + // logger.info() << ".." call style details::line_logger trace(); details::line_logger debug(); details::line_logger info(); @@ -101,6 +98,7 @@ 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); @@ -117,6 +115,8 @@ protected: 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); + template + inline details::line_logger _log_if_enabled(level::level_enum lvl, const T& msg); friend details::line_logger;