2015-11-28 11:24:20 -05:00
|
|
|
//
|
|
|
|
// Copyright(c) 2015 Gabi Melman.
|
|
|
|
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
|
|
|
|
//
|
2014-10-31 21:20:54 -04:00
|
|
|
|
2014-10-24 11:01:11 -04:00
|
|
|
#pragma once
|
2015-11-28 11:24:20 -05:00
|
|
|
|
2014-10-24 11:01:11 -04:00
|
|
|
// Loggers registy of unique name->logger pointer
|
2015-12-12 11:44:42 -05:00
|
|
|
// An attempt to create a logger with an already existing name will be ignored
|
2014-10-24 11:01:11 -04:00
|
|
|
// If user requests a non existing logger, nullptr will be returned
|
|
|
|
// This class is thread safe
|
|
|
|
|
2016-02-24 22:26:18 -05:00
|
|
|
#include <spdlog/details/null_mutex.h>
|
|
|
|
#include <spdlog/logger.h>
|
|
|
|
#include <spdlog/async_logger.h>
|
|
|
|
#include <spdlog/common.h>
|
|
|
|
|
2016-02-24 23:20:07 -05:00
|
|
|
#include <chrono>
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
2014-10-24 11:01:11 -04:00
|
|
|
#include <mutex>
|
2016-02-24 23:20:07 -05:00
|
|
|
#include <string>
|
2014-10-24 11:01:11 -04:00
|
|
|
#include <unordered_map>
|
2014-10-30 19:13:27 -04:00
|
|
|
|
2014-10-31 20:28:49 -04:00
|
|
|
namespace spdlog
|
|
|
|
{
|
|
|
|
namespace details
|
|
|
|
{
|
2015-05-09 14:32:53 -04:00
|
|
|
template <class Mutex> class registry_t
|
2014-10-31 20:28:49 -04:00
|
|
|
{
|
2014-10-24 11:01:11 -04:00
|
|
|
public:
|
2015-04-11 09:36:31 -04:00
|
|
|
|
2015-04-11 10:15:20 -04:00
|
|
|
void register_logger(std::shared_ptr<logger> logger)
|
2015-04-11 09:36:31 -04:00
|
|
|
{
|
2015-05-09 14:32:53 -04:00
|
|
|
std::lock_guard<Mutex> lock(_mutex);
|
2016-03-22 12:32:38 -04:00
|
|
|
auto logger_name = logger->name();
|
|
|
|
throw_if_exists(logger_name);
|
|
|
|
_loggers[logger_name] = logger;
|
2015-04-11 09:36:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-20 19:47:04 -05:00
|
|
|
std::shared_ptr<logger> get(const std::string& logger_name)
|
|
|
|
{
|
2015-05-09 14:32:53 -04:00
|
|
|
std::lock_guard<Mutex> lock(_mutex);
|
2014-12-20 19:47:04 -05:00
|
|
|
auto found = _loggers.find(logger_name);
|
|
|
|
return found == _loggers.end() ? nullptr : found->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class It>
|
|
|
|
std::shared_ptr<logger> create(const std::string& logger_name, const It& sinks_begin, const It& sinks_end)
|
2016-03-22 12:32:38 -04:00
|
|
|
{
|
2015-05-09 14:32:53 -04:00
|
|
|
std::lock_guard<Mutex> lock(_mutex);
|
2016-03-22 12:32:38 -04:00
|
|
|
throw_if_exists(logger_name);
|
|
|
|
std::shared_ptr<logger> new_logger;
|
2014-12-20 19:47:04 -05:00
|
|
|
if (_async_mode)
|
2015-05-10 18:17:49 -04:00
|
|
|
new_logger = std::make_shared<async_logger>(logger_name, sinks_begin, sinks_end, _async_q_size, _overflow_policy, _worker_warmup_cb, _flush_interval_ms);
|
2014-12-20 19:47:04 -05:00
|
|
|
else
|
|
|
|
new_logger = std::make_shared<logger>(logger_name, sinks_begin, sinks_end);
|
|
|
|
|
|
|
|
if (_formatter)
|
|
|
|
new_logger->set_formatter(_formatter);
|
2015-04-11 09:36:31 -04:00
|
|
|
|
2015-04-11 10:15:20 -04:00
|
|
|
new_logger->set_level(_level);
|
2016-03-22 12:32:38 -04:00
|
|
|
//Add to registry
|
|
|
|
_loggers[logger_name] = new_logger;
|
2014-12-20 19:47:04 -05:00
|
|
|
return new_logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
void drop(const std::string& logger_name)
|
|
|
|
{
|
2015-05-09 14:32:53 -04:00
|
|
|
std::lock_guard<Mutex> lock(_mutex);
|
2014-12-20 19:47:04 -05:00
|
|
|
_loggers.erase(logger_name);
|
|
|
|
}
|
|
|
|
|
2014-12-27 06:01:15 -05:00
|
|
|
void drop_all()
|
|
|
|
{
|
2015-05-09 14:32:53 -04:00
|
|
|
std::lock_guard<Mutex> lock(_mutex);
|
2014-12-27 06:01:15 -05:00
|
|
|
_loggers.clear();
|
|
|
|
}
|
2014-12-20 19:47:04 -05:00
|
|
|
std::shared_ptr<logger> create(const std::string& logger_name, sinks_init_list sinks)
|
|
|
|
{
|
|
|
|
return create(logger_name, sinks.begin(), sinks.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<logger> create(const std::string& logger_name, sink_ptr sink)
|
|
|
|
{
|
|
|
|
return create(logger_name, { sink });
|
|
|
|
}
|
|
|
|
|
2015-04-11 09:36:31 -04:00
|
|
|
|
2014-12-20 19:47:04 -05:00
|
|
|
void formatter(formatter_ptr f)
|
|
|
|
{
|
2015-05-09 14:32:53 -04:00
|
|
|
std::lock_guard<Mutex> lock(_mutex);
|
2014-12-20 19:47:04 -05:00
|
|
|
_formatter = f;
|
|
|
|
for (auto& l : _loggers)
|
|
|
|
l.second->set_formatter(_formatter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_pattern(const std::string& pattern)
|
|
|
|
{
|
2015-05-09 14:32:53 -04:00
|
|
|
std::lock_guard<Mutex> lock(_mutex);
|
2014-12-20 19:47:04 -05:00
|
|
|
_formatter = std::make_shared<pattern_formatter>(pattern);
|
|
|
|
for (auto& l : _loggers)
|
|
|
|
l.second->set_formatter(_formatter);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_level(level::level_enum log_level)
|
|
|
|
{
|
2015-05-09 14:32:53 -04:00
|
|
|
std::lock_guard<Mutex> lock(_mutex);
|
2014-12-20 19:47:04 -05:00
|
|
|
for (auto& l : _loggers)
|
|
|
|
l.second->set_level(log_level);
|
2015-03-18 17:31:17 -04:00
|
|
|
_level = log_level;
|
2014-12-20 19:47:04 -05:00
|
|
|
}
|
|
|
|
|
2015-05-10 18:17:49 -04:00
|
|
|
void set_async_mode(size_t q_size, const async_overflow_policy overflow_policy, const std::function<void()>& worker_warmup_cb, const std::chrono::milliseconds& flush_interval_ms)
|
2014-12-20 19:47:04 -05:00
|
|
|
{
|
2015-05-09 14:32:53 -04:00
|
|
|
std::lock_guard<Mutex> lock(_mutex);
|
2014-12-20 19:47:04 -05:00
|
|
|
_async_mode = true;
|
|
|
|
_async_q_size = q_size;
|
2015-01-14 16:39:11 -05:00
|
|
|
_overflow_policy = overflow_policy;
|
|
|
|
_worker_warmup_cb = worker_warmup_cb;
|
2015-05-10 18:17:49 -04:00
|
|
|
_flush_interval_ms = flush_interval_ms;
|
2014-12-20 19:47:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_sync_mode()
|
|
|
|
{
|
2015-05-09 14:32:53 -04:00
|
|
|
std::lock_guard<Mutex> lock(_mutex);
|
2014-12-20 19:47:04 -05:00
|
|
|
_async_mode = false;
|
|
|
|
}
|
|
|
|
|
2015-05-09 14:32:53 -04:00
|
|
|
static registry_t<Mutex>& instance()
|
2014-12-20 19:47:04 -05:00
|
|
|
{
|
2015-05-09 14:32:53 -04:00
|
|
|
static registry_t<Mutex> s_instance;
|
2014-12-20 19:47:04 -05:00
|
|
|
return s_instance;
|
|
|
|
}
|
2014-10-24 11:01:11 -04:00
|
|
|
|
2016-03-22 12:32:38 -04:00
|
|
|
private:
|
2015-11-28 09:52:02 -05:00
|
|
|
registry_t<Mutex>() {}
|
2015-05-09 14:32:53 -04:00
|
|
|
registry_t<Mutex>(const registry_t<Mutex>&) = delete;
|
|
|
|
registry_t<Mutex>& operator=(const registry_t<Mutex>&) = delete;
|
2016-03-22 12:32:38 -04:00
|
|
|
|
|
|
|
void throw_if_exists(const std::string &logger_name)
|
|
|
|
{
|
|
|
|
if (_loggers.find(logger_name) != _loggers.end())
|
|
|
|
throw spdlog_ex("logger with name '" + logger_name + "' already exists");
|
|
|
|
}
|
2015-05-09 14:32:53 -04:00
|
|
|
Mutex _mutex;
|
2014-12-20 19:47:04 -05:00
|
|
|
std::unordered_map <std::string, std::shared_ptr<logger>> _loggers;
|
|
|
|
formatter_ptr _formatter;
|
|
|
|
level::level_enum _level = level::info;
|
|
|
|
bool _async_mode = false;
|
|
|
|
size_t _async_q_size = 0;
|
2015-01-15 03:34:50 -05:00
|
|
|
async_overflow_policy _overflow_policy = async_overflow_policy::block_retry;
|
2015-01-14 16:39:11 -05:00
|
|
|
std::function<void()> _worker_warmup_cb = nullptr;
|
2015-05-10 18:17:49 -04:00
|
|
|
std::chrono::milliseconds _flush_interval_ms;
|
2014-10-24 11:01:11 -04:00
|
|
|
};
|
2015-05-09 14:44:02 -04:00
|
|
|
#ifdef SPDLOG_NO_REGISTRY_MUTEX
|
2015-05-09 14:32:53 -04:00
|
|
|
typedef registry_t<spdlog::details::null_mutex> registry;
|
2015-05-09 14:44:02 -04:00
|
|
|
#else
|
|
|
|
typedef registry_t<std::mutex> registry;
|
2015-05-09 14:32:53 -04:00
|
|
|
#endif
|
2014-10-24 11:01:11 -04:00
|
|
|
}
|
|
|
|
}
|