|
|
|
@ -19,22 +19,22 @@
|
|
|
|
|
#include "../formatter.h"
|
|
|
|
|
#include "../sinks/sink.h"
|
|
|
|
|
|
|
|
|
|
#include<iostream>
|
|
|
|
|
#include <chrono>
|
|
|
|
|
#include <condition_variable>
|
|
|
|
|
#include <exception>
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <thread>
|
|
|
|
|
#include <utility>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <condition_variable>
|
|
|
|
|
|
|
|
|
|
namespace spdlog {
|
|
|
|
|
namespace details {
|
|
|
|
|
namespace details {
|
|
|
|
|
|
|
|
|
|
class async_log_helper
|
|
|
|
|
{
|
|
|
|
|
class async_log_helper
|
|
|
|
|
{
|
|
|
|
|
// Async msg to move to/from the queue
|
|
|
|
|
// Movable only. should never be copied
|
|
|
|
|
enum class async_msg_type
|
|
|
|
@ -117,9 +117,9 @@ namespace spdlog {
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
public:
|
|
|
|
|
using item_type = async_msg;
|
|
|
|
|
using q_type = details::mpmc_bounded_queue <item_type>;
|
|
|
|
|
using q_type = details::mpmc_bounded_queue<item_type>;
|
|
|
|
|
|
|
|
|
|
using clock = std::chrono::steady_clock;
|
|
|
|
|
|
|
|
|
@ -142,7 +142,7 @@ namespace spdlog {
|
|
|
|
|
|
|
|
|
|
void set_error_handler(spdlog::log_err_handler err_handler);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
private:
|
|
|
|
|
formatter_ptr _formatter;
|
|
|
|
|
std::vector<std::shared_ptr<sinks::sink>> _sinks;
|
|
|
|
|
|
|
|
|
@ -166,7 +166,7 @@ namespace spdlog {
|
|
|
|
|
const std::function<void()> _worker_teardown_cb;
|
|
|
|
|
|
|
|
|
|
std::mutex null_mutex_;
|
|
|
|
|
//null_mutex null_mutex_;
|
|
|
|
|
// null_mutex null_mutex_;
|
|
|
|
|
std::condition_variable_any not_empty_cv_;
|
|
|
|
|
std::condition_variable_any not_full_cv_;
|
|
|
|
|
|
|
|
|
@ -185,9 +185,8 @@ namespace spdlog {
|
|
|
|
|
void handle_flush_interval();
|
|
|
|
|
|
|
|
|
|
void flush_sinks();
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
} // namespace details
|
|
|
|
|
};
|
|
|
|
|
} // namespace details
|
|
|
|
|
} // namespace spdlog
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
@ -241,7 +240,6 @@ inline void spdlog::details::async_log_helper::enqueue_msg(details::async_log_he
|
|
|
|
|
{
|
|
|
|
|
_q.enqueue_nowait(std::move(new_msg));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// optionally wait for the queue be empty and request flush from the sinks
|
|
|
|
@ -283,7 +281,7 @@ inline void spdlog::details::async_log_helper::worker_loop()
|
|
|
|
|
inline bool spdlog::details::async_log_helper::process_next_msg()
|
|
|
|
|
{
|
|
|
|
|
async_msg incoming_async_msg;
|
|
|
|
|
bool dequeued = _q.dequeue_for(incoming_async_msg, std::chrono::milliseconds(1000));
|
|
|
|
|
bool dequeued = _q.dequeue_for(incoming_async_msg, std::chrono::seconds(2));
|
|
|
|
|
if (!dequeued)
|
|
|
|
|
{
|
|
|
|
|
handle_flush_interval();
|
|
|
|
@ -296,12 +294,10 @@ inline bool spdlog::details::async_log_helper::process_next_msg()
|
|
|
|
|
flush_sinks();
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case async_msg_type::terminate:
|
|
|
|
|
//flush_sinks();
|
|
|
|
|
flush_sinks();
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
log_msg incoming_log_msg;
|
|
|
|
|
incoming_async_msg.fill_log_msg(incoming_log_msg);
|
|
|
|
@ -313,26 +309,13 @@ inline bool spdlog::details::async_log_helper::process_next_msg()
|
|
|
|
|
s->log(incoming_log_msg);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
handle_flush_interval();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
assert(false);
|
|
|
|
|
return true; // should not be reached
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// flush all sinks if _flush_interval_ms has expired. only called if queue is empty
|
|
|
|
|
inline void spdlog::details::async_log_helper::handle_flush_interval()
|
|
|
|
|
{
|
|
|
|
|
if (_flush_interval_ms == std::chrono::milliseconds::zero())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
auto delta = details::os::now() - _last_flush;;
|
|
|
|
|
if (delta >= _flush_interval_ms)
|
|
|
|
|
{
|
|
|
|
|
flush_sinks();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline void spdlog::details::async_log_helper::set_formatter(formatter_ptr msg_formatter)
|
|
|
|
@ -340,21 +323,33 @@ inline void spdlog::details::async_log_helper::set_formatter(formatter_ptr msg_f
|
|
|
|
|
_formatter = std::move(msg_formatter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
inline void spdlog::details::async_log_helper::set_error_handler(spdlog::log_err_handler err_handler)
|
|
|
|
|
{
|
|
|
|
|
_err_handler = std::move(err_handler);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// flush all sinks if _flush_interval_ms has expired.
|
|
|
|
|
inline void spdlog::details::async_log_helper::handle_flush_interval()
|
|
|
|
|
{
|
|
|
|
|
if (_flush_interval_ms == std::chrono::milliseconds::zero())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
auto delta = details::os::now() - _last_flush;
|
|
|
|
|
;
|
|
|
|
|
if (delta >= _flush_interval_ms)
|
|
|
|
|
{
|
|
|
|
|
flush_sinks();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// flush all sinks if _flush_interval_ms has expired. only called if queue is empty
|
|
|
|
|
inline void spdlog::details::async_log_helper::flush_sinks()
|
|
|
|
|
{
|
|
|
|
|
printf("FLUSH!\n");
|
|
|
|
|
for (auto &s : _sinks)
|
|
|
|
|
{
|
|
|
|
|
s->flush();
|
|
|
|
|
}
|
|
|
|
|
_last_flush = os::now();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|