spdlog/include/c11log/logger.h

199 lines
5.1 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 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"
#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-14 09:29:52 +08:00
using sink_ptr = std::shared_ptr<sinks::sink>;
2014-03-22 20:11:17 +08:00
using sinks_vector_t = std::vector<sink_ptr>;
using sinks_init_list = std::initializer_list<sink_ptr>;
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);
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);
~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-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-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
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-03-22 20:11:17 +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),
_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)
_formatter = std::make_unique<pattern_formatter>("%t");
2014-03-22 20:11:17 +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-29 06:20:51 +08:00
inline c11log::logger::logger(const std::string& name, sink_ptr sink, formatter_ptr f) :
logger(name, {sink}, std::move(f)) {}
2014-03-22 20:11:17 +08:00
2014-03-29 07:25:52 +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;
}
2014-10-14 09:14:35 +08:00
template <typename... Args>
2014-10-14 09:14:35 +08:00
inline c11log::details::line_logger c11log::logger::trace(const Args&... args) {
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>
inline c11log::details::line_logger c11log::logger::debug(const Args&... args) {
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>
inline c11log::details::line_logger c11log::logger::info(const Args&... args) {
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>
inline c11log::details::line_logger c11log::logger::warn(const Args&... args) {
return log(level::WARNING, args...);
2014-03-31 07:24:11 +08:00
}
2014-10-14 09:14:35 +08:00
template <typename... Args>
inline c11log::details::line_logger c11log::logger::critical(const Args&... args) {
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-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
}
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
{
_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