spdlog/include/c11log/logger.h

165 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>
#include "level.h"
#include "sinks/base_sink.h"
#include "details/factory.h"
namespace c11log
{
namespace details
{
class line_logger;
}
2014-01-25 17:09:04 +08:00
class logger
{
2014-01-25 17:09:04 +08:00
public:
typedef std::shared_ptr<sinks::base_sink> sink_ptr_t;
typedef std::vector<sink_ptr_t> sinks_vector_t;
2014-01-25 17:09:04 +08:00
explicit logger(const std::string& name) : logger_name_(name),
formatter_(std::make_unique<formatters::default_formatter>()) {
atomic_level_.store(level::INFO);
}
2014-01-25 17:09:04 +08:00
~logger() = default;
2014-01-25 17:09:04 +08:00
logger(const logger&) = delete;
logger& operator=(const logger&) = delete;
2014-01-25 17:09:04 +08:00
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;
2014-01-25 17:09:04 +08:00
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:
friend details::line_logger;
2014-01-25 17:09:04 +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
void log_it_(const std::string& msg);
2014-01-25 17:09:04 +08:00
};
logger& get_logger(const std::string& name);
}
#include "details/line_logger.h"
2014-01-25 21:52:10 +08:00
//
// Logger inline impl
//
2014-01-25 17:09:04 +08:00
inline c11log::details::line_logger c11log::logger::log(c11log::level::level_enum msg_level)
{
if (msg_level >= atomic_level_) {
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()
{
return log(c11log::level::DEBUG);
2014-01-25 17:09:04 +08:00
}
inline c11log::details::line_logger c11log::logger::info()
{
return log(c11log::level::INFO);
2014-01-25 17:09:04 +08:00
}
inline c11log::details::line_logger c11log::logger::warn()
{
return log(c11log::level::WARNING);
2014-01-25 17:09:04 +08:00
}
inline c11log::details::line_logger c11log::logger::error()
{
return log(level::ERROR);
2014-01-25 17:09:04 +08:00
}
inline c11log::details::line_logger c11log::logger::fatal()
{
return log(c11log::level::FATAL);
2014-01-25 17:09:04 +08:00
}
inline void c11log::logger::set_name(const std::string& name)
{
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()
{
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)
{
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)
{
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)
{
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)
{
atomic_level_.store(level);
2014-01-25 17:09:04 +08:00
}
inline c11log::level::level_enum c11log::logger::get_level() const
{
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
{
return level >= atomic_level_.load();
2014-01-25 17:09:04 +08:00
}
2014-01-25 21:52:10 +08:00
inline void c11log::logger::log_it_(const std::string& msg)
2014-01-25 17:09:04 +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_)
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)
{
return *(c11log::details::factory::instance().get_logger(name));
2014-01-25 17:09:04 +08:00
}