mirror of
https://github.com/gabime/spdlog.git
synced 2025-09-29 01:29:35 +08:00
move log_msg to details namespace
This commit is contained in:
@@ -7,7 +7,10 @@
|
||||
#include "base_sink.h"
|
||||
#include "../logger.h"
|
||||
#include "../details/blocking_queue.h"
|
||||
#include "../details/log_msg.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
namespace c11log
|
||||
{
|
||||
namespace sinks
|
||||
@@ -16,7 +19,9 @@ namespace sinks
|
||||
class async_sink : public base_sink
|
||||
{
|
||||
public:
|
||||
using size_type = c11log::details::blocking_queue<std::string>::size_type;
|
||||
|
||||
using queue_t = c11log::details::blocking_queue<std::shared_ptr<details::log_msg>>;
|
||||
using size_type = queue_t::size_type;
|
||||
|
||||
explicit async_sink(const size_type max_queue_size);
|
||||
~async_sink();
|
||||
@@ -28,13 +33,13 @@ public:
|
||||
|
||||
|
||||
protected:
|
||||
void _sink_it(const bufpair_t& msg) override;
|
||||
void _sink_it(const details::log_msg& msg) override;
|
||||
void _thread_loop();
|
||||
|
||||
private:
|
||||
c11log::logger::sinks_vector_t _sinks;
|
||||
std::atomic<bool> _active;
|
||||
c11log::details::blocking_queue<std::string> _q;
|
||||
queue_t _q;
|
||||
std::thread _back_thread;
|
||||
//Clear all remaining messages(if any), stop the _back_thread and join it
|
||||
void _shutdown();
|
||||
@@ -46,7 +51,7 @@ private:
|
||||
// async_sink class implementation
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
inline c11log::sinks::async_sink::async_sink(const std::size_t max_queue_size)
|
||||
inline c11log::sinks::async_sink::async_sink(const size_type max_queue_size)
|
||||
:_sinks(),
|
||||
_active(true),
|
||||
_q(max_queue_size),
|
||||
@@ -57,27 +62,33 @@ inline c11log::sinks::async_sink::~async_sink()
|
||||
{
|
||||
_shutdown();
|
||||
}
|
||||
inline void c11log::sinks::async_sink::_sink_it(const bufpair_t& msg)
|
||||
inline void c11log::sinks::async_sink::_sink_it(const details::log_msg& msg)
|
||||
{
|
||||
std::string s {msg.first, msg.first+msg.second};
|
||||
_q.push(s);
|
||||
//re allocate on the heap the (stack based) message
|
||||
auto new_msg = new details::log_msg();
|
||||
*new_msg = msg;
|
||||
auto msg_size = msg.msg_buf.second;
|
||||
char *buf = new char[msg_size];
|
||||
std::memcpy(buf, msg.msg_buf.first, msg_size);
|
||||
new_msg->msg_buf = bufpair_t(buf, msg_size);
|
||||
auto new_shared = std::shared_ptr<details::log_msg>(new_msg, [](details::log_msg* msg_to_delete)
|
||||
{
|
||||
delete []msg_to_delete->msg_buf.first;
|
||||
});
|
||||
_q.push(new_shared);
|
||||
}
|
||||
|
||||
inline void c11log::sinks::async_sink::_thread_loop()
|
||||
{
|
||||
static std::chrono::seconds pop_timeout { 1 };
|
||||
std::string msg;
|
||||
|
||||
while (_active)
|
||||
{
|
||||
std::shared_ptr<details::log_msg> msg;
|
||||
if (_q.pop(msg, pop_timeout))
|
||||
{
|
||||
bufpair_t buf(msg.data(), msg.size());
|
||||
for (auto &sink : _sinks)
|
||||
{
|
||||
sink->log(buf, level::INFO);
|
||||
if (!_active)
|
||||
return;
|
||||
sink->log(*msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "../formatter.h"
|
||||
#include "../common_types.h"
|
||||
#include "../details/log_msg.h"
|
||||
|
||||
namespace c11log
|
||||
{
|
||||
@@ -19,7 +20,7 @@ public:
|
||||
base_sink(const base_sink&) = delete;
|
||||
base_sink& operator=(const base_sink&) = delete;
|
||||
|
||||
void log(const log_msg& msg)
|
||||
void log(const details::log_msg& msg)
|
||||
{
|
||||
if (_enabled)
|
||||
{
|
||||
@@ -38,14 +39,14 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void _sink_it(const log_msg& msg) = 0;
|
||||
virtual void _sink_it(const details::log_msg& msg) = 0;
|
||||
std::atomic<bool> _enabled;
|
||||
};
|
||||
|
||||
class null_sink:public base_sink
|
||||
{
|
||||
protected:
|
||||
void _sink_it(const log_msg&) override
|
||||
void _sink_it(const details::log_msg&) override
|
||||
{
|
||||
}
|
||||
};
|
||||
|
@@ -19,7 +19,7 @@ public:
|
||||
virtual ~console_sink() = default;
|
||||
|
||||
protected:
|
||||
virtual void _sink_it(const log_msg& msg) override
|
||||
virtual void _sink_it(const details::log_msg& msg) override
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
_ostream.write(msg.msg_buf.first, msg.msg_buf.second);
|
||||
|
@@ -6,6 +6,7 @@
|
||||
#include <mutex>
|
||||
#include "base_sink.h"
|
||||
#include "../details/flush_helper.h"
|
||||
#include "../details/blocking_queue.h"
|
||||
|
||||
namespace c11log
|
||||
{
|
||||
@@ -27,7 +28,7 @@ public:
|
||||
{
|
||||
}
|
||||
protected:
|
||||
void _sink_it(const log_msg& msg) override
|
||||
void _sink_it(const details::log_msg& msg) override
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
_flush_helper.write(msg.msg_buf, _ofstream);
|
||||
@@ -60,7 +61,7 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
void _sink_it(const log_msg& msg) override
|
||||
void _sink_it(const details::log_msg& msg) override
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
|
||||
@@ -134,7 +135,7 @@ public:
|
||||
}
|
||||
|
||||
protected:
|
||||
void _sink_it(const log_msg& msg) override
|
||||
void _sink_it(const details::log_msg& msg) override
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(_mutex);
|
||||
if (std::chrono::system_clock::now() >= _midnight_tp)
|
||||
|
Reference in New Issue
Block a user