spdlog/include/spdlog/details/registry.h

105 lines
2.7 KiB
C
Raw Normal View History

2014-10-24 23:01:11 +08:00
#pragma once
// Loggers registy of unique name->logger pointer
// An attempt to create a logger with an alreasy existing name will be ignored
2014-10-24 23:01:11 +08:00
// If user requests a non existing logger, nullptr will be returned
// This class is thread safe
#include <string>
#include <mutex>
#include <unordered_map>
2014-10-31 07:13:27 +08:00
2014-10-24 23:01:11 +08:00
#include "../logger.h"
#include "../common.h"
2014-10-31 07:13:27 +08:00
namespace spdlog {
2014-10-24 23:01:11 +08:00
namespace details {
2014-10-31 07:13:27 +08:00
2014-10-24 23:01:11 +08:00
class registry {
public:
std::shared_ptr<logger> get(const std::string& logger_name)
2014-10-24 23:01:11 +08:00
{
2014-10-31 07:13:27 +08:00
std::lock_guard<std::mutex> lock(_mutex);
auto found = _loggers.find(logger_name);
2014-10-24 23:01:11 +08:00
return found == _loggers.end() ? nullptr : found->second;
}
2014-10-31 07:13:27 +08:00
template<class It>
std::shared_ptr<logger> create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end)
2014-10-24 23:01:11 +08:00
{
2014-10-31 07:13:27 +08:00
std::lock_guard<std::mutex> lock(_mutex);
//If already exists, just return it
auto found = _loggers.find(logger_name);
if (found != _loggers.end())
return found->second;
2014-10-31 07:13:27 +08:00
auto new_logger = std::make_shared<logger>(logger_name, sinks_begin, sinks_end);
2014-10-25 05:32:56 +08:00
new_logger->set_formatter(_formatter);
2014-10-31 07:13:27 +08:00
new_logger->set_level(_level);
2014-10-25 05:32:56 +08:00
_loggers[logger_name] = new_logger;
return new_logger;
2014-10-24 23:01:11 +08:00
}
2014-10-31 07:13:27 +08:00
std::shared_ptr<logger> create(const std::string& logger_name, sinks_init_list sinks)
{
return create(logger_name, sinks.begin(), sinks.end());
}
2014-10-24 23:01:11 +08:00
std::shared_ptr<logger> create(const std::string& logger_name, sink_ptr sink)
{
2014-10-25 05:32:56 +08:00
return create(logger_name, { sink });
2014-10-24 23:01:11 +08:00
}
2014-10-31 07:13:27 +08:00
void formatter(formatter_ptr f)
2014-10-24 23:01:11 +08:00
{
2014-10-31 07:13:27 +08:00
std::lock_guard<std::mutex> lock(_mutex);
_formatter = f;
for (auto& l : _loggers)
l.second->set_formatter(_formatter);
2014-10-25 05:32:56 +08:00
}
2014-10-31 07:13:27 +08:00
void set_pattern(const std::string& pattern)
2014-10-25 05:32:56 +08:00
{
2014-10-31 07:13:27 +08:00
std::lock_guard<std::mutex> lock(_mutex);
_formatter = std::make_shared<pattern_formatter>(pattern);
for (auto& l : _loggers)
l.second->set_formatter(_formatter);
2014-10-24 23:01:11 +08:00
}
2014-10-31 07:13:27 +08:00
void set_level(level::level_enum log_level)
2014-10-25 05:32:56 +08:00
{
2014-10-31 07:13:27 +08:00
std::lock_guard<std::mutex> lock(_mutex);
for (auto& l : _loggers)
l.second->set_level(log_level);
2014-10-25 05:32:56 +08:00
}
2014-10-24 23:01:11 +08:00
2014-11-01 08:12:12 +08:00
void close_all()
2014-10-25 05:32:56 +08:00
{
2014-10-31 07:13:27 +08:00
std::lock_guard<std::mutex> lock(_mutex);
_level = level::OFF;
for (auto& l : _loggers)
2014-11-01 08:12:12 +08:00
l.second->close();
2014-10-25 05:32:56 +08:00
}
2014-10-24 23:01:11 +08:00
static registry& instance()
{
static registry s_instance;
return s_instance;
}
private:
registry() = default;
registry(const registry&) = delete;
std::mutex _mutex;
std::unordered_map <std::string, std::shared_ptr<logger>> _loggers;
2014-10-25 05:32:56 +08:00
formatter_ptr _formatter;
2014-10-31 07:13:27 +08:00
level::level_enum _level = level::INFO;
2014-10-24 23:01:11 +08:00
};
}
}