spdlog/include/c11log/factory.h

61 lines
1.4 KiB
C
Raw Normal View History

2014-03-22 08:11:17 -04:00
#pragma once
#include <unordered_map>
#include <string>
#include <memory>
#include <mutex>
2014-09-01 17:24:35 -04:00
2014-03-22 08:11:17 -04:00
namespace c11log
{
namespace details
{
class factory
{
public:
using logger_ptr = std::shared_ptr<c11log::logger>;
using logger_map = std::unordered_map<std::string, logger_ptr>;
2014-09-01 17:24:35 -04:00
std::shared_ptr<logger> create_logger(const std::string& name, logger::sinks_init_list, logger::formatter_ptr = nullptr);
2014-03-22 08:11:17 -04:00
logger_ptr get_logger(const std::string &name);
2014-09-01 17:24:35 -04:00
static factory& instance();
2014-03-22 08:11:17 -04:00
private:
2014-09-01 17:24:35 -04:00
std::mutex _factory_mutex;
logger_map _factory;
2014-03-22 08:11:17 -04:00
};
}
}
2014-09-01 17:24:35 -04:00
inline std::shared_ptr<c11log::logger> c11log::details::factory::create_logger(const std::string& name, logger::sinks_init_list sinks, logger::formatter_ptr formatter)
2014-03-22 08:11:17 -04:00
{
2014-09-01 17:24:35 -04:00
std::lock_guard<std::mutex> lock(_factory_mutex);
logger_ptr logger_p = std::make_shared<logger>(name, sinks, std::move(formatter));
_factory.insert(logger_map::value_type(name, logger_p));
return logger_p;
2014-03-22 08:11:17 -04:00
}
2014-09-01 17:24:35 -04:00
2014-03-22 08:11:17 -04:00
inline c11log::details::factory::logger_ptr c11log::details::factory::get_logger(const std::string &name)
{
2014-09-01 17:24:35 -04:00
std::lock_guard<std::mutex> lock(_factory_mutex);
2014-03-22 08:11:17 -04:00
2014-09-01 17:24:35 -04:00
auto found = _factory.find(name);
if (found != _factory.end())
2014-03-22 08:11:17 -04:00
return found->second;
else
return logger_ptr(nullptr);
}
2014-09-01 17:24:35 -04:00
2014-03-22 08:11:17 -04:00
inline c11log::details::factory & c11log::details::factory::instance()
{
2014-09-01 17:24:35 -04:00
static factory instance;
2014-03-22 08:11:17 -04:00
return instance;
}