New async_logger class and API
This commit is contained in:
parent
e88a46691a
commit
4cb6aa90b2
@ -31,8 +31,8 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include "spdlog/spdlog.h"
|
#include "spdlog/spdlog.h"
|
||||||
|
#include "spdlog/async_logger.h"
|
||||||
#include "spdlog/sinks/file_sinks.h"
|
#include "spdlog/sinks/file_sinks.h"
|
||||||
#include "spdlog/sinks/async_sink.h"
|
|
||||||
#include "spdlog/sinks/null_sink.h"
|
#include "spdlog/sinks/null_sink.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
@ -50,8 +50,8 @@ void bench_mt(int howmany, std::shared_ptr<spdlog::logger> log, int thread_count
|
|||||||
int main(int argc, char* argv[])
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
|
|
||||||
int howmany = 250000;
|
int howmany = 1000000;
|
||||||
int threads = 4;
|
int threads = 10;
|
||||||
int flush_interval = 1000;
|
int flush_interval = 1000;
|
||||||
int file_size = 30 * 1024 * 1024;
|
int file_size = 30 * 1024 * 1024;
|
||||||
int rotating_files = 5;
|
int rotating_files = 5;
|
||||||
@ -70,11 +70,8 @@ int main(int argc, char* argv[])
|
|||||||
|
|
||||||
auto rotating_st = spdlog::rotating_logger_st("rotating_st", "logs/rotating_st", file_size, rotating_files, flush_interval);
|
auto rotating_st = spdlog::rotating_logger_st("rotating_st", "logs/rotating_st", file_size, rotating_files, flush_interval);
|
||||||
bench(howmany, rotating_st);
|
bench(howmany, rotating_st);
|
||||||
|
|
||||||
|
|
||||||
auto daily_st = spdlog::daily_logger_st("daily_st", "logs/daily_st", flush_interval);
|
auto daily_st = spdlog::daily_logger_st("daily_st", "logs/daily_st", flush_interval);
|
||||||
bench(howmany, daily_st);
|
bench(howmany, daily_st);
|
||||||
|
|
||||||
bench(howmany, spdlog::create<null_sink_st>("null_st"));
|
bench(howmany, spdlog::create<null_sink_st>("null_st"));
|
||||||
|
|
||||||
cout << "\n*******************************************************************************\n";
|
cout << "\n*******************************************************************************\n";
|
||||||
@ -87,8 +84,25 @@ int main(int argc, char* argv[])
|
|||||||
|
|
||||||
auto daily_mt = spdlog::daily_logger_mt("daily_mt", "logs/daily_mt", flush_interval);
|
auto daily_mt = spdlog::daily_logger_mt("daily_mt", "logs/daily_mt", flush_interval);
|
||||||
bench_mt(howmany, daily_mt, threads);
|
bench_mt(howmany, daily_mt, threads);
|
||||||
|
bench(howmany, spdlog::create<null_sink_st>("null_mt"));
|
||||||
|
|
||||||
|
|
||||||
|
cout << "\n*******************************************************************************\n";
|
||||||
|
cout << "async logging.. " << threads << " threads sharing same logger, " << format(howmany) << " iterations, flush every " << flush_interval << " lines" << endl;
|
||||||
|
cout << "*******************************************************************************\n";
|
||||||
|
|
||||||
|
spdlog::set_async_mode(howmany);
|
||||||
|
auto rotating_st_async = spdlog::rotating_logger_st("rotating_st_Async", "logs/rotating_st_async", file_size, rotating_files, flush_interval);
|
||||||
|
bench(howmany, rotating_st_async);
|
||||||
|
auto daily_st_async = spdlog::daily_logger_st("daily_st_async", "logs/daily_st_async", flush_interval);
|
||||||
|
bench(howmany, daily_st_async);
|
||||||
|
bench(howmany, spdlog::create<null_sink_st>("null_st_async"));
|
||||||
|
|
||||||
|
|
||||||
|
spdlog::stop();
|
||||||
|
cin.ignore();
|
||||||
|
|
||||||
|
|
||||||
bench_mt(howmany, spdlog::create<null_sink_mt>("null_mt"), threads);
|
|
||||||
}
|
}
|
||||||
catch (std::exception &ex)
|
catch (std::exception &ex)
|
||||||
{
|
{
|
||||||
|
@ -29,7 +29,7 @@
|
|||||||
#include "spdlog/spdlog.h"
|
#include "spdlog/spdlog.h"
|
||||||
|
|
||||||
|
|
||||||
int main(int, char* [])
|
int main_(int, char* [])
|
||||||
{
|
{
|
||||||
|
|
||||||
namespace spd = spdlog;
|
namespace spd = spdlog;
|
||||||
@ -63,7 +63,19 @@ int main(int, char* [])
|
|||||||
|
|
||||||
SPDLOG_TRACE(file_logger, "This is a trace message (only #ifdef _DEBUG)", 123);
|
SPDLOG_TRACE(file_logger, "This is a trace message (only #ifdef _DEBUG)", 123);
|
||||||
|
|
||||||
|
//
|
||||||
|
// Asynchronous logging is easy..
|
||||||
|
// Just call spdlog::set_async_mode(max_q_size) and all created loggers from now on will be asynchronous..
|
||||||
|
//
|
||||||
|
|
||||||
|
size_t max_q_size = 100000;
|
||||||
|
spdlog::set_async_mode(max_q_size);
|
||||||
|
auto async_file= spd::daily_logger_st("async_file_logger", "async_" + filename);
|
||||||
|
async_file->info() << "This is async log.." << "Should be very fast!";
|
||||||
|
|
||||||
|
//
|
||||||
|
// syslog example
|
||||||
|
//
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
auto syslog_logger = spd::syslog_logger("syslog");
|
auto syslog_logger = spd::syslog_logger("syslog");
|
||||||
syslog_logger->warn("This is warning that will end up in syslog. This is Linux only!");
|
syslog_logger->warn("This is warning that will end up in syslog. This is Linux only!");
|
||||||
|
66
include/spdlog/async_logger.h
Normal file
66
include/spdlog/async_logger.h
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
/*************************************************************************/
|
||||||
|
/* 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
|
||||||
|
|
||||||
|
// Async logger
|
||||||
|
// Upon each log write the logger:
|
||||||
|
// 1. Checks if its log level is enough to log the message
|
||||||
|
// 2. Push a new copy of the message to a queue (uses sinks::async_sink for this)
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
#include "common.h"
|
||||||
|
#include "logger.h"
|
||||||
|
|
||||||
|
|
||||||
|
namespace spdlog
|
||||||
|
{
|
||||||
|
|
||||||
|
namespace sinks {
|
||||||
|
class async_sink;
|
||||||
|
}
|
||||||
|
|
||||||
|
class async_logger :public logger
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
template<class It>
|
||||||
|
async_logger(const std::string& name, const It& begin, const It& end, size_t queue_size, const log_clock::duration& shutdown_duration);
|
||||||
|
async_logger(const std::string& logger_name, sinks_init_list sinks, size_t queue_size, const log_clock::duration& shutdown_duration);
|
||||||
|
async_logger(const std::string& logger_name, sink_ptr single_sink, size_t queue_size, const log_clock::duration& shutdown_duration);
|
||||||
|
|
||||||
|
|
||||||
|
protected:
|
||||||
|
void _log_msg(details::log_msg& msg) override;
|
||||||
|
void _set_formatter(spdlog::formatter_ptr msg_formatter) override;
|
||||||
|
void _set_pattern(const std::string& pattern) override;
|
||||||
|
void _stop() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unique_ptr<sinks::async_sink> _as;
|
||||||
|
log_clock::duration _shutdown_duration;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#include "./details/async_logger_impl.h"
|
78
include/spdlog/details/async_logger_impl.h
Normal file
78
include/spdlog/details/async_logger_impl.h
Normal 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);
|
||||||
|
}
|
@ -58,7 +58,19 @@ struct log_msg
|
|||||||
raw(std::move(other.raw)),
|
raw(std::move(other.raw)),
|
||||||
formatted(std::move(other.formatted)) {}
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@
|
|||||||
#include "./line_logger.h"
|
#include "./line_logger.h"
|
||||||
|
|
||||||
|
|
||||||
|
/* public functions */
|
||||||
template<class It>
|
template<class It>
|
||||||
inline spdlog::logger::logger(const std::string& logger_name, const It& begin, const It& end) :
|
inline spdlog::logger::logger(const std::string& logger_name, const It& begin, const It& end) :
|
||||||
_name(logger_name),
|
_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)
|
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)
|
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>
|
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()
|
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);
|
set_level(level::OFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* private functions */
|
||||||
inline void spdlog::logger::_variadic_log(spdlog::details::line_logger&) {}
|
inline void spdlog::logger::_variadic_log(spdlog::details::line_logger&) {}
|
||||||
|
|
||||||
template <typename Last>
|
template <typename Last>
|
||||||
@ -181,10 +201,5 @@ inline void spdlog::logger::_variadic_log(spdlog::details::line_logger& l, const
|
|||||||
_variadic_log(l, rest...);
|
_variadic_log(l, rest...);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void spdlog::logger::_log_msg(details::log_msg& msg)
|
|
||||||
{
|
|
||||||
_formatter->format(msg);
|
|
||||||
for (auto &sink : _sinks)
|
|
||||||
sink->log(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@ -33,6 +33,7 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
#include "../logger.h"
|
#include "../logger.h"
|
||||||
|
#include "../async_logger.h"
|
||||||
#include "../common.h"
|
#include "../common.h"
|
||||||
|
|
||||||
namespace spdlog
|
namespace spdlog
|
||||||
@ -58,8 +59,12 @@ public:
|
|||||||
auto found = _loggers.find(logger_name);
|
auto found = _loggers.find(logger_name);
|
||||||
if (found != _loggers.end())
|
if (found != _loggers.end())
|
||||||
return found->second;
|
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)
|
if (_formatter)
|
||||||
new_logger->set_formatter(_formatter);
|
new_logger->set_formatter(_formatter);
|
||||||
new_logger->set_level(_level);
|
new_logger->set_level(_level);
|
||||||
@ -102,7 +107,20 @@ public:
|
|||||||
std::lock_guard<std::mutex> lock(_mutex);
|
std::lock_guard<std::mutex> lock(_mutex);
|
||||||
for (auto& l : _loggers)
|
for (auto& l : _loggers)
|
||||||
l.second->set_level(log_level);
|
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()
|
void stop_all()
|
||||||
@ -128,6 +146,9 @@ private:
|
|||||||
std::unordered_map <std::string, std::shared_ptr<logger>> _loggers;
|
std::unordered_map <std::string, std::shared_ptr<logger>> _loggers;
|
||||||
formatter_ptr _formatter;
|
formatter_ptr _formatter;
|
||||||
level::level_enum _level = level::INFO;
|
level::level_enum _level = level::INFO;
|
||||||
|
bool _async_mode = false;
|
||||||
|
size_t _async_q_size = 0;
|
||||||
|
log_clock::duration _async_shutdown_duration;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -128,6 +128,17 @@ inline void spdlog::set_level(level::level_enum log_level)
|
|||||||
return details::registry::instance().set_level(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()
|
inline void spdlog::stop()
|
||||||
{
|
{
|
||||||
return details::registry::instance().stop_all();
|
return details::registry::instance().stop_all();
|
||||||
|
@ -34,6 +34,7 @@
|
|||||||
#include<vector>
|
#include<vector>
|
||||||
#include<memory>
|
#include<memory>
|
||||||
#include "sinks/base_sink.h"
|
#include "sinks/base_sink.h"
|
||||||
|
#include "sinks/async_sink.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
namespace spdlog
|
namespace spdlog
|
||||||
@ -47,17 +48,12 @@ class line_logger;
|
|||||||
class logger
|
class logger
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
logger(const std::string& logger_name, sink_ptr single_sink);
|
logger(const std::string& logger_name, sink_ptr single_sink);
|
||||||
logger(const std::string& name, sinks_init_list);
|
logger(const std::string& name, sinks_init_list);
|
||||||
template<class It>
|
template<class It>
|
||||||
logger(const std::string& name, const It& begin, const It& end);
|
logger(const std::string& name, const It& begin, const It& end);
|
||||||
|
|
||||||
void set_pattern(const std::string&);
|
virtual ~logger();
|
||||||
void set_formatter(formatter_ptr);
|
|
||||||
formatter_ptr get_formatter() const;
|
|
||||||
|
|
||||||
|
|
||||||
logger(const logger&) = delete;
|
logger(const logger&) = delete;
|
||||||
logger& operator=(const logger&) = delete;
|
logger& operator=(const logger&) = delete;
|
||||||
|
|
||||||
@ -83,18 +79,30 @@ public:
|
|||||||
template <typename... Args> details::line_logger emerg(const Args&... args);
|
template <typename... Args> details::line_logger emerg(const Args&... args);
|
||||||
|
|
||||||
|
|
||||||
private:
|
void set_pattern(const std::string&);
|
||||||
|
void set_formatter(formatter_ptr);
|
||||||
|
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void _log_msg(details::log_msg& msg);
|
||||||
|
virtual void _set_pattern(const std::string&);
|
||||||
|
virtual void _set_formatter(formatter_ptr);
|
||||||
|
virtual void _stop();
|
||||||
|
|
||||||
|
|
||||||
friend details::line_logger;
|
friend details::line_logger;
|
||||||
std::string _name;
|
std::string _name;
|
||||||
std::vector<sink_ptr> _sinks;
|
std::vector<sink_ptr> _sinks;
|
||||||
formatter_ptr _formatter;
|
formatter_ptr _formatter;
|
||||||
std::atomic_int _level;
|
std::atomic_int _level;
|
||||||
|
|
||||||
|
private:
|
||||||
void _variadic_log(details::line_logger& l);
|
void _variadic_log(details::line_logger& l);
|
||||||
template <typename Last>
|
template <typename Last>
|
||||||
inline void _variadic_log(spdlog::details::line_logger& l, const Last& last);
|
inline void _variadic_log(spdlog::details::line_logger& l, const Last& last);
|
||||||
template <typename First, typename... Rest>
|
template <typename First, typename... Rest>
|
||||||
void _variadic_log(details::line_logger&l, const First& first, const Rest&... rest);
|
void _variadic_log(details::line_logger&l, const First& first, const Rest&... rest);
|
||||||
void _log_msg(details::log_msg& msg);
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,7 +35,6 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
#include "./base_sink.h"
|
#include "./base_sink.h"
|
||||||
#include "../logger.h"
|
#include "../logger.h"
|
||||||
@ -43,14 +42,14 @@
|
|||||||
#include "../details/null_mutex.h"
|
#include "../details/null_mutex.h"
|
||||||
#include "../details/log_msg.h"
|
#include "../details/log_msg.h"
|
||||||
|
|
||||||
#include<iostream>
|
|
||||||
|
|
||||||
namespace spdlog
|
namespace spdlog
|
||||||
{
|
{
|
||||||
namespace sinks
|
namespace sinks
|
||||||
{
|
{
|
||||||
|
|
||||||
class async_sink : public base_sink < details::null_mutex >
|
|
||||||
|
class async_sink : public base_sink < details::null_mutex > //single worker thread so null_mutex
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using q_type = details::blocking_queue < std::unique_ptr<details::log_msg> > ;
|
using q_type = details::blocking_queue < std::unique_ptr<details::log_msg> > ;
|
||||||
@ -61,9 +60,9 @@ public:
|
|||||||
~async_sink();
|
~async_sink();
|
||||||
void add_sink(sink_ptr sink);
|
void add_sink(sink_ptr sink);
|
||||||
void remove_sink(sink_ptr sink_ptr);
|
void remove_sink(sink_ptr sink_ptr);
|
||||||
q_type& q();
|
void set_formatter(formatter_ptr);
|
||||||
//Wait to remaining items (if any) in the queue to be written and shutdown
|
//Wait to remaining items (if any) in the queue to be written and shutdown
|
||||||
void shutdown(const std::chrono::milliseconds& timeout);
|
void shutdown(const log_clock::duration& timeout);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -80,6 +79,7 @@ private:
|
|||||||
//Last exception thrown from the back thread
|
//Last exception thrown from the back thread
|
||||||
std::shared_ptr<spdlog_ex> _last_backthread_ex;
|
std::shared_ptr<spdlog_ex> _last_backthread_ex;
|
||||||
|
|
||||||
|
formatter_ptr _formatter;
|
||||||
|
|
||||||
//will throw last back thread exception or if backthread no active
|
//will throw last back thread exception or if backthread no active
|
||||||
void _push_sentry();
|
void _push_sentry();
|
||||||
@ -124,13 +124,13 @@ inline void spdlog::sinks::async_sink::_thread_loop()
|
|||||||
{
|
{
|
||||||
if (!_active)
|
if (!_active)
|
||||||
return;
|
return;
|
||||||
|
_formatter->format(*msg);
|
||||||
for (auto &s : _sinks)
|
for (auto &s : _sinks)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
s->log(*msg);
|
s->log(*msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
catch (const std::exception& ex)
|
catch (const std::exception& ex)
|
||||||
{
|
{
|
||||||
_last_backthread_ex = std::make_shared<spdlog_ex>(ex.what());
|
_last_backthread_ex = std::make_shared<spdlog_ex>(ex.what());
|
||||||
@ -139,7 +139,6 @@ inline void spdlog::sinks::async_sink::_thread_loop()
|
|||||||
{
|
{
|
||||||
_last_backthread_ex = std::make_shared<spdlog_ex>("Unknown exception");
|
_last_backthread_ex = std::make_shared<spdlog_ex>("Unknown exception");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -159,13 +158,14 @@ inline void spdlog::sinks::async_sink::remove_sink(spdlog::sink_ptr s)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline spdlog::sinks::async_sink::q_type& spdlog::sinks::async_sink::q()
|
inline void spdlog::sinks::async_sink::set_formatter(formatter_ptr msg_formatter)
|
||||||
{
|
{
|
||||||
return _q;
|
_formatter = msg_formatter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
inline void spdlog::sinks::async_sink::shutdown(const std::chrono::milliseconds& timeout)
|
|
||||||
|
inline void spdlog::sinks::async_sink::shutdown(const log_clock::duration& timeout)
|
||||||
{
|
{
|
||||||
if (timeout > std::chrono::milliseconds::zero())
|
if (timeout > std::chrono::milliseconds::zero())
|
||||||
{
|
{
|
||||||
|
@ -43,37 +43,59 @@ namespace spdlog
|
|||||||
std::shared_ptr<logger> get(const std::string& name);
|
std::shared_ptr<logger> get(const std::string& name);
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
// Set global formatting
|
// Set global formatting
|
||||||
// spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %v");
|
// e.g. spdlog::set_pattern("%Y-%m-%d %H:%M:%S.%e %l : %v");
|
||||||
|
//
|
||||||
void set_pattern(const std::string& format_string);
|
void set_pattern(const std::string& format_string);
|
||||||
|
void set_formatter(formatter_ptr f);
|
||||||
|
|
||||||
|
//
|
||||||
//Set global logging level
|
// Set global logging level
|
||||||
|
//
|
||||||
void set_level(level::level_enum log_level);
|
void set_level(level::level_enum log_level);
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
|
// Async mode - off by default.
|
||||||
|
//
|
||||||
|
|
||||||
|
// Turn on async mode and set the queue size for each async_logger
|
||||||
|
// shutdown_duration indicates max time to wait for the worker thread to log its messages before terminating.
|
||||||
|
|
||||||
|
void set_async_mode(size_t queue_size, const log_clock::duration& shutdown_duration = std::chrono::seconds(5));
|
||||||
|
// Turn off async mode
|
||||||
|
void set_sync_mode();
|
||||||
|
|
||||||
|
//
|
||||||
// Create multi/single threaded rotating file logger
|
// Create multi/single threaded rotating file logger
|
||||||
|
//
|
||||||
std::shared_ptr<logger> rotating_logger_mt(const std::string& logger_name, const std::string& filename, size_t max_file_size, size_t max_files, size_t flush_inverval = 0);
|
std::shared_ptr<logger> rotating_logger_mt(const std::string& logger_name, const std::string& filename, size_t max_file_size, size_t max_files, size_t flush_inverval = 0);
|
||||||
std::shared_ptr<logger> rotating_logger_st(const std::string& logger_name, const std::string& filename, size_t max_file_size, size_t max_files, size_t flush_inverval = 0);
|
std::shared_ptr<logger> rotating_logger_st(const std::string& logger_name, const std::string& filename, size_t max_file_size, size_t max_files, size_t flush_inverval = 0);
|
||||||
|
|
||||||
|
//
|
||||||
// Create file logger which creates new file at midnight):
|
// Create file logger which creates new file at midnight):
|
||||||
|
//
|
||||||
std::shared_ptr<logger> daily_logger_mt(const std::string& logger_name, const std::string& filename, size_t flush_inverval = 0);
|
std::shared_ptr<logger> daily_logger_mt(const std::string& logger_name, const std::string& filename, size_t flush_inverval = 0);
|
||||||
std::shared_ptr<logger> daily_logger_st(const std::string& logger_name, const std::string& filename, size_t flush_inverval = 0);
|
std::shared_ptr<logger> daily_logger_st(const std::string& logger_name, const std::string& filename, size_t flush_inverval = 0);
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
// Create stdout/stderr loggers
|
// Create stdout/stderr loggers
|
||||||
|
//
|
||||||
std::shared_ptr<logger> stdout_logger_mt(const std::string& logger_name);
|
std::shared_ptr<logger> stdout_logger_mt(const std::string& logger_name);
|
||||||
std::shared_ptr<logger> stdout_logger_st(const std::string& logger_name);
|
std::shared_ptr<logger> stdout_logger_st(const std::string& logger_name);
|
||||||
std::shared_ptr<logger> stderr_logger_mt(const std::string& logger_name);
|
std::shared_ptr<logger> stderr_logger_mt(const std::string& logger_name);
|
||||||
std::shared_ptr<logger> stderr_logger_st(const std::string& logger_name);
|
std::shared_ptr<logger> stderr_logger_st(const std::string& logger_name);
|
||||||
|
|
||||||
|
|
||||||
|
//
|
||||||
// Create a syslog logger
|
// Create a syslog logger
|
||||||
|
//
|
||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
std::shared_ptr<logger> syslog_logger(const std::string& logger_name);
|
std::shared_ptr<logger> syslog_logger(const std::string& logger_name);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Create a logger with multiple sinks
|
// Create a logger with multiple sinks
|
||||||
//
|
//
|
||||||
@ -90,8 +112,6 @@ std::shared_ptr<spdlog::logger> create(const std::string& logger_name, const Arg
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Set global formatter object
|
|
||||||
void set_formatter(formatter_ptr f);
|
|
||||||
|
|
||||||
// Stop logging by setting all the loggers to log level OFF
|
// Stop logging by setting all the loggers to log level OFF
|
||||||
void stop();
|
void stop();
|
||||||
|
Loading…
Reference in New Issue
Block a user