2014-03-22 20:11:17 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
// Thread safe logger
|
|
|
|
// Has log level and vector sinks which do the actual logging
|
|
|
|
#include<vector>
|
|
|
|
#include<memory>
|
|
|
|
#include<mutex>
|
|
|
|
#include<atomic>
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "common_types.h"
|
|
|
|
#include "sinks/base_sink.h"
|
|
|
|
#include "details/factory.h"
|
|
|
|
|
|
|
|
|
|
|
|
//Thread safe, fast logger.
|
|
|
|
//All initialization is done in ctor only, so we get away lot of locking
|
|
|
|
namespace c11log
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace details
|
|
|
|
{
|
|
|
|
class line_logger;
|
|
|
|
template<std::size_t> class fast_buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class logger
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
using sink_ptr = std::shared_ptr<sinks::base_sink>;
|
|
|
|
using formatter_ptr = std::shared_ptr<c11log::formatters::formatter>;
|
|
|
|
using sinks_vector_t = std::vector<sink_ptr>;
|
|
|
|
using sinks_init_list = std::initializer_list<sink_ptr>;
|
|
|
|
|
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() = default;
|
|
|
|
|
|
|
|
//Non copybale in anyway
|
|
|
|
logger(const logger&) = delete;
|
|
|
|
logger(logger&&) = delete;
|
|
|
|
logger& operator=(const logger&) = delete;
|
|
|
|
logger& operator=(logger&&) = delete;
|
|
|
|
|
|
|
|
void set_level(c11log::level::level_enum);
|
|
|
|
c11log::level::level_enum get_level() const;
|
|
|
|
|
|
|
|
const std::string& get_name() const;
|
|
|
|
bool should_log(c11log::level::level_enum) const;
|
|
|
|
|
2014-03-29 05:27:13 +08:00
|
|
|
details::line_logger log(c11log::level::level_enum);
|
|
|
|
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&);
|
2014-03-29 06:38:05 +08:00
|
|
|
template<typename T> details::line_logger critical(const T&);
|
2014-03-29 05:27:13 +08:00
|
|
|
template<typename T> details::line_logger fatal(const T&);
|
|
|
|
|
2014-03-22 20:11:17 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
friend details::line_logger;
|
|
|
|
|
2014-03-29 06:20:51 +08:00
|
|
|
std::string _logger_name;
|
2014-03-22 20:11:17 +08:00
|
|
|
formatter_ptr _formatter;
|
|
|
|
sinks_vector_t _sinks;
|
2014-03-22 22:26:08 +08:00
|
|
|
std::atomic_int _logger_level;
|
2014-03-22 20:11:17 +08:00
|
|
|
|
2014-03-29 00:03:24 +08:00
|
|
|
void _log_it(const details::log_msg& msg);
|
2014-03-22 20:11:17 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
logger& get_logger(const std::string& name);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Logger inline implementation
|
|
|
|
//
|
|
|
|
#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-03-22 20:11:17 +08:00
|
|
|
_logger_name(name),
|
|
|
|
_formatter(f),
|
|
|
|
_sinks(sinks_list)
|
|
|
|
{
|
2014-03-28 21:05:09 +08:00
|
|
|
//Seems that vs2013 doesnt support std::atomic member initialization, so its done here
|
2014-03-22 22:26:08 +08:00
|
|
|
_logger_level = level::INFO;
|
2014-03-29 06:38:05 +08:00
|
|
|
if(!_formatter)
|
|
|
|
_formatter = std::make_shared<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) :
|
2014-03-29 06:38:05 +08:00
|
|
|
logger(name, {sink}, f) {}
|
2014-03-22 20:11:17 +08:00
|
|
|
|
|
|
|
inline c11log::details::line_logger c11log::logger::log(c11log::level::level_enum msg_level)
|
|
|
|
{
|
2014-03-22 22:26:08 +08:00
|
|
|
return details::line_logger(this, msg_level, msg_level >= _logger_level);
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
|
|
|
|
2014-03-29 05:27:13 +08:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline c11log::details::line_logger c11log::logger::debug(const T& what)
|
2014-03-22 20:11:17 +08:00
|
|
|
{
|
2014-03-29 05:27:13 +08:00
|
|
|
bool really_log = should_log(level::DEBUG);
|
|
|
|
details::line_logger l(this, level::DEBUG, really_log);
|
2014-03-29 06:38:05 +08:00
|
|
|
if(really_log)
|
|
|
|
l << what;
|
2014-03-29 05:27:13 +08:00
|
|
|
return l;
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
2014-03-29 05:27:13 +08:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline c11log::details::line_logger c11log::logger::info(const T& what)
|
|
|
|
{
|
|
|
|
bool really_log = should_log(level::INFO);
|
|
|
|
details::line_logger l(this, level::INFO, really_log);
|
2014-03-29 06:38:05 +08:00
|
|
|
if(really_log)
|
|
|
|
l << what;
|
2014-03-29 05:27:13 +08:00
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline c11log::details::line_logger c11log::logger::warn(const T& what)
|
2014-03-22 20:11:17 +08:00
|
|
|
{
|
2014-03-29 05:27:13 +08:00
|
|
|
bool really_log = should_log(level::WARNING);
|
|
|
|
details::line_logger l(this, level::WARNING, really_log);
|
2014-03-29 06:38:05 +08:00
|
|
|
if(really_log)
|
|
|
|
l << what;
|
2014-03-29 05:27:13 +08:00
|
|
|
return l;
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
2014-03-29 05:27:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline c11log::details::line_logger c11log::logger::error(const T& what)
|
2014-03-22 20:11:17 +08:00
|
|
|
{
|
2014-03-29 05:27:13 +08:00
|
|
|
bool really_log = should_log(level::ERROR);
|
|
|
|
details::line_logger l(this, level::ERROR, really_log);
|
2014-03-29 06:38:05 +08:00
|
|
|
if(really_log)
|
|
|
|
l << what;
|
2014-03-29 05:27:13 +08:00
|
|
|
return l;
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
2014-03-29 05:27:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline c11log::details::line_logger c11log::logger::critical(const T& what)
|
2014-03-22 20:11:17 +08:00
|
|
|
{
|
2014-03-29 05:27:13 +08:00
|
|
|
bool really_log = should_log(level::CRITICAL);
|
|
|
|
details::line_logger l(this, level::CRITICAL, really_log);
|
2014-03-29 06:38:05 +08:00
|
|
|
if(really_log)
|
|
|
|
l << what;
|
2014-03-29 05:27:13 +08:00
|
|
|
return l;
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
2014-03-29 05:27:13 +08:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline c11log::details::line_logger c11log::logger::fatal(const T& what)
|
2014-03-22 20:11:17 +08:00
|
|
|
{
|
2014-03-29 06:38:05 +08:00
|
|
|
bool really_log = should_log(level::FATAL);
|
2014-03-29 05:27:13 +08:00
|
|
|
details::line_logger l(this, level::FATAL, really_log);
|
2014-03-29 06:38:05 +08:00
|
|
|
if(really_log)
|
|
|
|
l << what;
|
2014-03-29 05:27:13 +08:00
|
|
|
return l;
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-29 05:27:13 +08:00
|
|
|
|
2014-03-22 20:11:17 +08:00
|
|
|
inline const std::string& c11log::logger::get_name() const
|
|
|
|
{
|
|
|
|
return _logger_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void c11log::logger::set_level(c11log::level::level_enum level)
|
|
|
|
{
|
2014-03-22 22:26:08 +08:00
|
|
|
_logger_level.store(level);
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline c11log::level::level_enum c11log::logger::get_level() const
|
|
|
|
{
|
2014-03-22 22:26:08 +08:00
|
|
|
return static_cast<c11log::level::level_enum>(_logger_level.load());
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool c11log::logger::should_log(c11log::level::level_enum level) const
|
|
|
|
{
|
2014-03-22 22:26:08 +08:00
|
|
|
return level >= _logger_level.load();
|
2014-03-22 20:11:17 +08:00
|
|
|
}
|
|
|
|
|
2014-03-29 00:03:24 +08:00
|
|
|
inline void c11log::logger::_log_it(const details::log_msg& 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
|
|
|
}
|
|
|
|
|
|
|
|
// Static factory function
|
|
|
|
inline c11log::logger& c11log::get_logger(const std::string& name)
|
|
|
|
{
|
|
|
|
return *(c11log::details::factory::instance().get_logger(name));
|
|
|
|
}
|