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
|
2014-10-15 05:46:14 +08:00
|
|
|
// 3. Pass the formatted message to its sinks to performa the actual logging
|
2014-10-11 02:17:26 +08:00
|
|
|
|
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:56:10 +08:00
|
|
|
#include "formatter.h"
|
|
|
|
#include "details/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-14 09:29:52 +08:00
|
|
|
using sink_ptr = std::shared_ptr<sinks::sink>;
|
2014-10-14 13:00:39 +08:00
|
|
|
using formatter_ptr = std::shared_ptr<formatter>;
|
2014-03-22 20:11:17 +08:00
|
|
|
|
2014-10-14 13:00:39 +08:00
|
|
|
logger(const std::string& name, std::initializer_list<sink_ptr>, formatter_ptr formatter = nullptr);
|
2014-10-12 09:38:06 +08:00
|
|
|
template<class It>
|
2014-10-14 13:00:39 +08:00
|
|
|
logger(const std::string& name, const It& begin, const It& end);
|
2014-10-12 09:38:06 +08:00
|
|
|
|
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-14 13:00:39 +08:00
|
|
|
void formatter(formatter_ptr);
|
|
|
|
formatter_ptr formatter() const;
|
|
|
|
|
2014-10-12 09:38:06 +08:00
|
|
|
|
2014-10-14 09:14:35 +08:00
|
|
|
template <typename... Args> details::line_logger log(level::level_enum lvl, const Args&... args);
|
|
|
|
template <typename... Args> details::line_logger trace(const Args&... args);
|
|
|
|
template <typename... Args> details::line_logger debug(const Args&... args);
|
2014-10-12 09:38:06 +08:00
|
|
|
template <typename... Args> details::line_logger info(const Args&... args);
|
2014-10-14 09:14:35 +08:00
|
|
|
template <typename... Args> details::line_logger warn(const Args&... args);
|
|
|
|
template <typename... Args> details::line_logger error(const Args&... args);
|
|
|
|
template <typename... Args> details::line_logger critical(const Args&... args);
|
2014-10-12 09:38:06 +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;
|
2014-10-14 13:00:39 +08:00
|
|
|
std::vector<sink_ptr> _sinks;
|
2014-10-11 02:17:26 +08:00
|
|
|
std::atomic_int _level;
|
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-10-14 13:00:39 +08:00
|
|
|
formatter_ptr _default_formatter();
|
|
|
|
const char* _default_pattern = "[%Y:%m:%d %H:%M:%S.%e] [%n:%l] %t";
|
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-10-14 13:00:39 +08:00
|
|
|
inline c11log::logger::logger(const std::string& logger_name, std::initializer_list<sink_ptr> sinks_list, formatter_ptr formatter) :
|
2014-10-14 09:56:10 +08:00
|
|
|
_name(logger_name),
|
2014-10-14 13:00:39 +08:00
|
|
|
_formatter(formatter),
|
2014-03-22 20:11:17 +08:00
|
|
|
_sinks(sinks_list)
|
|
|
|
{
|
2014-10-14 13:00:39 +08:00
|
|
|
if (!formatter) //default formatter
|
|
|
|
_formatter = std::make_shared<details::pattern_formatter>(_default_pattern);
|
|
|
|
|
|
|
|
//Seems that vs2013 doesn't support atomic member initialization yet
|
2014-10-11 02:17:26 +08:00
|
|
|
_level = level::INFO;
|
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>
|
2014-10-14 13:00:39 +08:00
|
|
|
inline c11log::logger::logger(const std::string& logger_name, const It& begin, const It& end):
|
2014-10-14 09:56:10 +08:00
|
|
|
_name(logger_name),
|
2014-10-12 09:38:06 +08:00
|
|
|
_sinks(begin, end)
|
2014-10-14 13:00:39 +08:00
|
|
|
{}
|
2014-03-29 07:25:52 +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>
|
2014-10-14 13:00:39 +08:00
|
|
|
inline c11log::details::line_logger c11log::logger::trace(const Args&... args)
|
|
|
|
{
|
2014-10-14 09:14:35 +08:00
|
|
|
return log(level::TRACE, args...);
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
2014-03-31 07:09:13 +08:00
|
|
|
|
2014-10-14 09:14:35 +08:00
|
|
|
template <typename... Args>
|
2014-10-14 13:00:39 +08:00
|
|
|
inline c11log::details::line_logger c11log::logger::debug(const Args&... args)
|
|
|
|
{
|
2014-10-14 09:14:35 +08:00
|
|
|
return log(level::DEBUG, args...);
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
2014-03-29 05:27:13 +08:00
|
|
|
|
2014-10-14 09:14:35 +08:00
|
|
|
template <typename... Args>
|
2014-10-14 13:00:39 +08:00
|
|
|
inline c11log::details::line_logger c11log::logger::info(const Args&... args)
|
|
|
|
{
|
2014-10-14 09:14:35 +08:00
|
|
|
return log(level::INFO, args...);
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
2014-03-29 05:27:13 +08:00
|
|
|
|
2014-10-14 09:14:35 +08:00
|
|
|
template <typename... Args>
|
2014-10-14 13:00:39 +08:00
|
|
|
inline c11log::details::line_logger c11log::logger::warn(const Args&... args)
|
|
|
|
{
|
2014-10-14 09:14:35 +08:00
|
|
|
return log(level::WARNING, args...);
|
2014-03-31 07:24:11 +08:00
|
|
|
}
|
|
|
|
|
2014-10-14 09:14:35 +08:00
|
|
|
template <typename... Args>
|
2014-10-14 13:00:39 +08:00
|
|
|
inline c11log::details::line_logger c11log::logger::critical(const Args&... args)
|
|
|
|
{
|
2014-10-14 09:14:35 +08:00
|
|
|
return log(level::CRITICAL, args...);
|
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-14 09:56:10 +08:00
|
|
|
inline void c11log::logger::level(c11log::level::level_enum log_level)
|
2014-03-22 20:11:17 +08:00
|
|
|
{
|
2014-10-14 09:56:10 +08:00
|
|
|
_level.store(log_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
|
|
|
}
|
|
|
|
|
2014-10-14 09:56:10 +08:00
|
|
|
inline bool c11log::logger::should_log(c11log::level::level_enum msg_level) const
|
2014-03-22 20:11:17 +08:00
|
|
|
{
|
2014-10-14 09:56:10 +08:00
|
|
|
return msg_level >= _level.load();
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
|
|
|
|
2014-10-12 09:38:06 +08:00
|
|
|
|
2014-10-14 13:00:39 +08:00
|
|
|
inline void c11log::logger::formatter(formatter_ptr formatter)
|
|
|
|
{
|
|
|
|
_formatter = formatter;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline c11log::logger::formatter_ptr c11log::logger::formatter() const
|
|
|
|
{
|
|
|
|
return _formatter;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-14 09:56:10 +08:00
|
|
|
inline void c11log::logger::_variadic_log(c11log::details::line_logger&) {}
|
2014-10-12 09:38:06 +08:00
|
|
|
|
|
|
|
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
|
|
|
|