factory changes

This commit is contained in:
gabi 2014-09-02 00:24:35 +03:00
parent 8e8c39375a
commit f0622c8fb8
3 changed files with 44 additions and 33 deletions

View File

@ -5,9 +5,9 @@
#include <memory>
#include <mutex>
namespace c11log
{
class logger;
namespace details
{
class factory
@ -15,47 +15,46 @@ class factory
public:
using logger_ptr = std::shared_ptr<c11log::logger>;
using logger_map = std::unordered_map<std::string, logger_ptr>;
void add_logger(const std::string& name, logger_ptr);
std::shared_ptr<logger> create_logger(const std::string& name, logger::sinks_init_list, logger::formatter_ptr = nullptr);
logger_ptr get_logger(const std::string &name);
static c11log::details::factory& instance();
static factory& instance();
private:
std::mutex _loggers_mutex;
logger_map _loggers;
std::mutex _factory_mutex;
logger_map _factory;
};
}
}
inline void c11log::details::factory::add_logger(const std::string& name, logger_ptr logger_p)
inline std::shared_ptr<c11log::logger> c11log::details::factory::create_logger(const std::string& name, logger::sinks_init_list sinks, logger::formatter_ptr formatter)
{
std::lock_guard<std::mutex> lock(_loggers_mutex);
_loggers.insert(logger_map::value_type(name, logger_p));
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;
}
inline c11log::details::factory::logger_ptr c11log::details::factory::get_logger(const std::string &name)
{
std::lock_guard<std::mutex> lock(_loggers_mutex);
std::lock_guard<std::mutex> lock(_factory_mutex);
auto found = _loggers.find(name);
if (found != _loggers.end())
auto found = _factory.find(name);
if (found != _factory.end())
return found->second;
else
return logger_ptr(nullptr);
/*
auto found = _loggers.find(name);
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;
}*/
}
inline c11log::details::factory & c11log::details::factory::instance()
{
static c11log::details::factory instance;
static factory instance;
return instance;
}

View File

@ -7,9 +7,11 @@
#include<mutex>
#include<atomic>
#include <sstream>
#include "common_types.h"
#include "sinks/base_sink.h"
#include "details/factory.h"
#include "common_types.h"
//Thread safe, fast logger.
@ -23,6 +25,9 @@ class line_logger;
template<std::size_t> class fast_buf;
}
class logger
{
public:
@ -79,15 +84,28 @@ private:
};
//Get from loggers pool if exists with such name
logger& get_logger(const std::string& name);
std::shared_ptr<c11log::logger> create_logger(const std::string& name, logger::sinks_init_list sinks, logger::formatter_ptr formatter = nullptr);
std::shared_ptr<logger> get_logger(const std::string& name);
}
//
// Logger inline implementation
//
#include "details/line_logger.h"
#include "details/factory.h"
inline std::shared_ptr<c11log::logger> c11log::create_logger(const std::string& name, logger::sinks_init_list sinks, logger::formatter_ptr formatter)
{
return details::factory::instance().create_logger(name, sinks, std::move(formatter));
}
inline std::shared_ptr<c11log::logger> c11log::get_logger(const std::string& name)
{
return details::factory::instance().get_logger(name);
}
inline c11log::logger::logger(const std::string& name, sinks_init_list sinks_list, formatter_ptr f) :
@ -215,8 +233,4 @@ inline void c11log::logger::_log_it(details::log_msg& msg)
sink->log(msg);
}
// Static factory function
inline c11log::logger& c11log::get_logger(const std::string& name)
{
return *(c11log::details::factory::instance().get_logger(name));
}

View File

@ -43,7 +43,5 @@ inline std::shared_ptr<console_sink>& stderr_sink ()
return inst;
}
}
}