New async_logger class and API

This commit is contained in:
gabi
2014-11-24 01:29:09 +02:00
parent e88a46691a
commit 4cb6aa90b2
11 changed files with 305 additions and 48 deletions

View File

@@ -0,0 +1,78 @@
/*************************************************************************/
/* spdlog - an extremely fast and easy to use c++11 logging library. */
/* Copyright (c) 2014 Gabi Melman. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#pragma once
#include <memory>
#include "../sinks/async_sink.h"
//
// Async Logger implementation
// Use single async_sink (queue) to perform the logging in a worker thread
//
template<class It>
inline spdlog::async_logger::async_logger(const std::string& name, const It& begin, const It& end, size_t queue_size, const log_clock::duration& shutdown_duration) :
logger(name, begin, end),
_shutdown_duration(shutdown_duration),
_as(std::unique_ptr<sinks::async_sink>(new sinks::async_sink(queue_size)))
{
_as->set_formatter(_formatter);
for (auto &s : _sinks)
_as->add_sink(s);
};
inline spdlog::async_logger::async_logger(const std::string& logger_name, sinks_init_list sinks, size_t queue_size, const log_clock::duration& shutdown_duration) :
async_logger(logger_name, sinks.begin(), sinks.end(), queue_size, shutdown_duration) {}
inline spdlog::async_logger::async_logger(const std::string& logger_name, sink_ptr single_sink, size_t queue_size, const log_clock::duration& shutdown_duration) :
async_logger(logger_name, { single_sink }, queue_size, shutdown_duration) {}
inline void spdlog::async_logger::_log_msg(details::log_msg& msg)
{
_as->log(msg);
}
inline void spdlog::async_logger::_set_formatter(spdlog::formatter_ptr msg_formatter)
{
_formatter = msg_formatter;
_as->set_formatter(_formatter);
}
inline void spdlog::async_logger::_set_pattern(const std::string& pattern)
{
_formatter = std::make_shared<pattern_formatter>(pattern);
_as->set_formatter(_formatter);
}
inline void spdlog::async_logger::_stop()
{
set_level(level::OFF);
_as->shutdown(_shutdown_duration);
}

View File

@@ -58,7 +58,19 @@ struct log_msg
raw(std::move(other.raw)),
formatted(std::move(other.formatted)) {}
log_msg& operator=(log_msg&& other) = delete;
log_msg& operator=(log_msg&& other)
{
if (this == &other)
return *this;
logger_name = std::move(other.logger_name);
level = other.level;
time = std::move(other.time);
tm_time = other.tm_time;
raw = std::move(other.raw);
formatted = std::move(other.formatted);
return *this;
}

View File

@@ -30,7 +30,7 @@
#include "./line_logger.h"
/* public functions */
template<class It>
inline spdlog::logger::logger(const std::string& logger_name, const It& begin, const It& end) :
_name(logger_name),
@@ -51,20 +51,18 @@ inline spdlog::logger::logger(const std::string& logger_name, spdlog::sink_ptr s
{}
inline spdlog::logger::~logger() {}
inline void spdlog::logger::set_formatter(spdlog::formatter_ptr msg_formatter)
{
_formatter = msg_formatter;
_set_formatter(msg_formatter);
}
inline void spdlog::logger::set_pattern(const std::string& pattern)
{
_formatter = std::make_shared<pattern_formatter>(pattern);
_set_pattern(pattern);
}
inline spdlog::formatter_ptr spdlog::logger::get_formatter() const
{
return _formatter;
}
template <typename... Args>
@@ -158,11 +156,33 @@ inline bool spdlog::logger::should_log(spdlog::level::level_enum msg_level) cons
}
inline void spdlog::logger::stop()
{
_stop();
}
/* protected virtual */
inline void spdlog::logger::_log_msg(details::log_msg& msg)
{
_formatter->format(msg);
for (auto &sink : _sinks)
sink->log(msg);
}
inline void spdlog::logger::_set_pattern(const std::string& pattern)
{
_formatter = std::make_shared<pattern_formatter>(pattern);
}
inline void spdlog::logger::_set_formatter(formatter_ptr msg_formatter)
{
_formatter = msg_formatter;
}
inline void spdlog::logger::_stop()
{
set_level(level::OFF);
}
/* private functions */
inline void spdlog::logger::_variadic_log(spdlog::details::line_logger&) {}
template <typename Last>
@@ -181,10 +201,5 @@ inline void spdlog::logger::_variadic_log(spdlog::details::line_logger& l, const
_variadic_log(l, rest...);
}
inline void spdlog::logger::_log_msg(details::log_msg& msg)
{
_formatter->format(msg);
for (auto &sink : _sinks)
sink->log(msg);
}

View File

@@ -33,6 +33,7 @@
#include <unordered_map>
#include "../logger.h"
#include "../async_logger.h"
#include "../common.h"
namespace spdlog
@@ -58,8 +59,12 @@ public:
auto found = _loggers.find(logger_name);
if (found != _loggers.end())
return found->second;
std::shared_ptr<logger> new_logger;
if (_async_mode)
new_logger = std::make_shared<async_logger>(logger_name, sinks_begin, sinks_end, _async_q_size, _async_shutdown_duration);
else
new_logger = std::make_shared<logger>(logger_name, sinks_begin, sinks_end);
auto new_logger = std::make_shared<logger>(logger_name, sinks_begin, sinks_end);
if (_formatter)
new_logger->set_formatter(_formatter);
new_logger->set_level(_level);
@@ -102,7 +107,20 @@ public:
std::lock_guard<std::mutex> lock(_mutex);
for (auto& l : _loggers)
l.second->set_level(log_level);
}
void set_async_mode(size_t q_size, const log_clock::duration& shutdown_duration)
{
std::lock_guard<std::mutex> lock(_mutex);
_async_mode = true;
_async_q_size = q_size;
_async_shutdown_duration = shutdown_duration;
}
void set_sync_mode()
{
std::lock_guard<std::mutex> lock(_mutex);
_async_mode = false;
}
void stop_all()
@@ -128,6 +146,9 @@ private:
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;
log_clock::duration _async_shutdown_duration;
};
}
}

View File

@@ -128,6 +128,17 @@ inline void spdlog::set_level(level::level_enum log_level)
return details::registry::instance().set_level(log_level);
}
inline void spdlog::set_async_mode(size_t queue_size, const log_clock::duration& shutdown_duration)
{
details::registry::instance().set_async_mode(queue_size, shutdown_duration);
}
inline void spdlog::set_sync_mode()
{
details::registry::instance().set_sync_mode();
}
inline void spdlog::stop()
{
return details::registry::instance().stop_all();