mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-25 10:01:33 +08:00
factory changes
This commit is contained in:
parent
8e8c39375a
commit
f0622c8fb8
@ -5,9 +5,9 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
|
||||||
|
|
||||||
namespace c11log
|
namespace c11log
|
||||||
{
|
{
|
||||||
class logger;
|
|
||||||
namespace details
|
namespace details
|
||||||
{
|
{
|
||||||
class factory
|
class factory
|
||||||
@ -15,47 +15,46 @@ class factory
|
|||||||
public:
|
public:
|
||||||
using logger_ptr = std::shared_ptr<c11log::logger>;
|
using logger_ptr = std::shared_ptr<c11log::logger>;
|
||||||
using logger_map = std::unordered_map<std::string, logger_ptr>;
|
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);
|
logger_ptr get_logger(const std::string &name);
|
||||||
static c11log::details::factory& instance();
|
static factory& instance();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::mutex _loggers_mutex;
|
std::mutex _factory_mutex;
|
||||||
logger_map _loggers;
|
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);
|
std::lock_guard<std::mutex> lock(_factory_mutex);
|
||||||
_loggers.insert(logger_map::value_type(name, logger_p));
|
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)
|
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);
|
auto found = _factory.find(name);
|
||||||
if (found != _loggers.end())
|
if (found != _factory.end())
|
||||||
return found->second;
|
return found->second;
|
||||||
else
|
else
|
||||||
return logger_ptr(nullptr);
|
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()
|
inline c11log::details::factory & c11log::details::factory::instance()
|
||||||
{
|
{
|
||||||
static c11log::details::factory instance;
|
static factory instance;
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,11 @@
|
|||||||
#include<mutex>
|
#include<mutex>
|
||||||
#include<atomic>
|
#include<atomic>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include "common_types.h"
|
|
||||||
#include "sinks/base_sink.h"
|
#include "sinks/base_sink.h"
|
||||||
#include "details/factory.h"
|
#include "common_types.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Thread safe, fast logger.
|
//Thread safe, fast logger.
|
||||||
@ -23,6 +25,9 @@ class line_logger;
|
|||||||
template<std::size_t> class fast_buf;
|
template<std::size_t> class fast_buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class logger
|
class logger
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -79,15 +84,28 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
//Get from loggers pool if exists with such name
|
std::shared_ptr<c11log::logger> create_logger(const std::string& name, logger::sinks_init_list sinks, logger::formatter_ptr formatter = nullptr);
|
||||||
logger& get_logger(const std::string& name);
|
std::shared_ptr<logger> get_logger(const std::string& name);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Logger inline implementation
|
// Logger inline implementation
|
||||||
//
|
//
|
||||||
|
|
||||||
#include "details/line_logger.h"
|
#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) :
|
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);
|
sink->log(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Static factory function
|
|
||||||
inline c11log::logger& c11log::get_logger(const std::string& name)
|
|
||||||
{
|
|
||||||
return *(c11log::details::factory::instance().get_logger(name));
|
|
||||||
}
|
|
||||||
|
@ -43,7 +43,5 @@ inline std::shared_ptr<console_sink>& stderr_sink ()
|
|||||||
return inst;
|
return inst;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user