2014-01-25 17:09:04 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <string>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
|
|
|
|
2014-03-07 06:52:50 +08:00
|
|
|
namespace c11log
|
|
|
|
{
|
2014-01-25 17:09:04 +08:00
|
|
|
class logger;
|
2014-03-07 06:52:50 +08:00
|
|
|
namespace details
|
|
|
|
{
|
|
|
|
class factory
|
|
|
|
{
|
2014-01-25 17:09:04 +08:00
|
|
|
public:
|
2014-03-08 22:18:57 +08:00
|
|
|
using logger_ptr = std::shared_ptr<c11log::logger>;
|
|
|
|
using logger_map = std::unordered_map<std::string, logger_ptr>;
|
2014-03-08 22:40:47 +08:00
|
|
|
void add_logger(const std::string& name, logger_ptr);
|
|
|
|
logger_ptr get_logger(const std::string &name);
|
2014-01-25 17:09:04 +08:00
|
|
|
static c11log::details::factory& instance();
|
|
|
|
private:
|
2014-03-08 22:40:47 +08:00
|
|
|
std::mutex _loggers_mutex;
|
2014-01-25 17:09:04 +08:00
|
|
|
logger_map _loggers;
|
2014-03-08 22:18:57 +08:00
|
|
|
|
2014-01-25 17:09:04 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2014-02-21 03:39:58 +08:00
|
|
|
|
|
|
|
|
2014-03-08 22:18:57 +08:00
|
|
|
inline void c11log::details::factory::add_logger(const std::string& name, logger_ptr logger_p)
|
|
|
|
{
|
2014-03-08 22:40:47 +08:00
|
|
|
std::lock_guard<std::mutex> lock(_loggers_mutex);
|
|
|
|
_loggers.insert(logger_map::value_type(name, logger_p));
|
2014-03-08 22:18:57 +08:00
|
|
|
}
|
|
|
|
|
2014-02-21 03:39:58 +08:00
|
|
|
inline c11log::details::factory::logger_ptr c11log::details::factory::get_logger(const std::string &name)
|
|
|
|
{
|
2014-02-22 04:51:54 +08:00
|
|
|
std::lock_guard<std::mutex> lock(_loggers_mutex);
|
2014-03-08 22:18:57 +08:00
|
|
|
|
2014-03-08 22:40:47 +08:00
|
|
|
auto found = _loggers.find(name);
|
|
|
|
if (found != _loggers.end())
|
|
|
|
return found->second;
|
|
|
|
else
|
|
|
|
return logger_ptr(nullptr);
|
|
|
|
/*
|
|
|
|
auto found = _loggers.find(name);
|
2014-03-08 22:18:57 +08:00
|
|
|
|
2014-02-22 04:51:54 +08:00
|
|
|
if (found == _loggers.end()) {
|
|
|
|
auto new_logger_ptr = std::make_shared<c11log::logger>(name);
|
|
|
|
_loggers.insert(std::make_pair(name, new_logger_ptr));
|
|
|
|
return new_logger_ptr;
|
|
|
|
} else {
|
|
|
|
return found->second;
|
2014-03-08 22:18:57 +08:00
|
|
|
}*/
|
2014-02-21 03:39:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline c11log::details::factory & c11log::details::factory::instance()
|
|
|
|
{
|
2014-02-22 04:51:54 +08:00
|
|
|
static c11log::details::factory instance;
|
|
|
|
return instance;
|
2014-02-21 03:39:58 +08:00
|
|
|
}
|