mirror of https://github.com/gabime/spdlog.git
Added registry
parent
319db7bccc
commit
796ec3db89
@ -0,0 +1,59 @@
|
|||||||
|
#pragma once
|
||||||
|
// Loggers registy of unique name->logger pointer
|
||||||
|
// If 2 loggers with same name are added, the last will be used
|
||||||
|
// If user requests a non existing logger, nullptr will be returned
|
||||||
|
// This class is thread safe
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <mutex>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include "../logger.h"
|
||||||
|
#include "../common.h"
|
||||||
|
|
||||||
|
namespace c11log {
|
||||||
|
namespace details {
|
||||||
|
class registry {
|
||||||
|
public:
|
||||||
|
std::shared_ptr<logger> get(const std::string& name)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> l(_mutex);
|
||||||
|
auto found = _loggers.find(name);
|
||||||
|
return found == _loggers.end() ? nullptr : found->second;
|
||||||
|
}
|
||||||
|
std::shared_ptr<logger> create(const std::string& logger_name, sinks_init_list sinks)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> l(_mutex);
|
||||||
|
return _loggers[logger_name] = std::make_shared<logger>(logger_name, sinks);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<logger> create(const std::string& logger_name, sink_ptr sink)
|
||||||
|
{
|
||||||
|
create(logger_name, { sink });
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
template<class It>
|
||||||
|
std::shared_ptr<logger> create (const std::string& logger_name, const It& sinks_begin, const It& sinks_end)
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> l(_mutex);
|
||||||
|
return _loggers[logger_name] = std::make_shared<logger>(logger_name, sinks_begin, sinks_end);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -1,106 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#include <memory>
|
|
||||||
#include "logger.h"
|
|
||||||
#include "sinks/file_sinks.h"
|
|
||||||
#include "sinks/stdout_sinks.h"
|
|
||||||
|
|
||||||
//
|
|
||||||
// logger creation shotcuts
|
|
||||||
//
|
|
||||||
namespace c11log
|
|
||||||
{
|
|
||||||
namespace factory
|
|
||||||
{
|
|
||||||
//
|
|
||||||
//
|
|
||||||
//std::shared_ptr<logger> get(const std::string& name);
|
|
||||||
//std::shared_ptr<logger> create(const std::string& name, sinks_init_list , formatter_ptr);
|
|
||||||
//std::shared_ptr<logger> create(const std::string& name, sinks_init_list, string format);
|
|
||||||
//template<class It>
|
|
||||||
//std::shared_ptr<logger> create (const std::string& name, const It& begin, const It& end);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//
|
|
||||||
//
|
|
||||||
////
|
|
||||||
//// console loggers single/multi threaded
|
|
||||||
////
|
|
||||||
//std::unique_ptr<logger> stdout_logger(const std::string& name = "")
|
|
||||||
//{
|
|
||||||
// auto sink = std::make_shared<sinks::stderr_sink_st>();
|
|
||||||
// return std::unique_ptr<logger>(new logger(name, { sink }));
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//std::unique_ptr<logger> stdout_logger_mt(const std::string& name = "")
|
|
||||||
//{
|
|
||||||
// auto sink = std::make_shared<sinks::stderr_sink_mt>();
|
|
||||||
// return std::unique_ptr<logger>(new logger(name, { sink }));
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
////
|
|
||||||
//// simple file logger single/multi threaded
|
|
||||||
////
|
|
||||||
//std::unique_ptr<logger> simple_file_logger(const std::string& filename, const std::string& logger_name = "" )
|
|
||||||
//{
|
|
||||||
// auto fsink = std::make_shared<sinks::simple_file_sink_st>(filename);
|
|
||||||
// return std::unique_ptr<logger>(new c11log::logger(logger_name, { fsink }));
|
|
||||||
//
|
|
||||||
//}
|
|
||||||
//std::unique_ptr<logger> simple_file_logger_mt(const std::string& filename, const std::string& logger_name = "")
|
|
||||||
//{
|
|
||||||
// auto fsink = std::make_shared<sinks::simple_file_sink_mt>(filename);
|
|
||||||
// return std::unique_ptr<logger>(new c11log::logger(logger_name, { fsink }));
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
////
|
|
||||||
//// daily file logger single/multi threaded
|
|
||||||
////
|
|
||||||
//std::unique_ptr<logger> daily_file_logger(
|
|
||||||
// const std::string &filename,
|
|
||||||
// const std::string &extension,
|
|
||||||
// const std::size_t flush_every,
|
|
||||||
// const std::string& logger_name = "")
|
|
||||||
//{
|
|
||||||
// auto fsink = std::make_shared<sinks::daily_file_sink_st>(filename, extension, flush_every);
|
|
||||||
// return std::unique_ptr<logger>(new c11log::logger(logger_name, { fsink }));
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//std::unique_ptr<logger> daily_file_logger_mt(
|
|
||||||
// const std::string &filename,
|
|
||||||
// const std::string &extension,
|
|
||||||
// const std::size_t flush_every,
|
|
||||||
// const std::string& logger_name = "")
|
|
||||||
//{
|
|
||||||
// auto fsink = std::make_shared<sinks::daily_file_sink_mt>(filename, extension, flush_every);
|
|
||||||
// return std::unique_ptr<logger>(new c11log::logger(logger_name, { fsink }));
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
////
|
|
||||||
//// rotating file logger single/multi threaded
|
|
||||||
////
|
|
||||||
//std::unique_ptr<logger> rotating_file_logger(
|
|
||||||
// const std::string &filename,
|
|
||||||
// const std::string &extension,
|
|
||||||
// const std::size_t max_size,
|
|
||||||
// const std::size_t max_files,
|
|
||||||
// const std::size_t flush_every,
|
|
||||||
// const std::string& logger_name = "")
|
|
||||||
//{
|
|
||||||
// auto fsink = std::make_shared<sinks::rotating_file_sink_st>(filename, extension, max_size, max_files, flush_every);
|
|
||||||
// return std::unique_ptr<logger>(new c11log::logger(logger_name, { fsink }));
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//std::unique_ptr<logger> rotating_file_logger_mt(
|
|
||||||
// const std::string &filename,
|
|
||||||
// const std::string &extension,
|
|
||||||
// const std::size_t max_size,
|
|
||||||
// const std::size_t max_files,
|
|
||||||
// const std::size_t flush_every,
|
|
||||||
// const std::string& logger_name = "")
|
|
||||||
//{
|
|
||||||
// auto fsink = std::make_shared<sinks::rotating_file_sink_mt>(filename, extension, max_size, max_files, flush_every);
|
|
||||||
// return std::unique_ptr<logger>(new c11log::logger(logger_name, { fsink }));
|
|
||||||
//}
|
|
||||||
} // ns factory
|
|
||||||
} // ns c11log
|
|
Loading…
Reference in New Issue