mirror of
https://github.com/gabime/spdlog.git
synced 2024-12-25 01:51:38 +08:00
format
This commit is contained in:
parent
80f19d7136
commit
0d7a1d1ef9
@ -25,8 +25,8 @@ int main(int argc, char *argv[])
|
|||||||
thread_count = std::atoi(argv[1]);
|
thread_count = std::atoi(argv[1]);
|
||||||
|
|
||||||
int howmany = 1000000;
|
int howmany = 1000000;
|
||||||
spdlog::init_thread_pool (howmany, 1);
|
spdlog::init_thread_pool(howmany, 1);
|
||||||
|
|
||||||
auto logger = spdlog::create_async_logger<spdlog::sinks::simple_file_sink_mt>("file_logger", "logs/spdlog-bench-async.log", false);
|
auto logger = spdlog::create_async_logger<spdlog::sinks::simple_file_sink_mt>("file_logger", "logs/spdlog-bench-async.log", false);
|
||||||
logger->set_pattern("[%Y-%m-%d %T.%F]: %L %t %v");
|
logger->set_pattern("[%Y-%m-%d %T.%F]: %L %t %v");
|
||||||
|
|
||||||
|
@ -10,8 +10,8 @@
|
|||||||
#include <thread>
|
#include <thread>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "spdlog/spdlog.h"
|
|
||||||
#include "spdlog/sinks/simple_file_sink.h"
|
#include "spdlog/sinks/simple_file_sink.h"
|
||||||
|
#include "spdlog/spdlog.h"
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
@ -6,8 +6,8 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "spdlog/spdlog.h"
|
|
||||||
#include "spdlog/sinks/simple_file_sink.h"
|
#include "spdlog/sinks/simple_file_sink.h"
|
||||||
|
#include "spdlog/spdlog.h"
|
||||||
|
|
||||||
int main(int, char *[])
|
int main(int, char *[])
|
||||||
{
|
{
|
||||||
|
@ -82,7 +82,7 @@ int main(int, char *[])
|
|||||||
|
|
||||||
// Asynchronous logging is very fast..
|
// Asynchronous logging is very fast..
|
||||||
// Just call spdlog::set_async_mode(q_size) and all created loggers from now on will be asynchronous..
|
// Just call spdlog::set_async_mode(q_size) and all created loggers from now on will be asynchronous..
|
||||||
//async_example();
|
// async_example();
|
||||||
|
|
||||||
// Log user-defined types example
|
// Log user-defined types example
|
||||||
user_defined_example();
|
user_defined_example();
|
||||||
@ -107,23 +107,22 @@ int main(int, char *[])
|
|||||||
#include "spdlog/async.h"
|
#include "spdlog/async.h"
|
||||||
void async_example()
|
void async_example()
|
||||||
{
|
{
|
||||||
//auto async_file = spd::basic_logger_mt<spdlog::create_async>("async_file_logger", "logs/async_log.txt");
|
// auto async_file = spd::basic_logger_mt<spdlog::create_async>("async_file_logger", "logs/async_log.txt");
|
||||||
|
|
||||||
for (int j = 0; j < 1; j++)
|
for (int j = 0; j < 1; j++)
|
||||||
{
|
{
|
||||||
spdlog::init_thread_pool(1024, 10);
|
spdlog::init_thread_pool(1024, 10);
|
||||||
auto async_file = spd::stderr_color_mt<spdlog::create_async>("console");
|
auto async_file = spd::stderr_color_mt<spdlog::create_async>("console");
|
||||||
for (int i = 0; i < 1024; ++i)
|
for (int i = 0; i < 1024; ++i)
|
||||||
{
|
{
|
||||||
async_file->info("{} Async message #{}", j, i);
|
async_file->info("{} Async message #{}", j, i);
|
||||||
}
|
}
|
||||||
spdlog::drop_all();
|
spdlog::drop_all();
|
||||||
}
|
}
|
||||||
//std::this_thread::sleep_for(std::chrono::seconds(1));
|
// std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||||
|
|
||||||
|
|
||||||
// you can also modify thread pool settings *before* creating the logger:
|
// you can also modify thread pool settings *before* creating the logger:
|
||||||
// spdlog::init_thread_pool(32768, 4); // queue with max 32k items 4 backing threads.
|
// spdlog::init_thread_pool(32768, 4); // queue with max 32k items 4 backing threads.
|
||||||
}
|
}
|
||||||
|
|
||||||
// syslog example (linux/osx/freebsd)
|
// syslog example (linux/osx/freebsd)
|
||||||
|
@ -179,7 +179,13 @@ using filename_t = std::wstring;
|
|||||||
using filename_t = std::string;
|
using filename_t = std::string;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define SPDLOG_CATCH_AND_HANDLE \
|
||||||
#define SPDLOG_CATCH_AND_HANDLE catch (const std::exception &ex) {_err_handler(ex.what());}\
|
catch (const std::exception &ex) \
|
||||||
catch (...) {_err_handler("Unknown exeption in logger");}
|
{ \
|
||||||
|
_err_handler(ex.what()); \
|
||||||
|
} \
|
||||||
|
catch (...) \
|
||||||
|
{ \
|
||||||
|
_err_handler("Unknown exeption in logger"); \
|
||||||
|
}
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
@ -80,7 +80,7 @@ inline void spdlog::async_logger::_backend_log(details::log_msg &incoming_log_ms
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SPDLOG_CATCH_AND_HANDLE
|
SPDLOG_CATCH_AND_HANDLE
|
||||||
|
|
||||||
if (_should_flush(incoming_log_msg))
|
if (_should_flush(incoming_log_msg))
|
||||||
{
|
{
|
||||||
@ -97,5 +97,5 @@ inline void spdlog::async_logger::_backend_flush()
|
|||||||
sink->flush();
|
sink->flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SPDLOG_CATCH_AND_HANDLE
|
SPDLOG_CATCH_AND_HANDLE
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const char *fmt, const Ar
|
|||||||
#endif
|
#endif
|
||||||
_sink_it(log_msg);
|
_sink_it(log_msg);
|
||||||
}
|
}
|
||||||
SPDLOG_CATCH_AND_HANDLE
|
SPDLOG_CATCH_AND_HANDLE
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename... Args>
|
template<typename... Args>
|
||||||
@ -82,7 +82,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const char *msg)
|
|||||||
log_msg.raw << msg;
|
log_msg.raw << msg;
|
||||||
_sink_it(log_msg);
|
_sink_it(log_msg);
|
||||||
}
|
}
|
||||||
SPDLOG_CATCH_AND_HANDLE
|
SPDLOG_CATCH_AND_HANDLE
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
@ -98,7 +98,7 @@ inline void spdlog::logger::log(level::level_enum lvl, const T &msg)
|
|||||||
log_msg.raw << msg;
|
log_msg.raw << msg;
|
||||||
_sink_it(log_msg);
|
_sink_it(log_msg);
|
||||||
}
|
}
|
||||||
SPDLOG_CATCH_AND_HANDLE
|
SPDLOG_CATCH_AND_HANDLE
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename Arg1, typename... Args>
|
template<typename Arg1, typename... Args>
|
||||||
@ -257,11 +257,11 @@ inline spdlog::log_err_handler spdlog::logger::error_handler()
|
|||||||
|
|
||||||
inline void spdlog::logger::flush()
|
inline void spdlog::logger::flush()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
_flush();
|
_flush();
|
||||||
}
|
}
|
||||||
SPDLOG_CATCH_AND_HANDLE
|
SPDLOG_CATCH_AND_HANDLE
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void spdlog::logger::flush_on(level::level_enum log_level)
|
inline void spdlog::logger::flush_on(level::level_enum log_level)
|
||||||
|
@ -10,197 +10,195 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace spdlog {
|
namespace spdlog {
|
||||||
namespace details {
|
namespace details {
|
||||||
|
|
||||||
using async_logger_ptr = std::shared_ptr<spdlog::async_logger>;
|
using async_logger_ptr = std::shared_ptr<spdlog::async_logger>;
|
||||||
|
|
||||||
enum class async_msg_type
|
enum class async_msg_type
|
||||||
{
|
{
|
||||||
log,
|
log,
|
||||||
flush,
|
flush,
|
||||||
terminate
|
terminate
|
||||||
};
|
};
|
||||||
|
|
||||||
// Async msg to move to/from the queue
|
// Async msg to move to/from the queue
|
||||||
// Movable only. should never be copied
|
// Movable only. should never be copied
|
||||||
struct async_msg
|
struct async_msg
|
||||||
{
|
{
|
||||||
async_msg_type msg_type;
|
async_msg_type msg_type;
|
||||||
level::level_enum level;
|
level::level_enum level;
|
||||||
log_clock::time_point time;
|
log_clock::time_point time;
|
||||||
size_t thread_id;
|
size_t thread_id;
|
||||||
std::string txt;
|
std::string txt;
|
||||||
|
|
||||||
size_t msg_id;
|
size_t msg_id;
|
||||||
async_logger_ptr worker_ptr;
|
async_logger_ptr worker_ptr;
|
||||||
|
|
||||||
async_msg() = default;
|
async_msg() = default;
|
||||||
~async_msg() = default;
|
~async_msg() = default;
|
||||||
|
|
||||||
// should only be moved in or out of the queue..
|
// should only be moved in or out of the queue..
|
||||||
async_msg(const async_msg &) = delete;
|
async_msg(const async_msg &) = delete;
|
||||||
async_msg(async_msg &&other) = default;
|
async_msg(async_msg &&other) = default;
|
||||||
async_msg &operator=(async_msg &&other) = default;
|
async_msg &operator=(async_msg &&other) = default;
|
||||||
|
|
||||||
// construct from log_msg with given type
|
// construct from log_msg with given type
|
||||||
async_msg(async_logger_ptr &&worker, async_msg_type the_type, details::log_msg &&m)
|
async_msg(async_logger_ptr &&worker, async_msg_type the_type, details::log_msg &&m)
|
||||||
: msg_type(the_type)
|
: msg_type(the_type)
|
||||||
, level(m.level)
|
, level(m.level)
|
||||||
, time(m.time)
|
, time(m.time)
|
||||||
, thread_id(m.thread_id)
|
, thread_id(m.thread_id)
|
||||||
, txt(m.raw.data(), m.raw.size())
|
, txt(m.raw.data(), m.raw.size())
|
||||||
, msg_id(m.msg_id)
|
, msg_id(m.msg_id)
|
||||||
, worker_ptr(std::forward<async_logger_ptr>(worker))
|
, worker_ptr(std::forward<async_logger_ptr>(worker))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
async_msg(async_logger_ptr &&worker, async_msg_type the_type)
|
async_msg(async_logger_ptr &&worker, async_msg_type the_type)
|
||||||
: async_msg(std::forward<async_logger_ptr>(worker), the_type, details::log_msg())
|
: async_msg(std::forward<async_logger_ptr>(worker), the_type, details::log_msg())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
async_msg(async_msg_type the_type)
|
async_msg(async_msg_type the_type)
|
||||||
: async_msg(nullptr, the_type, details::log_msg())
|
: async_msg(nullptr, the_type, details::log_msg())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
// copy into log_msg
|
// copy into log_msg
|
||||||
void to_log_msg(log_msg &&msg)
|
void to_log_msg(log_msg &&msg)
|
||||||
{
|
{
|
||||||
msg.logger_name = &worker_ptr->name();
|
msg.logger_name = &worker_ptr->name();
|
||||||
msg.level = level;
|
msg.level = level;
|
||||||
msg.time = time;
|
msg.time = time;
|
||||||
msg.thread_id = thread_id;
|
msg.thread_id = thread_id;
|
||||||
msg.raw.clear();
|
msg.raw.clear();
|
||||||
msg.raw << txt;
|
msg.raw << txt;
|
||||||
msg.formatted.clear();
|
msg.formatted.clear();
|
||||||
msg.msg_id = msg_id;
|
msg.msg_id = msg_id;
|
||||||
msg.color_range_start = 0;
|
msg.color_range_start = 0;
|
||||||
msg.color_range_end = 0;
|
msg.color_range_end = 0;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class thread_pool
|
class thread_pool
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
using item_type = async_msg;
|
using item_type = async_msg;
|
||||||
using q_type = details::mpmc_blocking_queue<item_type>;
|
using q_type = details::mpmc_blocking_queue<item_type>;
|
||||||
using clock_type = std::chrono::steady_clock;
|
using clock_type = std::chrono::steady_clock;
|
||||||
|
|
||||||
thread_pool(size_t q_size_bytes, size_t threads_n)
|
thread_pool(size_t q_size_bytes, size_t threads_n)
|
||||||
: msg_counter_(0)
|
: msg_counter_(0)
|
||||||
, _q(q_size_bytes)
|
, _q(q_size_bytes)
|
||||||
{
|
{
|
||||||
// std::cout << "thread_pool() q_size_bytes: " << q_size_bytes << "\tthreads_n: " << threads_n << std::endl;
|
// std::cout << "thread_pool() q_size_bytes: " << q_size_bytes << "\tthreads_n: " << threads_n << std::endl;
|
||||||
if (threads_n == 0 || threads_n > 1000)
|
if (threads_n == 0 || threads_n > 1000)
|
||||||
{
|
{
|
||||||
throw spdlog_ex("spdlog::thread_pool(): invalid threads_n param (valid range is 1-1000)");
|
throw spdlog_ex("spdlog::thread_pool(): invalid threads_n param (valid range is 1-1000)");
|
||||||
}
|
}
|
||||||
for (size_t i = 0; i < threads_n; i++)
|
for (size_t i = 0; i < threads_n; i++)
|
||||||
{
|
{
|
||||||
_threads.emplace_back(std::bind(&thread_pool::worker_loop, this));
|
_threads.emplace_back(std::bind(&thread_pool::worker_loop, this));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// message all threads to terminate gracefully join them
|
// message all threads to terminate gracefully join them
|
||||||
~thread_pool()
|
~thread_pool()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < _threads.size(); i++)
|
for (size_t i = 0; i < _threads.size(); i++)
|
||||||
{
|
{
|
||||||
post_async_msg(async_msg(async_msg_type::terminate), async_overflow_policy::block_retry);
|
post_async_msg(async_msg(async_msg_type::terminate), async_overflow_policy::block_retry);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto &t : _threads)
|
for (auto &t : _threads)
|
||||||
{
|
{
|
||||||
t.join();
|
t.join();
|
||||||
}
|
}
|
||||||
// std::cout << "~thread_pool() msg_counter_: " << msg_counter_ << std::endl;
|
// std::cout << "~thread_pool() msg_counter_: " << msg_counter_ << std::endl;
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void post_log(async_logger_ptr &&worker_ptr, details::log_msg &&msg, async_overflow_policy overflow_policy)
|
void post_log(async_logger_ptr &&worker_ptr, details::log_msg &&msg, async_overflow_policy overflow_policy)
|
||||||
{
|
{
|
||||||
async_msg async_m(std::forward<async_logger_ptr>(worker_ptr), async_msg_type::log, std::forward<log_msg>(msg));
|
async_msg async_m(std::forward<async_logger_ptr>(worker_ptr), async_msg_type::log, std::forward<log_msg>(msg));
|
||||||
post_async_msg(std::move(async_m), overflow_policy);
|
post_async_msg(std::move(async_m), overflow_policy);
|
||||||
}
|
}
|
||||||
|
|
||||||
void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy)
|
void post_flush(async_logger_ptr &&worker_ptr, async_overflow_policy overflow_policy)
|
||||||
{
|
{
|
||||||
post_async_msg(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy);
|
post_async_msg(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t msg_counter()
|
size_t msg_counter()
|
||||||
{
|
{
|
||||||
return msg_counter_.load(std::memory_order_relaxed);
|
return msg_counter_.load(std::memory_order_relaxed);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::atomic<size_t> msg_counter_; // total # of messages processed in this pool
|
std::atomic<size_t> msg_counter_; // total # of messages processed in this pool
|
||||||
q_type _q;
|
q_type _q;
|
||||||
|
|
||||||
std::vector<std::thread> _threads;
|
std::vector<std::thread> _threads;
|
||||||
|
|
||||||
void post_async_msg(async_msg &&new_msg, async_overflow_policy overflow_policy)
|
void post_async_msg(async_msg &&new_msg, async_overflow_policy overflow_policy)
|
||||||
{
|
{
|
||||||
if (overflow_policy == async_overflow_policy::block_retry)
|
if (overflow_policy == async_overflow_policy::block_retry)
|
||||||
{
|
{
|
||||||
_q.enqueue(std::move(new_msg));
|
_q.enqueue(std::move(new_msg));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_q.enqueue_nowait(std::move(new_msg));
|
_q.enqueue_nowait(std::move(new_msg));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void worker_loop()
|
void worker_loop()
|
||||||
{
|
{
|
||||||
while (process_next_msg())
|
while (process_next_msg()) {};
|
||||||
{
|
}
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// process next message in the queue
|
// process next message in the queue
|
||||||
// return true if this thread should still be active (while no terminate msg was received)
|
// return true if this thread should still be active (while no terminate msg was received)
|
||||||
bool process_next_msg()
|
bool process_next_msg()
|
||||||
{
|
{
|
||||||
async_msg incoming_async_msg;
|
async_msg incoming_async_msg;
|
||||||
bool dequeued = _q.dequeue_for(incoming_async_msg, std::chrono::seconds(10));
|
bool dequeued = _q.dequeue_for(incoming_async_msg, std::chrono::seconds(10));
|
||||||
if (!dequeued)
|
if (!dequeued)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (incoming_async_msg.msg_type)
|
switch (incoming_async_msg.msg_type)
|
||||||
{
|
{
|
||||||
case async_msg_type::flush:
|
case async_msg_type::flush:
|
||||||
{
|
{
|
||||||
incoming_async_msg.worker_ptr->_backend_flush();
|
incoming_async_msg.worker_ptr->_backend_flush();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
case async_msg_type::terminate:
|
case async_msg_type::terminate:
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
log_msg msg;
|
log_msg msg;
|
||||||
incoming_async_msg.to_log_msg(std::move(msg));
|
incoming_async_msg.to_log_msg(std::move(msg));
|
||||||
incoming_async_msg.worker_ptr->_backend_log(msg);
|
incoming_async_msg.worker_ptr->_backend_log(msg);
|
||||||
msg_counter_.fetch_add(1, std::memory_order_relaxed);
|
msg_counter_.fetch_add(1, std::memory_order_relaxed);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert(false);
|
assert(false);
|
||||||
return true; // should not be reached
|
return true; // should not be reached
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace details
|
} // namespace details
|
||||||
} // namespace spdlog
|
} // namespace spdlog
|
||||||
|
@ -13,14 +13,14 @@ class failing_sink : public spdlog::sinks::sink
|
|||||||
}
|
}
|
||||||
|
|
||||||
void flush() override
|
void flush() override
|
||||||
{
|
{
|
||||||
throw std::runtime_error("some error happened during flush");
|
throw std::runtime_error("some error happened during flush");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST_CASE("default_error_handler", "[errors]]")
|
TEST_CASE("default_error_handler", "[errors]]")
|
||||||
{
|
{
|
||||||
spdlog::drop_all();
|
spdlog::drop_all();
|
||||||
prepare_logdir();
|
prepare_logdir();
|
||||||
std::string filename = "logs/simple_log.txt";
|
std::string filename = "logs/simple_log.txt";
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ struct custom_ex
|
|||||||
};
|
};
|
||||||
TEST_CASE("custom_error_handler", "[errors]]")
|
TEST_CASE("custom_error_handler", "[errors]]")
|
||||||
{
|
{
|
||||||
spdlog::drop_all();
|
spdlog::drop_all();
|
||||||
prepare_logdir();
|
prepare_logdir();
|
||||||
std::string filename = "logs/simple_log.txt";
|
std::string filename = "logs/simple_log.txt";
|
||||||
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true);
|
auto logger = spdlog::create<spdlog::sinks::simple_file_sink_mt>("logger", filename, true);
|
||||||
@ -62,7 +62,7 @@ TEST_CASE("custom_error_handler", "[errors]]")
|
|||||||
|
|
||||||
TEST_CASE("default_error_handler2", "[errors]]")
|
TEST_CASE("default_error_handler2", "[errors]]")
|
||||||
{
|
{
|
||||||
spdlog::drop_all();
|
spdlog::drop_all();
|
||||||
auto logger = spdlog::create<failing_sink>("failed_logger");
|
auto logger = spdlog::create<failing_sink>("failed_logger");
|
||||||
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
|
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
|
||||||
REQUIRE_THROWS_AS(logger->info("Some message"), custom_ex);
|
REQUIRE_THROWS_AS(logger->info("Some message"), custom_ex);
|
||||||
@ -70,15 +70,15 @@ TEST_CASE("default_error_handler2", "[errors]]")
|
|||||||
|
|
||||||
TEST_CASE("flush_error_handler", "[errors]]")
|
TEST_CASE("flush_error_handler", "[errors]]")
|
||||||
{
|
{
|
||||||
spdlog::drop_all();
|
spdlog::drop_all();
|
||||||
auto logger = spdlog::create<failing_sink>("failed_logger");
|
auto logger = spdlog::create<failing_sink>("failed_logger");
|
||||||
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
|
logger->set_error_handler([=](const std::string &) { throw custom_ex(); });
|
||||||
REQUIRE_THROWS_AS(logger->flush(), custom_ex);
|
REQUIRE_THROWS_AS(logger->flush(), custom_ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("async_error_handler", "[errors]]")
|
TEST_CASE("async_error_handler", "[errors]]")
|
||||||
{
|
{
|
||||||
spdlog::drop_all();
|
spdlog::drop_all();
|
||||||
prepare_logdir();
|
prepare_logdir();
|
||||||
std::string err_msg("log failed with some msg");
|
std::string err_msg("log failed with some msg");
|
||||||
|
|
||||||
@ -109,7 +109,7 @@ TEST_CASE("async_error_handler", "[errors]]")
|
|||||||
// Make sure async error handler is executed
|
// Make sure async error handler is executed
|
||||||
TEST_CASE("async_error_handler2", "[errors]]")
|
TEST_CASE("async_error_handler2", "[errors]]")
|
||||||
{
|
{
|
||||||
spdlog::drop_all();
|
spdlog::drop_all();
|
||||||
prepare_logdir();
|
prepare_logdir();
|
||||||
std::string err_msg("This is async handler error message");
|
std::string err_msg("This is async handler error message");
|
||||||
{
|
{
|
||||||
|
@ -50,7 +50,7 @@ TEST_CASE("flush_on", "[flush_on]]")
|
|||||||
TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
|
TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
|
||||||
{
|
{
|
||||||
prepare_logdir();
|
prepare_logdir();
|
||||||
size_t max_size = 1024 * 10;
|
size_t max_size = 1024 * 10;
|
||||||
std::string basename = "logs/rotating_log";
|
std::string basename = "logs/rotating_log";
|
||||||
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 0);
|
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 0);
|
||||||
|
|
||||||
@ -71,7 +71,7 @@ TEST_CASE("rotating_file_logger1", "[rotating_logger]]")
|
|||||||
TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
|
TEST_CASE("rotating_file_logger2", "[rotating_logger]]")
|
||||||
{
|
{
|
||||||
prepare_logdir();
|
prepare_logdir();
|
||||||
size_t max_size = 1024 * 10;
|
size_t max_size = 1024 * 10;
|
||||||
std::string basename = "logs/rotating_log";
|
std::string basename = "logs/rotating_log";
|
||||||
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 1);
|
auto logger = spdlog::rotating_logger_mt("logger", basename, max_size, 1);
|
||||||
for (int i = 0; i < 10; ++i)
|
for (int i = 0; i < 10; ++i)
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
#include "includes.h"
|
#include "includes.h"
|
||||||
#include "test_sink.h"
|
|
||||||
#include "spdlog/async.h"
|
#include "spdlog/async.h"
|
||||||
#include "spdlog/sinks/simple_file_sink.h"
|
#include "spdlog/sinks/simple_file_sink.h"
|
||||||
|
#include "test_sink.h"
|
||||||
|
|
||||||
//std::unique_ptr<spdlog::async_logger> create_logger(size_t tp_queue_size, size_t tp_threads)
|
// std::unique_ptr<spdlog::async_logger> create_logger(size_t tp_queue_size, size_t tp_threads)
|
||||||
//{
|
//{
|
||||||
// auto tp = std::make_shared<details::thread_pool>(8192, 1);
|
// auto tp = std::make_shared<details::thread_pool>(8192, 1);
|
||||||
// auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block_retry);
|
// auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block_retry);
|
||||||
@ -11,124 +11,122 @@
|
|||||||
|
|
||||||
TEST_CASE("basic async test ", "[async]")
|
TEST_CASE("basic async test ", "[async]")
|
||||||
{
|
{
|
||||||
using namespace spdlog;
|
using namespace spdlog;
|
||||||
auto test_sink = std::make_shared<sinks::test_sink_mt>();
|
auto test_sink = std::make_shared<sinks::test_sink_mt>();
|
||||||
size_t queue_size = 128;
|
size_t queue_size = 128;
|
||||||
size_t messages = 256;
|
size_t messages = 256;
|
||||||
{
|
{
|
||||||
auto tp = std::make_shared<details::thread_pool>(queue_size, 1);
|
auto tp = std::make_shared<details::thread_pool>(queue_size, 1);
|
||||||
auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block_retry);
|
auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block_retry);
|
||||||
for (size_t i = 0; i < messages; i++)
|
for (size_t i = 0; i < messages; i++)
|
||||||
{
|
{
|
||||||
logger->info("Hello message #{}", i);
|
logger->info("Hello message #{}", i);
|
||||||
}
|
}
|
||||||
logger->flush();
|
logger->flush();
|
||||||
}
|
}
|
||||||
REQUIRE(test_sink->msg_counter() == messages);
|
REQUIRE(test_sink->msg_counter() == messages);
|
||||||
REQUIRE(test_sink->flushed_msg_counter() == messages);
|
REQUIRE(test_sink->flushed_msg_counter() == messages);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("discard policy ", "[async]")
|
TEST_CASE("discard policy ", "[async]")
|
||||||
{
|
{
|
||||||
using namespace spdlog;
|
using namespace spdlog;
|
||||||
auto test_sink = std::make_shared<sinks::test_sink_mt>();
|
auto test_sink = std::make_shared<sinks::test_sink_mt>();
|
||||||
size_t queue_size = 2;
|
size_t queue_size = 2;
|
||||||
size_t messages = 1024;
|
size_t messages = 1024;
|
||||||
{
|
{
|
||||||
auto tp = std::make_shared<details::thread_pool>(queue_size, 1);
|
auto tp = std::make_shared<details::thread_pool>(queue_size, 1);
|
||||||
auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::discard_log_msg);
|
auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::discard_log_msg);
|
||||||
for (size_t i = 0; i < messages; i++)
|
for (size_t i = 0; i < messages; i++)
|
||||||
{
|
{
|
||||||
logger->info("Hello message #{}", i);
|
logger->info("Hello message #{}", i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
REQUIRE(test_sink->msg_counter() < messages);
|
REQUIRE(test_sink->msg_counter() < messages);
|
||||||
REQUIRE(test_sink->flushed_msg_counter() < messages);
|
REQUIRE(test_sink->flushed_msg_counter() < messages);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("flush", "[async]")
|
TEST_CASE("flush", "[async]")
|
||||||
{
|
{
|
||||||
using namespace spdlog;
|
using namespace spdlog;
|
||||||
auto test_sink = std::make_shared<sinks::test_sink_mt>();
|
auto test_sink = std::make_shared<sinks::test_sink_mt>();
|
||||||
size_t queue_size = 256;
|
size_t queue_size = 256;
|
||||||
size_t messages = 256;
|
size_t messages = 256;
|
||||||
{
|
{
|
||||||
auto tp = std::make_shared<details::thread_pool>(queue_size, 1);
|
auto tp = std::make_shared<details::thread_pool>(queue_size, 1);
|
||||||
auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block_retry);
|
auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block_retry);
|
||||||
for (size_t i = 0; i < messages; i++)
|
for (size_t i = 0; i < messages; i++)
|
||||||
{
|
{
|
||||||
logger->info("Hello message #{}", i);
|
logger->info("Hello message #{}", i);
|
||||||
}
|
}
|
||||||
|
|
||||||
logger->flush();
|
logger->flush();
|
||||||
}
|
}
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(250));
|
std::this_thread::sleep_for(std::chrono::milliseconds(250));
|
||||||
REQUIRE(test_sink->msg_counter() == messages);
|
REQUIRE(test_sink->msg_counter() == messages);
|
||||||
REQUIRE(test_sink->flushed_msg_counter() == messages);
|
REQUIRE(test_sink->flushed_msg_counter() == messages);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("multi threads", "[async]")
|
TEST_CASE("multi threads", "[async]")
|
||||||
{
|
{
|
||||||
using namespace spdlog;
|
using namespace spdlog;
|
||||||
auto test_sink = std::make_shared<sinks::test_sink_mt>();
|
auto test_sink = std::make_shared<sinks::test_sink_mt>();
|
||||||
size_t queue_size = 128;
|
size_t queue_size = 128;
|
||||||
size_t messages = 256;
|
size_t messages = 256;
|
||||||
size_t n_threads = 10;
|
size_t n_threads = 10;
|
||||||
{
|
{
|
||||||
auto tp = std::make_shared<details::thread_pool>(queue_size, 1);
|
auto tp = std::make_shared<details::thread_pool>(queue_size, 1);
|
||||||
auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block_retry);
|
auto logger = std::make_shared<async_logger>("as", test_sink, tp, async_overflow_policy::block_retry);
|
||||||
|
|
||||||
std::vector<std::thread> threads;
|
std::vector<std::thread> threads;
|
||||||
for (size_t i = 0; i < n_threads; i++)
|
for (size_t i = 0; i < n_threads; i++)
|
||||||
{
|
{
|
||||||
threads.emplace_back([logger, messages] {
|
threads.emplace_back([logger, messages] {
|
||||||
for (size_t j = 0; j < messages; j++)
|
for (size_t j = 0; j < messages; j++)
|
||||||
{
|
{
|
||||||
logger->info("Hello message #{}", j);
|
logger->info("Hello message #{}", j);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
});
|
}
|
||||||
}
|
|
||||||
|
|
||||||
for (auto &t : threads)
|
|
||||||
{
|
|
||||||
t.join();
|
|
||||||
}
|
|
||||||
logger->flush();
|
|
||||||
|
|
||||||
}
|
for (auto &t : threads)
|
||||||
|
{
|
||||||
REQUIRE(test_sink->msg_counter() == messages * n_threads);
|
t.join();
|
||||||
REQUIRE(test_sink->flushed_msg_counter() == messages * n_threads);
|
}
|
||||||
|
logger->flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
REQUIRE(test_sink->msg_counter() == messages * n_threads);
|
||||||
|
REQUIRE(test_sink->flushed_msg_counter() == messages * n_threads);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("to_file", "[async]")
|
TEST_CASE("to_file", "[async]")
|
||||||
{
|
{
|
||||||
prepare_logdir();
|
prepare_logdir();
|
||||||
size_t queue_size = 512;
|
size_t queue_size = 512;
|
||||||
size_t messages = 512;
|
size_t messages = 512;
|
||||||
size_t n_threads = 4;
|
size_t n_threads = 4;
|
||||||
spdlog::init_thread_pool(queue_size, n_threads);
|
spdlog::init_thread_pool(queue_size, n_threads);
|
||||||
auto logger= spdlog::basic_logger_mt<spdlog::create_async>("as", "logs/async_test.log", true);
|
auto logger = spdlog::basic_logger_mt<spdlog::create_async>("as", "logs/async_test.log", true);
|
||||||
|
|
||||||
std::vector<std::thread> threads;
|
|
||||||
for (size_t i = 0; i < n_threads; i++)
|
|
||||||
{
|
|
||||||
threads.emplace_back([logger, messages] {
|
|
||||||
for (size_t j = 0; j < messages; j++)
|
|
||||||
{
|
|
||||||
logger->info("Hello message #{}", j);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto &t : threads)
|
std::vector<std::thread> threads;
|
||||||
{
|
for (size_t i = 0; i < n_threads; i++)
|
||||||
t.join();
|
{
|
||||||
}
|
threads.emplace_back([logger, messages] {
|
||||||
logger.reset();
|
for (size_t j = 0; j < messages; j++)
|
||||||
spdlog::drop("as");
|
{
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
logger->info("Hello message #{}", j);
|
||||||
REQUIRE(count_lines("logs/async_test.log") == messages * n_threads);
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto &t : threads)
|
||||||
|
{
|
||||||
|
t.join();
|
||||||
|
}
|
||||||
|
logger.reset();
|
||||||
|
spdlog::drop("as");
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||||
|
REQUIRE(count_lines("logs/async_test.log") == messages * n_threads);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user