spdlog/include/c11log/logger.h

160 lines
4.0 KiB
C
Raw Normal View History

2014-01-25 17:09:04 +08:00
#pragma once
2014-01-25 21:52:10 +08:00
// Thread safe logger
// Has log level and vector sinks which do the actual logging
2014-01-25 17:09:04 +08:00
#include<vector>
#include<memory>
#include<mutex>
#include<atomic>
#include <algorithm>
2014-03-01 20:06:58 +08:00
#include "common_types.h"
2014-01-25 17:09:04 +08:00
#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
2014-03-07 06:52:50 +08:00
namespace c11log
{
2014-03-01 20:06:58 +08:00
2014-03-07 06:52:50 +08:00
namespace details
{
class line_logger;
}
2014-01-25 17:09:04 +08:00
2014-03-01 20:06:58 +08:00
2014-03-07 06:52:50 +08:00
class logger
{
2014-01-25 17:09:04 +08:00
public:
using sink_ptr = std::shared_ptr<sinks::base_sink>;
2014-03-08 22:40:47 +08:00
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>;
logger(const std::string& name, formatter_ptr, sinks_init_list);
logger(const std::string& name, sinks_init_list);
2014-03-08 22:40:47 +08:00
logger(sinks_init_list sinks_list);
2014-02-22 04:51:54 +08:00
2014-02-22 04:51:54 +08:00
~logger() = default;
2014-03-08 22:40:47 +08:00
//Non copybale in anyway
2014-02-22 04:51:54 +08:00
logger(const logger&) = delete;
2014-03-08 22:40:47 +08:00
logger(logger&&) = delete;
2014-02-22 04:51:54 +08:00
logger& operator=(const logger&) = delete;
2014-03-08 22:40:47 +08:00
logger& operator=(logger&&) = delete;
2014-02-22 04:51:54 +08:00
void set_level(c11log::level::level_enum);
2014-02-22 04:51:54 +08:00
c11log::level::level_enum get_level() const;
const std::string& get_name() const;
bool should_log(c11log::level::level_enum) const;
details::line_logger log(level::level_enum);
2014-02-22 04:51:54 +08:00
details::line_logger debug();
details::line_logger info();
details::line_logger warn();
details::line_logger error();
details::line_logger fatal();
2014-01-25 17:09:04 +08:00
private:
2014-02-22 04:51:54 +08:00
friend details::line_logger;
2014-01-25 17:09:04 +08:00
2014-02-22 16:34:42 +08:00
std::string _logger_name = "";
formatter_ptr _formatter;
2014-02-22 16:34:42 +08:00
sinks_vector_t _sinks;
std::atomic_int _atomic_level;
2014-01-25 17:09:04 +08:00
void _log_it(const std::string& msg, const level::level_enum level);
2014-01-25 17:09:04 +08:00
};
logger& get_logger(const std::string& name);
2014-03-02 21:59:18 +08:00
2014-01-25 17:09:04 +08:00
}
2014-03-02 21:59:18 +08:00
2014-01-25 21:52:10 +08:00
//
2014-03-08 22:40:47 +08:00
// Logger inline implementation
2014-01-25 21:52:10 +08:00
//
2014-02-22 04:51:54 +08:00
#include "details/line_logger.h"
2014-03-08 22:40:47 +08:00
inline c11log::logger::logger(const std::string& name, formatter_ptr f, sinks_init_list sinks_list) :
_logger_name(name),
_formatter(f),
_sinks(sinks_list)
{
//Seems that vs2013 doesnt support atomic member initialization in ctor, so its done here
_atomic_level = level::INFO;
}
inline c11log::logger::logger(const std::string& name, sinks_init_list sinks_list) :
logger(name, std::make_shared<formatters::default_formatter>(), sinks_list) {}
inline c11log::logger::logger(sinks_init_list sinks_list) :
logger("", std::make_shared<formatters::default_formatter>(), sinks_list) {}
2014-01-25 17:09:04 +08:00
inline c11log::details::line_logger c11log::logger::log(c11log::level::level_enum msg_level)
{
return details::line_logger(this, msg_level, msg_level >= _atomic_level);
2014-01-25 17:09:04 +08:00
}
inline c11log::details::line_logger c11log::logger::debug()
{
2014-02-22 04:51:54 +08:00
return log(c11log::level::DEBUG);
2014-01-25 17:09:04 +08:00
}
inline c11log::details::line_logger c11log::logger::info()
{
2014-02-22 04:51:54 +08:00
return log(c11log::level::INFO);
2014-01-25 17:09:04 +08:00
}
inline c11log::details::line_logger c11log::logger::warn()
{
2014-02-22 04:51:54 +08:00
return log(c11log::level::WARNING);
2014-01-25 17:09:04 +08:00
}
inline c11log::details::line_logger c11log::logger::error()
{
2014-02-22 04:51:54 +08:00
return log(level::ERROR);
2014-01-25 17:09:04 +08:00
}
inline c11log::details::line_logger c11log::logger::fatal()
{
2014-02-22 04:51:54 +08:00
return log(c11log::level::FATAL);
2014-01-25 17:09:04 +08:00
}
inline const std::string& c11log::logger::get_name() const
2014-01-25 17:09:04 +08:00
{
2014-02-22 16:34:42 +08:00
return _logger_name;
2014-01-25 17:09:04 +08:00
}
inline void c11log::logger::set_level(c11log::level::level_enum level)
{
2014-02-22 16:34:42 +08:00
_atomic_level.store(level);
2014-01-25 17:09:04 +08:00
}
inline c11log::level::level_enum c11log::logger::get_level() const
2014-03-08 22:40:47 +08:00
{
2014-02-22 16:34:42 +08:00
return static_cast<c11log::level::level_enum>(_atomic_level.load());
2014-01-25 17:09:04 +08:00
}
inline bool c11log::logger::should_log(c11log::level::level_enum level) const
{
2014-02-22 16:34:42 +08:00
return level >= _atomic_level.load();
2014-01-25 17:09:04 +08:00
}
inline void c11log::logger::_log_it(const std::string& msg, const level::level_enum level)
{
2014-02-22 16:34:42 +08:00
for (auto &sink : _sinks)
2014-02-22 04:51:54 +08:00
sink->log(msg, level);
2014-01-25 17:09:04 +08:00
}
2014-01-25 21:52:10 +08:00
// Static factory function
2014-01-25 17:09:04 +08:00
inline c11log::logger& c11log::get_logger(const std::string& name)
{
2014-02-22 04:51:54 +08:00
return *(c11log::details::factory::instance().get_logger(name));
2014-01-25 17:09:04 +08:00
}