From 332b7c0d7f9d07922d09ffdcc02526bd187b0f0d Mon Sep 17 00:00:00 2001 From: gabime Date: Mon, 22 Aug 2016 22:09:23 +0300 Subject: [PATCH] astyle --- include/spdlog/async_logger.h | 154 +++++++++++----------- include/spdlog/details/async_log_helper.h | 25 ++-- tests/logs/daily_dateonly20160820.txt | 10 -- 3 files changed, 90 insertions(+), 99 deletions(-) delete mode 100644 tests/logs/daily_dateonly20160820.txt diff --git a/include/spdlog/async_logger.h b/include/spdlog/async_logger.h index f2e09f93..1c42fd9c 100644 --- a/include/spdlog/async_logger.h +++ b/include/spdlog/async_logger.h @@ -1,77 +1,77 @@ -// -// Copyright(c) 2015 Gabi Melman. -// Distributed under the MIT License (http://opensource.org/licenses/MIT) -// - -#pragma once - -// Very fast asynchronous logger (millions of logs per second on an average desktop) -// Uses pre allocated lockfree queue for maximum throughput even under large number of threads. -// Creates a single back thread to pop messages from the queue and log them. -// -// 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 (or block the caller until space is available in the queue) -// 3. will throw spdlog_ex upon log exceptions -// Upon destruction, logs all remaining messages in the queue before destructing.. - -#include -#include - -#include -#include -#include -#include - -namespace spdlog -{ - -namespace details -{ -class async_log_helper; -} - -class async_logger :public logger -{ -public: - template - async_logger(const std::string& name, - const It& begin, - const It& end, - size_t queue_size, - const async_overflow_policy overflow_policy = async_overflow_policy::block_retry, - const std::function& worker_warmup_cb = nullptr, - const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(), - const std::function& worker_teardown_cb = nullptr); - - async_logger(const std::string& logger_name, - sinks_init_list sinks, - size_t queue_size, - const async_overflow_policy overflow_policy = async_overflow_policy::block_retry, - const std::function& worker_warmup_cb = nullptr, - const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(), - const std::function& worker_teardown_cb = nullptr); - - async_logger(const std::string& logger_name, - sink_ptr single_sink, - size_t queue_size, - const async_overflow_policy overflow_policy = async_overflow_policy::block_retry, - const std::function& worker_warmup_cb = nullptr, - const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(), - const std::function& worker_teardown_cb = nullptr); - - //Wait for the queue to be empty, and flush synchronously - //Warning: this can potentialy last forever as we wait it to complete - void flush() override; -protected: - void _sink_it(details::log_msg& msg) override; - void _set_formatter(spdlog::formatter_ptr msg_formatter) override; - void _set_pattern(const std::string& pattern) override; - -private: - std::unique_ptr _async_log_helper; -}; -} - - -#include +// +// Copyright(c) 2015 Gabi Melman. +// Distributed under the MIT License (http://opensource.org/licenses/MIT) +// + +#pragma once + +// Very fast asynchronous logger (millions of logs per second on an average desktop) +// Uses pre allocated lockfree queue for maximum throughput even under large number of threads. +// Creates a single back thread to pop messages from the queue and log them. +// +// 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 (or block the caller until space is available in the queue) +// 3. will throw spdlog_ex upon log exceptions +// Upon destruction, logs all remaining messages in the queue before destructing.. + +#include +#include + +#include +#include +#include +#include + +namespace spdlog +{ + +namespace details +{ +class async_log_helper; +} + +class async_logger :public logger +{ +public: + template + async_logger(const std::string& name, + const It& begin, + const It& end, + size_t queue_size, + const async_overflow_policy overflow_policy = async_overflow_policy::block_retry, + const std::function& worker_warmup_cb = nullptr, + const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(), + const std::function& worker_teardown_cb = nullptr); + + async_logger(const std::string& logger_name, + sinks_init_list sinks, + size_t queue_size, + const async_overflow_policy overflow_policy = async_overflow_policy::block_retry, + const std::function& worker_warmup_cb = nullptr, + const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(), + const std::function& worker_teardown_cb = nullptr); + + async_logger(const std::string& logger_name, + sink_ptr single_sink, + size_t queue_size, + const async_overflow_policy overflow_policy = async_overflow_policy::block_retry, + const std::function& worker_warmup_cb = nullptr, + const std::chrono::milliseconds& flush_interval_ms = std::chrono::milliseconds::zero(), + const std::function& worker_teardown_cb = nullptr); + + //Wait for the queue to be empty, and flush synchronously + //Warning: this can potentialy last forever as we wait it to complete + void flush() override; +protected: + void _sink_it(details::log_msg& msg) override; + void _set_formatter(spdlog::formatter_ptr msg_formatter) override; + void _set_pattern(const std::string& pattern) override; + +private: + std::unique_ptr _async_log_helper; +}; +} + + +#include diff --git a/include/spdlog/details/async_log_helper.h b/include/spdlog/details/async_log_helper.h index 42fbeba8..347b2037 100644 --- a/include/spdlog/details/async_log_helper.h +++ b/include/spdlog/details/async_log_helper.h @@ -77,8 +77,8 @@ async_msg(async_msg&& other) SPDLOG_NOEXCEPT: thread_id = other.thread_id; txt = std::move(other.txt); msg_type = other.msg_type; - return *this; - } + return *this; + } // never copy or assign. should only be moved.. async_msg(const async_msg&) = delete; @@ -179,8 +179,8 @@ private: // sleep,yield or return immediatly using the time passed since last message as a hint static void sleep_or_yield(const spdlog::log_clock::time_point& now, const log_clock::time_point& last_op_time); - // wait until the queue is empty - void wait_empty_q(); + // wait until the queue is empty + void wait_empty_q(); }; } @@ -252,9 +252,9 @@ inline void spdlog::details::async_log_helper::push_msg(details::async_log_helpe //wait for the queue be empty and request flush from its sinks inline void spdlog::details::async_log_helper::flush() { - wait_empty_q(); + wait_empty_q(); push_msg(async_msg(async_msg_type::flush)); - wait_empty_q(); //make sure the above flush message was processed + wait_empty_q(); //make sure the above flush message was processed } inline void spdlog::details::async_log_helper::worker_loop() @@ -304,7 +304,7 @@ inline bool spdlog::details::async_log_helper::process_next_msg(log_clock::time_ incoming_async_msg.fill_log_msg(incoming_log_msg); _formatter->format(incoming_log_msg); for (auto &s : _sinks) - s->log(incoming_log_msg); + s->log(incoming_log_msg); } return true; } @@ -369,11 +369,12 @@ inline void spdlog::details::async_log_helper::sleep_or_yield(const spdlog::log_ // wait for the queue to be empty inline void spdlog::details::async_log_helper::wait_empty_q() { - auto last_op = details::os::now(); - while (_q.approx_size() > 0) { - sleep_or_yield(details::os::now(), last_op); - } - + auto last_op = details::os::now(); + while (_q.approx_size() > 0) + { + sleep_or_yield(details::os::now(), last_op); + } + } diff --git a/tests/logs/daily_dateonly20160820.txt b/tests/logs/daily_dateonly20160820.txt deleted file mode 100644 index 45a997ed..00000000 --- a/tests/logs/daily_dateonly20160820.txt +++ /dev/null @@ -1,10 +0,0 @@ -[2016-08-20 13:50:56.499] [logger] [info] Test message 0 -[2016-08-20 13:50:56.499] [logger] [info] Test message 1 -[2016-08-20 13:50:56.499] [logger] [info] Test message 2 -[2016-08-20 13:50:56.499] [logger] [info] Test message 3 -[2016-08-20 13:50:56.499] [logger] [info] Test message 4 -[2016-08-20 13:50:56.499] [logger] [info] Test message 5 -[2016-08-20 13:50:56.500] [logger] [info] Test message 6 -[2016-08-20 13:50:56.500] [logger] [info] Test message 7 -[2016-08-20 13:50:56.500] [logger] [info] Test message 8 -[2016-08-20 13:50:56.500] [logger] [info] Test message 9