spdlog/include/c11log/logger.h

278 lines
7.6 KiB
C
Raw Normal View History

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 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 "details/pattern_formatter.h"
2014-09-02 05:24:35 +08:00
2014-10-24 23:01:11 +08:00
2014-03-22 20:11:17 +08:00
namespace c11log
{
namespace details
{
class line_logger;
}
class logger
{
public:
logger(const std::string& name, sinks_init_list);
template<class It>
2014-10-14 13:00:39 +08:00
logger(const std::string& name, const It& begin, const It& end);
2014-10-24 23:01:11 +08:00
void c11log::logger::set_format(const std::string& format);
void set_formatter(formatter_ptr);
formatter_ptr get_formatter() const;
2014-03-22 20:11:17 +08:00
logger(const logger&) = delete;
logger& operator=(const logger&) = delete;
2014-10-24 23:01:11 +08:00
void set_level(level::level_enum);
2014-10-11 02:32:10 +08:00
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 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);
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-24 23:01:11 +08:00
//static functions
//get/set default formatter
static formatter_ptr default_formatter(formatter_ptr formatter = nullptr);
static formatter_ptr default_format(const std::string& format);
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;
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-24 23:01:11 +08:00
//
// Registry functions for easy loggers creation and retrieval
// example
// auto console_logger = c11log::create("my_logger", c11log::sinks<stdout_sink_mt>);
// auto same_logger = c11log::get("my_logger");
// auto file_logger = c11
//
std::shared_ptr<logger> get(const std::string& name);
std::shared_ptr<logger> create(const std::string& logger_name, sinks_init_list sinks);
template <typename Sink, typename... Args>
std::shared_ptr<c11log::logger> create(const std::string& logger_name, const Args&... args);
template<class It>
std::shared_ptr<logger> create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end);
2014-03-22 20:11:17 +08:00
}
//
2014-10-24 23:01:11 +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"
inline c11log::logger::logger(const std::string& logger_name, sinks_init_list sinks_list):
2014-10-14 09:56:10 +08:00
_name(logger_name),
2014-03-22 20:11:17 +08:00
_sinks(sinks_list)
{
2014-10-15 07:24:35 +08:00
// no support under vs2013 for member initialization for std::atomic
2014-10-11 02:17:26 +08:00
_level = level::INFO;
2014-03-22 20:11:17 +08:00
}
template<class It>
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),
_sinks(begin, end)
2014-10-14 13:00:39 +08:00
{}
2014-03-29 07:25:52 +08:00
inline void c11log::logger::set_formatter(c11log::formatter_ptr msg_formatter)
{
_formatter = msg_formatter;
}
2014-10-24 23:01:11 +08:00
inline void c11log::logger::set_format(const std::string& format)
{
_formatter = std::make_shared<details::pattern_formatter>(format);
}
inline c11log::formatter_ptr c11log::logger::get_formatter() const
{
return _formatter;
}
2014-10-24 23:01:11 +08:00
inline c11log::formatter_ptr c11log::logger::default_formatter(formatter_ptr formatter)
{
static formatter_ptr g_default_formatter = std::make_shared<details::pattern_formatter>("[%Y-%m-%d %H:%M:%S.%e] [%n] [%l] %t");
if (formatter)
g_default_formatter = formatter;
return g_default_formatter;
}
2014-10-24 23:01:11 +08:00
inline c11log::formatter_ptr c11log::logger::default_format(const std::string& format)
{
return default_formatter(std::make_shared<details::pattern_formatter>(format));
}
template <typename... Args>
inline c11log::details::line_logger c11log::logger::log(level::level_enum lvl, const Args&... args) {
bool msg_enabled = should_log(lvl);
details::line_logger l(this, lvl, msg_enabled);
if (msg_enabled)
_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-24 23:01:11 +08:00
return log(level::WARN, args...);
2014-03-31 07:24:11 +08:00
}
2014-10-19 23:29:05 +08:00
template <typename... Args>
inline c11log::details::line_logger c11log::logger::error(const Args&... args)
{
2014-10-24 23:01:11 +08:00
return log(level::ERR, args...);
2014-10-19 23:29:05 +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-24 23:01:11 +08:00
inline void c11log::logger::set_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-14 13:00:39 +08:00
2014-10-14 09:56:10 +08:00
inline void c11log::logger::_variadic_log(c11log::details::line_logger&) {}
template <typename First, typename... Rest>
void c11log::logger::_variadic_log(c11log::details::line_logger& l, const First& first, const Rest&... rest)
{
l.write(first);
2014-10-24 23:01:11 +08:00
l.write(' ');
_variadic_log(l, rest...);
}
inline void c11log::logger::_log_msg(details::log_msg& msg)
2014-03-22 20:11:17 +08:00
{
2014-10-24 23:01:11 +08:00
if(!_formatter)
_formatter = logger::default_formatter();
_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-10-24 23:01:11 +08:00
//
// Global registry functions
//
#include "details/registry.h"
inline std::shared_ptr<c11log::logger> c11log::get(const std::string& name)
{
return details::registry::instance().get(name);
}
inline std::shared_ptr<c11log::logger> c11log::create(const std::string& logger_name, c11log::sinks_init_list sinks)
{
return details::registry::instance().create(logger_name, sinks);
}
template <typename Sink, typename... Args>
inline std::shared_ptr<c11log::logger> c11log::create(const std::string& logger_name, const Args&... args)
{
sink_ptr sink = std::make_shared<Sink>(args...);
return details::registry::instance().create(logger_name, { sink });
}
template<class It>
inline std::shared_ptr<c11log::logger> c11log::create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end)
{
std::lock_guard<std::mutex> l(_mutex);
return details::registry::instance().create(logger_name, std::forward(sinks_begin), std::forward(sinks_end));
}
2014-09-02 05:24:35 +08:00