spdlog/include/c11log/logger.h

221 lines
5.8 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"
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>;
using sinks_init_list = std::initializer_list<sink_ptr>;
2014-10-11 02:32:10 +08:00
using formatter_ptr = std::unique_ptr<formatters::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-03-29 06:38:05 +08:00
logger(const std::string& name, sink_ptr, formatter_ptr = nullptr);
2014-03-22 20:11:17 +08:00
logger(const logger&) = delete;
logger& operator=(const logger&) = delete;
2014-10-11 02:17:26 +08:00
~logger() = default;
2014-03-22 20:11:17 +08:00
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-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&);
template<typename T> details::line_logger info(const T&);
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&);
template<typename T> details::line_logger fatal(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 info();
details::line_logger warn();
details::line_logger error();
details::line_logger critical();
details::line_logger fatal();
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 _log_it(details::log_msg& msg);
2014-03-22 20:11:17 +08:00
};
2014-03-31 07:09:13 +08:00
2014-10-10 07:46:03 +08:00
//std::shared_ptr<c11log::logger> create_logger(const std::string& name, logger::sinks_init_list sinks, logger::formatter_ptr formatter = nullptr);
//std::shared_ptr<logger> get_logger(const std::string& name);
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-09-02 05:24:35 +08:00
#include "details/factory.h"
2014-10-10 07:46:03 +08:00
/*
2014-09-02 05:24:35 +08:00
inline std::shared_ptr<c11log::logger> c11log::create_logger(const std::string& name, logger::sinks_init_list sinks, logger::formatter_ptr formatter)
{
return details::factory::instance().create_logger(name, sinks, std::move(formatter));
}
inline std::shared_ptr<c11log::logger> c11log::get_logger(const std::string& name)
{
return details::factory::instance().get_logger(name);
2014-10-10 07:46:03 +08:00
}*/
2014-03-22 20:11:17 +08:00
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<formatters::default_formatter>();
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) :
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-03-31 07:31:26 +08:00
details::line_logger l(this, level::DEBUG, should_log(level::DEBUG));
l.write(msg);
return l;
2014-03-22 20:11:17 +08:00
}
2014-03-31 07:09:13 +08:00
template<typename T>
inline c11log::details::line_logger c11log::logger::info(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::INFO, should_log(level::INFO));
l.write(msg);
return l;
2014-03-22 20:11:17 +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-03-31 07:09:13 +08:00
template<typename T>
inline c11log::details::line_logger c11log::logger::fatal(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::FATAL, should_log(level::FATAL));
l.write(msg);
return l;
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::info()
{
2014-03-31 07:31:26 +08:00
return details::line_logger(this, level::INFO, should_log(level::INFO));
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
}
inline c11log::details::line_logger c11log::logger::fatal()
{
2014-03-31 07:31:26 +08:00
return details::line_logger(this, level::FATAL, should_log(level::FATAL));
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::_log_it(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