spdlog/include/c11log/logger.h

172 lines
4.2 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"
2014-02-22 04:51:54 +08:00
namespace c11log {
2014-03-01 20:06:58 +08:00
2014-02-22 04:51:54 +08:00
namespace details {
class line_logger;
}
2014-01-25 17:09:04 +08:00
2014-03-01 20:06:58 +08:00
2014-02-22 04:51:54 +08:00
class logger {
2014-01-25 17:09:04 +08:00
public:
2014-02-22 04:51:54 +08:00
typedef std::shared_ptr<sinks::base_sink> sink_ptr_t;
typedef std::vector<sink_ptr_t> sinks_vector_t;
explicit logger(const std::string& name) :
2014-02-22 16:34:42 +08:00
_logger_name(name),
_formatter(new formatters::default_formatter()),
_sinks(),
_mutex(),
_atomic_level(level::INFO) {
2014-02-22 04:51:54 +08:00
}
~logger() = default;
logger(const logger&) = delete;
logger& operator=(const logger&) = delete;
void set_name(const std::string& name);
const std::string& get_name();
void add_sink(sink_ptr_t sink_ptr);
void remove_sink(sink_ptr_t sink_ptr);
void set_formatter(std::unique_ptr<formatters::formatter> formatter);
void set_level(c11log::level::level_enum level);
c11log::level::level_enum get_level() const;
bool should_log(c11log::level::level_enum level) const;
details::line_logger log(level::level_enum level);
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 = "";
std::unique_ptr<c11log::formatters::formatter> _formatter;
sinks_vector_t _sinks;
std::mutex _mutex;
std::atomic_int _atomic_level;
2014-01-25 17:09:04 +08:00
2014-02-22 16:34:42 +08:00
void _log_it(const std::string& msg);
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
//
// Logger inline impl
//
2014-02-22 04:51:54 +08:00
#include "details/line_logger.h"
2014-01-25 17:09:04 +08:00
inline c11log::details::line_logger c11log::logger::log(c11log::level::level_enum msg_level)
{
2014-02-22 16:34:42 +08:00
if (msg_level >= _atomic_level)
2014-02-22 04:51:54 +08:00
return details::line_logger(this, msg_level);
else
return details::line_logger(nullptr);
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 void c11log::logger::set_name(const std::string& name)
{
2014-02-22 16:34:42 +08:00
std::lock_guard<std::mutex> lock(_mutex);
_logger_name = name;
2014-01-25 17:09:04 +08:00
}
inline const std::string& c11log::logger::get_name()
{
2014-02-22 16:34:42 +08:00
std::lock_guard<std::mutex> lock(_mutex);
return _logger_name;
2014-01-25 17:09:04 +08:00
}
inline void c11log::logger::add_sink(sink_ptr_t sink_ptr)
{
2014-02-22 16:34:42 +08:00
std::lock_guard<std::mutex> lock(_mutex);
_sinks.push_back(sink_ptr);
2014-01-25 17:09:04 +08:00
}
inline void c11log::logger::remove_sink(sink_ptr_t sink_ptr)
{
2014-02-22 16:34:42 +08:00
std::lock_guard<std::mutex> lock(_mutex);
_sinks.erase(std::remove(_sinks.begin(), _sinks.end(), sink_ptr), _sinks.end());
2014-01-25 17:09:04 +08:00
}
inline void c11log::logger::set_formatter(std::unique_ptr<formatters::formatter> formatter)
{
2014-02-22 16:34:42 +08:00
std::lock_guard<std::mutex> lock(_mutex);
_formatter = std::move(formatter);
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-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
}
2014-02-22 16:34:42 +08:00
inline void c11log::logger::_log_it(const std::string& msg)
2014-01-25 17:09:04 +08:00
{
2014-02-22 16:34:42 +08:00
level::level_enum level = static_cast<level::level_enum>(_atomic_level.load());
std::lock_guard<std::mutex> lock(_mutex);
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
}
2014-02-21 03:39:58 +08:00