2014-03-22 20:11:17 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// Thread safe logger
|
2014-10-11 02:17:26 +08:00
|
|
|
// Has name, log level, vector of std::shared sink pointers and formatter
|
|
|
|
// Upon each log write the logger:
|
|
|
|
// 1. Checks if its log level is enough to log the message
|
|
|
|
// 2. Format the message using the formatter function
|
|
|
|
// 3. Pass the formatted message to it sinks to performa the actual logging
|
|
|
|
|
2014-03-22 20:11:17 +08:00
|
|
|
#include<vector>
|
|
|
|
#include<memory>
|
|
|
|
#include<atomic>
|
2014-05-08 07:23:07 +08:00
|
|
|
#include <sstream>
|
2014-09-02 05:24:35 +08:00
|
|
|
|
2014-03-22 20:11:17 +08:00
|
|
|
#include "sinks/base_sink.h"
|
2014-10-10 08:36:50 +08:00
|
|
|
#include "common.h"
|
2014-10-14 09:00:56 +08:00
|
|
|
#include "pattern_formatter.h"
|
2014-09-02 05:24:35 +08:00
|
|
|
|
2014-03-22 20:11:17 +08:00
|
|
|
namespace c11log
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace details
|
|
|
|
{
|
|
|
|
class line_logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
class logger
|
|
|
|
{
|
|
|
|
public:
|
2014-10-10 07:46:03 +08:00
|
|
|
using sink_ptr = std::shared_ptr<sinks::isink>;
|
2014-03-22 20:11:17 +08:00
|
|
|
using sinks_vector_t = std::vector<sink_ptr>;
|
2014-05-09 20:27:06 +08:00
|
|
|
using sinks_init_list = std::initializer_list<sink_ptr>;
|
2014-10-14 09:00:56 +08:00
|
|
|
using formatter_ptr = std::unique_ptr<formatter>;
|
2014-03-22 20:11:17 +08:00
|
|
|
|
2014-03-29 06:20:51 +08:00
|
|
|
logger(const std::string& name, sinks_init_list, formatter_ptr = nullptr);
|
2014-10-12 09:38:06 +08:00
|
|
|
template<class It>
|
|
|
|
logger(const std::string& name, It begin, It end, formatter_ptr = nullptr);
|
2014-03-29 06:38:05 +08:00
|
|
|
logger(const std::string& name, sink_ptr, formatter_ptr = nullptr);
|
2014-10-12 09:38:06 +08:00
|
|
|
~logger() = default;
|
|
|
|
|
2014-03-22 20:11:17 +08:00
|
|
|
logger(const logger&) = delete;
|
|
|
|
logger& operator=(const logger&) = delete;
|
|
|
|
|
2014-10-11 02:32:10 +08:00
|
|
|
void level(level::level_enum);
|
|
|
|
level::level_enum level() const;
|
2014-10-11 02:17:26 +08:00
|
|
|
|
|
|
|
const std::string& name() const;
|
2014-10-11 02:32:10 +08:00
|
|
|
bool should_log(level::level_enum) const;
|
2014-03-31 07:29:57 +08:00
|
|
|
|
2014-10-12 09:38:06 +08:00
|
|
|
template <typename... Args> details::line_logger log(level::level_enum lvl, const Args&... args);
|
|
|
|
|
2014-03-31 07:31:26 +08:00
|
|
|
template<typename T> details::line_logger trace(const T&);
|
|
|
|
template<typename T> details::line_logger debug(const T&);
|
2014-10-12 09:38:06 +08:00
|
|
|
template <typename... Args> details::line_logger info(const Args&... args);
|
|
|
|
|
2014-03-31 07:31:26 +08:00
|
|
|
template<typename T> details::line_logger warn(const T&);
|
|
|
|
template<typename T> details::line_logger error(const T&);
|
|
|
|
template<typename T> details::line_logger critical(const T&);
|
2014-03-31 07:29:57 +08:00
|
|
|
|
2014-03-31 07:31:26 +08:00
|
|
|
details::line_logger trace();
|
|
|
|
details::line_logger debug();
|
|
|
|
details::line_logger warn();
|
2014-10-12 09:38:06 +08:00
|
|
|
details::line_logger& error();
|
2014-03-31 07:31:26 +08:00
|
|
|
details::line_logger critical();
|
2014-10-12 09:38:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-03-31 07:24:11 +08:00
|
|
|
|
2014-03-22 20:11:17 +08:00
|
|
|
private:
|
|
|
|
friend details::line_logger;
|
2014-10-11 02:32:10 +08:00
|
|
|
std::string _name;
|
2014-03-22 20:11:17 +08:00
|
|
|
formatter_ptr _formatter;
|
|
|
|
sinks_vector_t _sinks;
|
2014-10-11 02:17:26 +08:00
|
|
|
std::atomic_int _level;
|
2014-03-22 20:11:17 +08:00
|
|
|
|
2014-10-12 09:38:06 +08:00
|
|
|
void _variadic_log(details::line_logger& l);
|
|
|
|
template <typename First, typename... Rest>
|
|
|
|
void _variadic_log(details::line_logger&l, const First& first, const Rest&... rest);
|
|
|
|
void _log_msg(details::log_msg& msg);
|
2014-03-31 07:09:13 +08:00
|
|
|
|
2014-03-22 20:11:17 +08:00
|
|
|
|
2014-10-12 09:38:06 +08:00
|
|
|
};
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
|
|
|
|
2014-10-12 09:38:06 +08:00
|
|
|
//
|
|
|
|
// trace & debug macros
|
|
|
|
//
|
|
|
|
#ifdef FFLOG_ENABLE_TRACE
|
|
|
|
#define FFLOG_TRACE(logger, ...) logger->log(c11log::level::TRACE, __FILE__, " #", __LINE__,": " __VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define FFLOG_TRACE(logger, ...) {}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef FFLOG_ENABLE_DEBUG
|
|
|
|
#define FFLOG_DEBUG(logger, ...) logger->log(c11log::level::DEBUG, __VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define FFLOG_DEBUG(logger, ...) {}
|
|
|
|
#endif
|
|
|
|
|
2014-03-22 20:11:17 +08:00
|
|
|
//
|
2014-10-10 07:46:03 +08:00
|
|
|
// Logger implementation
|
2014-03-22 20:11:17 +08:00
|
|
|
//
|
2014-09-02 05:24:35 +08:00
|
|
|
|
2014-03-22 20:11:17 +08:00
|
|
|
#include "details/line_logger.h"
|
|
|
|
|
|
|
|
|
2014-03-29 06:20:51 +08:00
|
|
|
inline c11log::logger::logger(const std::string& name, sinks_init_list sinks_list, formatter_ptr f) :
|
2014-10-11 02:32:10 +08:00
|
|
|
_name(name),
|
2014-05-13 06:41:04 +08:00
|
|
|
_formatter(std::move(f)),
|
2014-03-22 20:11:17 +08:00
|
|
|
_sinks(sinks_list)
|
|
|
|
{
|
2014-10-10 07:46:03 +08:00
|
|
|
//Seems that vs2013 doesn't support std::atomic member initialization yet
|
2014-10-11 02:17:26 +08:00
|
|
|
_level = level::INFO;
|
2014-03-29 06:38:05 +08:00
|
|
|
if(!_formatter)
|
2014-10-14 09:00:56 +08:00
|
|
|
_formatter = std::make_unique<pattern_formatter>("%t");
|
2014-05-13 06:41:04 +08:00
|
|
|
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
|
|
|
|
2014-10-12 09:38:06 +08:00
|
|
|
template<class It>
|
|
|
|
inline c11log::logger::logger(const std::string& name, It begin, It end, formatter_ptr f):
|
|
|
|
_name(name),
|
|
|
|
_formatter(std::move(f)),
|
|
|
|
_sinks(begin, end)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-22 20:11:17 +08:00
|
|
|
|
2014-03-29 06:20:51 +08:00
|
|
|
inline c11log::logger::logger(const std::string& name, sink_ptr sink, formatter_ptr f) :
|
2014-05-13 06:41:04 +08:00
|
|
|
logger(name, {sink}, std::move(f)) {}
|
2014-03-22 20:11:17 +08:00
|
|
|
|
2014-03-29 07:25:52 +08:00
|
|
|
|
2014-03-31 07:29:57 +08:00
|
|
|
template<typename T>
|
|
|
|
inline c11log::details::line_logger c11log::logger::trace(const T& msg)
|
|
|
|
{
|
2014-03-31 07:31:26 +08:00
|
|
|
details::line_logger l(this, level::TRACE, should_log(level::TRACE));
|
|
|
|
l.write(msg);
|
|
|
|
return l;
|
2014-03-31 07:29:57 +08:00
|
|
|
}
|
|
|
|
|
2014-03-31 07:09:13 +08:00
|
|
|
template<typename T>
|
|
|
|
inline c11log::details::line_logger c11log::logger::debug(const T& msg)
|
2014-03-22 20:11:17 +08:00
|
|
|
{
|
2014-10-12 09:38:06 +08:00
|
|
|
c11log::details::line_logger l(this, level::DEBUG, should_log(level::DEBUG));
|
2014-03-31 07:31:26 +08:00
|
|
|
l.write(msg);
|
|
|
|
return l;
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
2014-03-31 07:09:13 +08:00
|
|
|
|
2014-10-12 09:38:06 +08:00
|
|
|
template <typename... Args>
|
|
|
|
inline c11log::details::line_logger c11log::logger::log(level::level_enum lvl, const Args&... args) {
|
|
|
|
details::line_logger l(this, lvl, true);
|
|
|
|
_variadic_log(l, args...);
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
inline c11log::details::line_logger c11log::logger::info(const Args&... args) {
|
2014-03-31 07:31:26 +08:00
|
|
|
details::line_logger l(this, level::INFO, should_log(level::INFO));
|
2014-10-12 09:38:06 +08:00
|
|
|
_variadic_log(l, args...);
|
2014-03-31 07:31:26 +08:00
|
|
|
return l;
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
2014-03-31 07:09:13 +08:00
|
|
|
|
2014-10-12 09:38:06 +08:00
|
|
|
|
2014-03-31 07:09:13 +08:00
|
|
|
template<typename T>
|
|
|
|
inline c11log::details::line_logger c11log::logger::warn(const T& msg)
|
2014-03-22 20:11:17 +08:00
|
|
|
{
|
2014-03-31 07:31:26 +08:00
|
|
|
details::line_logger l(this, level::WARNING, should_log(level::WARNING));
|
|
|
|
l.write(msg);
|
|
|
|
return l;
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
2014-03-29 05:27:13 +08:00
|
|
|
|
2014-03-31 07:09:13 +08:00
|
|
|
template<typename T>
|
|
|
|
inline c11log::details::line_logger c11log::logger::critical(const T& msg)
|
2014-03-22 20:11:17 +08:00
|
|
|
{
|
2014-03-31 07:31:26 +08:00
|
|
|
details::line_logger l(this, level::CRITICAL, should_log(level::CRITICAL));
|
|
|
|
l.write(msg);
|
|
|
|
return l;
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
2014-03-29 05:27:13 +08:00
|
|
|
|
2014-10-12 09:38:06 +08:00
|
|
|
|
2014-03-22 20:11:17 +08:00
|
|
|
|
2014-03-31 07:29:57 +08:00
|
|
|
|
|
|
|
inline c11log::details::line_logger c11log::logger::trace()
|
|
|
|
{
|
2014-03-31 07:31:26 +08:00
|
|
|
return details::line_logger(this, level::TRACE, should_log(level::TRACE));
|
2014-03-31 07:29:57 +08:00
|
|
|
}
|
|
|
|
|
2014-03-31 07:24:11 +08:00
|
|
|
inline c11log::details::line_logger c11log::logger::debug()
|
|
|
|
{
|
2014-03-31 07:31:26 +08:00
|
|
|
return details::line_logger(this, level::DEBUG, should_log(level::DEBUG));
|
2014-03-31 07:24:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline c11log::details::line_logger c11log::logger::warn()
|
|
|
|
{
|
2014-03-31 07:31:26 +08:00
|
|
|
return details::line_logger(this, level::WARNING, should_log(level::WARNING));
|
2014-03-31 07:24:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline c11log::details::line_logger c11log::logger::critical()
|
|
|
|
{
|
2014-03-31 07:31:26 +08:00
|
|
|
return details::line_logger(this, level::CRITICAL, should_log(level::CRITICAL));
|
2014-03-31 07:24:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-11 02:17:26 +08:00
|
|
|
inline const std::string& c11log::logger::name() const
|
2014-03-22 20:11:17 +08:00
|
|
|
{
|
2014-10-11 02:32:10 +08:00
|
|
|
return _name;
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
|
|
|
|
2014-10-11 02:17:26 +08:00
|
|
|
inline void c11log::logger::level(c11log::level::level_enum level)
|
2014-03-22 20:11:17 +08:00
|
|
|
{
|
2014-10-11 02:17:26 +08:00
|
|
|
_level.store(level);
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
|
|
|
|
2014-10-11 02:17:26 +08:00
|
|
|
inline c11log::level::level_enum c11log::logger::level() const
|
2014-03-22 20:11:17 +08:00
|
|
|
{
|
2014-10-11 02:17:26 +08:00
|
|
|
return static_cast<c11log::level::level_enum>(_level.load());
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool c11log::logger::should_log(c11log::level::level_enum level) const
|
|
|
|
{
|
2014-10-11 02:17:26 +08:00
|
|
|
return level >= _level.load();
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
|
|
|
|
2014-10-12 09:38:06 +08:00
|
|
|
|
|
|
|
inline void c11log::logger::_variadic_log(c11log::details::line_logger& l) {}
|
|
|
|
|
|
|
|
template <typename First, typename... Rest>
|
|
|
|
void c11log::logger::_variadic_log(c11log::details::line_logger& l, const First& first, const Rest&... rest)
|
|
|
|
{
|
|
|
|
l.write(first);
|
|
|
|
_variadic_log(l, rest...);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void c11log::logger::_log_msg(details::log_msg& msg)
|
2014-03-22 20:11:17 +08:00
|
|
|
{
|
2014-05-09 23:00:10 +08:00
|
|
|
_formatter->format(msg);
|
2014-03-22 20:11:17 +08:00
|
|
|
for (auto &sink : _sinks)
|
2014-03-28 21:05:09 +08:00
|
|
|
sink->log(msg);
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
|
|
|
|
2014-09-02 05:24:35 +08:00
|
|
|
|