spdlog/include/c11log/logger.h

167 lines
4.2 KiB
C
Raw Normal View History

2014-01-25 04:09:04 -05:00
#pragma once
2014-01-25 08:52:10 -05:00
// Thread safe logger
// Has log level and vector sinks which do the actual logging
2014-01-25 04:09:04 -05:00
#include<vector>
#include<memory>
#include<mutex>
#include<atomic>
#include <algorithm>
#include "level.h"
#include "sinks/base_sink.h"
#include "details/line_logger.h"
#include "details/factory.h"
2014-01-27 04:44:10 -05:00
2014-01-25 04:09:04 -05:00
namespace c11log {
class logger {
public:
typedef std::shared_ptr<sinks::base_sink> sink_ptr_t;
typedef std::vector<sink_ptr_t> sinks_vector_t;
2014-01-25 08:52:10 -05:00
explicit logger(const std::string& name) : logger_name_(name),
formatter_(std::make_unique<formatters::default_formatter>())
2014-01-25 04:09:04 -05:00
{
2014-01-25 08:52:10 -05:00
atomic_level_.store(level::INFO);
2014-01-25 04:09:04 -05:00
}
2014-01-25 08:52:10 -05:00
~logger() = default;
2014-01-25 04:09:04 -05:00
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();
private:
friend details::line_logger;
2014-01-25 08:52:10 -05: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 04:09:04 -05:00
2014-01-25 08:52:10 -05:00
void log_it_(const std::string& msg);
2014-01-25 04:09:04 -05:00
};
logger& get_logger(const std::string& name);
}
2014-01-25 08:52:10 -05:00
//
// Logger inline impl
//
2014-01-25 04:09:04 -05:00
inline c11log::details::line_logger c11log::logger::log(c11log::level::level_enum msg_level)
{
2014-01-25 08:52:10 -05:00
if (msg_level >= atomic_level_.load())
{
std::lock_guard<std::mutex> lock(mutex_);
2014-01-25 04:09:04 -05:00
return details::line_logger(this, msg_level);
2014-01-25 18:53:23 -05:00
}
2014-01-25 08:52:10 -05:00
else
{
2014-01-25 04:09:04 -05:00
return details::line_logger(nullptr);
}
}
inline c11log::details::line_logger c11log::logger::debug()
{
return log(c11log::level::DEBUG);
}
inline c11log::details::line_logger c11log::logger::info()
{
return log(c11log::level::INFO);
}
inline c11log::details::line_logger c11log::logger::warn()
{
return log(c11log::level::WARNING);
}
inline c11log::details::line_logger c11log::logger::error()
{
return log(level::ERROR);
}
inline c11log::details::line_logger c11log::logger::fatal()
{
return log(c11log::level::FATAL);
}
inline void c11log::logger::set_name(const std::string& name)
{
2014-01-25 08:52:10 -05:00
std::lock_guard<std::mutex> lock(mutex_);
logger_name_ = name;
2014-01-25 04:09:04 -05:00
}
inline const std::string& c11log::logger::get_name()
{
2014-01-25 08:52:10 -05:00
std::lock_guard<std::mutex> lock(mutex_);
return logger_name_;
2014-01-25 04:09:04 -05:00
}
inline void c11log::logger::add_sink(sink_ptr_t sink_ptr)
{
2014-01-25 08:52:10 -05:00
std::lock_guard<std::mutex> lock(mutex_);
sinks_.push_back(sink_ptr);
2014-01-25 04:09:04 -05:00
}
inline void c11log::logger::remove_sink(sink_ptr_t sink_ptr)
{
2014-01-25 08:52:10 -05:00
std::lock_guard<std::mutex> lock(mutex_);
sinks_.erase(std::remove(sinks_.begin(), sinks_.end(), sink_ptr), sinks_.end());
2014-01-25 04:09:04 -05:00
}
inline void c11log::logger::set_formatter(std::unique_ptr<formatters::formatter> formatter)
{
2014-01-25 08:52:10 -05:00
std::lock_guard<std::mutex> lock(mutex_);
formatter_ = std::move(formatter);
2014-01-25 04:09:04 -05:00
}
inline void c11log::logger::set_level(c11log::level::level_enum level)
{
2014-01-25 08:52:10 -05:00
atomic_level_.store(level);
2014-01-25 04:09:04 -05:00
}
inline c11log::level::level_enum c11log::logger::get_level() const
{
2014-01-25 08:52:10 -05:00
return static_cast<c11log::level::level_enum>(atomic_level_.load());
2014-01-25 04:09:04 -05:00
}
inline bool c11log::logger::should_log(c11log::level::level_enum level) const
{
2014-01-25 08:52:10 -05:00
return level >= atomic_level_.load();
2014-01-25 04:09:04 -05:00
}
2014-01-25 08:52:10 -05:00
inline void c11log::logger::log_it_(const std::string& msg)
2014-01-25 04:09:04 -05:00
{
2014-01-25 08:52:10 -05: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-01-25 04:09:04 -05:00
sink->log(msg, level);
}
2014-01-25 08:52:10 -05:00
// Static factory function
2014-01-25 04:09:04 -05:00
inline c11log::logger& c11log::get_logger(const std::string& name)
{
return *(c11log::details::factory::instance().get_logger(name));
}