diff --git a/include/spdlog/details/file_helper.h b/include/spdlog/details/file_helper.h index f6aa5a3e..fe12a76c 100644 --- a/include/spdlog/details/file_helper.h +++ b/include/spdlog/details/file_helper.h @@ -82,10 +82,10 @@ public: void reopen() { - if(_filename.empty()) - throw spdlog_ex("Failed re opening file - was not opened before"); - open(_filename); - + if(_filename.empty()) + throw spdlog_ex("Failed re opening file - was not opened before"); + open(_filename); + } void close() @@ -97,11 +97,11 @@ public: } } - void write(const log_msg& msg) + void write(const char* data, size_t size) { - auto& buf = msg.formatted.buf(); - size_t size = buf.size(); - if(std::fwrite(buf.data(), sizeof(char), size, _fd) != size) + + + if(std::fwrite(data, sizeof(char), size, _fd) != size) throw spdlog_ex("Failed writing to file " + _filename); if(--_flush_countdown == 0) diff --git a/include/spdlog/details/logger_impl.h b/include/spdlog/details/logger_impl.h index 25fb121b..4ea900b0 100644 --- a/include/spdlog/details/logger_impl.h +++ b/include/spdlog/details/logger_impl.h @@ -184,7 +184,8 @@ inline void spdlog::logger::_variadic_log(spdlog::details::line_logger& l, const inline void spdlog::logger::_log_msg(details::log_msg& msg) { _formatter->format(msg); + auto buf = msg.formatted.buf(); for (auto &sink : _sinks) - sink->log(msg); + sink->sink_it(buf.data(), buf.size()); } diff --git a/include/spdlog/sinks/async_sink.h b/include/spdlog/sinks/async_sink.h index 89b4696a..46406b56 100644 --- a/include/spdlog/sinks/async_sink.h +++ b/include/spdlog/sinks/async_sink.h @@ -53,7 +53,8 @@ namespace sinks class async_sink : public base_sink < details::null_mutex > { public: - using q_type = details::blocking_queue < details::log_msg > ; + using data_type = std::pair < const char*, size_t > ; + using q_type = details::blocking_queue < data_type > ; explicit async_sink(const q_type::size_type max_queue_size); @@ -68,7 +69,7 @@ public: protected: - void _sink_it(const details::log_msg& msg) override; + void _sink_it(const char*, size_t) override; void _thread_loop(); private: @@ -106,10 +107,12 @@ inline spdlog::sinks::async_sink::~async_sink() _join(); } -inline void spdlog::sinks::async_sink::_sink_it(const details::log_msg& msg) +inline void spdlog::sinks::async_sink::_sink_it(const char* data, size_t size) { _push_sentry(); - _q.push(msg); + auto data_copy = new char[size]; + std::memcpy(data_copy, data, size); + _q.push(data_type(data_copy, size)); } @@ -120,31 +123,35 @@ inline void spdlog::sinks::async_sink::_thread_loop() while (_active) { q_type::item_type msg; - if (_q.pop(msg, pop_timeout)) + if (!_q.pop(msg, pop_timeout)) + continue; + + + if (!_active) + return; + + for (auto &s : _sinks) { - if (!_active) - return; - for (auto &s : _sinks) + try { - try - { - s->log(msg); - } - - catch (const std::exception& ex) - { - _last_backthread_ex = std::make_shared(ex.what()); - } - catch (...) - { - _last_backthread_ex = std::make_shared("Unknown exception"); - } - + s->sink_it(msg.first, msg.second); } + + catch (const std::exception& ex) + { + _last_backthread_ex = std::make_shared(ex.what()); + } + catch (...) + { + _last_backthread_ex = std::make_shared("Unknown exception"); + } + } + delete[] msg.first; } } + inline void spdlog::sinks::async_sink::add_sink(spdlog::sink_ptr s) { std::lock_guard guard(_mutex); diff --git a/include/spdlog/sinks/base_sink.h b/include/spdlog/sinks/base_sink.h index a1384819..186d4123 100644 --- a/include/spdlog/sinks/base_sink.h +++ b/include/spdlog/sinks/base_sink.h @@ -52,15 +52,15 @@ public: base_sink(const base_sink&) = delete; base_sink& operator=(const base_sink&) = delete; - void log(const details::log_msg& msg) override + void sink_it(const char* data, size_t size) override { std::lock_guard lock(_mutex); - _sink_it(msg); + _sink_it(data, size); }; protected: - virtual void _sink_it(const details::log_msg& msg) = 0; + virtual void _sink_it(const char* data, size_t size) = 0; Mutex _mutex; }; } diff --git a/include/spdlog/sinks/file_sinks.h b/include/spdlog/sinks/file_sinks.h index 8e9c1485..286452fe 100644 --- a/include/spdlog/sinks/file_sinks.h +++ b/include/spdlog/sinks/file_sinks.h @@ -52,9 +52,9 @@ public: } protected: - void _sink_it(const details::log_msg& msg) override + void _sink_it(const char* data, size_t size) override { - _file_helper.write(msg); + _file_helper.write(data, size); } private: details::file_helper _file_helper; @@ -85,15 +85,15 @@ public: protected: - void _sink_it(const details::log_msg& msg) override + void _sink_it(const char* data, size_t size) override { - _current_size += msg.formatted.size(); + _current_size += size; if (_current_size > _max_size) { _rotate(); - _current_size = msg.formatted.size(); + _current_size = size; } - _file_helper.write(msg); + _file_helper.write(data, size); } @@ -125,12 +125,12 @@ private: std::string target = calc_filename(_base_filename, i, _extension); if (details::file_helper::file_exists(target)) - { - if (std::remove(target.c_str()) != 0) - { - throw spdlog_ex("rotating_file_sink: failed removing " + target); - } - } + { + if (std::remove(target.c_str()) != 0) + { + throw spdlog_ex("rotating_file_sink: failed removing " + target); + } + } if (details::file_helper::file_exists(src) && std::rename(src.c_str(), target.c_str())) { throw spdlog_ex("rotating_file_sink: failed renaming " + src + " to " + target); @@ -169,7 +169,7 @@ public: protected: - void _sink_it(const details::log_msg& msg) override + void _sink_it(const char* data, size_t size) override { if (std::chrono::system_clock::now() >= _midnight_tp) { @@ -177,7 +177,7 @@ protected: _file_helper.open(calc_filename(_base_filename, _extension)); _midnight_tp = _calc_midnight_tp(); } - _file_helper.write(msg); + _file_helper.write(data, size); } private: diff --git a/include/spdlog/sinks/null_sink.h b/include/spdlog/sinks/null_sink.h index 2cd416a9..bc18877f 100644 --- a/include/spdlog/sinks/null_sink.h +++ b/include/spdlog/sinks/null_sink.h @@ -37,7 +37,7 @@ template class null_sink : public base_sink < Mutex > { protected: - void _sink_it(const details::log_msg&) override + void _sink_it(const char*, size_t) override {} }; diff --git a/include/spdlog/sinks/ostream_sink.h b/include/spdlog/sinks/ostream_sink.h index 5f33743b..abc8a7ad 100644 --- a/include/spdlog/sinks/ostream_sink.h +++ b/include/spdlog/sinks/ostream_sink.h @@ -45,10 +45,9 @@ public: virtual ~ostream_sink() = default; protected: - virtual void _sink_it(const details::log_msg& msg) override + void _sink_it(const char* data, size_t size) override { - auto& buf = msg.formatted.buf(); - _ostream.write(buf.data(), buf.size()); + _ostream.write(data, size); } std::ostream& _ostream; }; diff --git a/include/spdlog/sinks/sink.h b/include/spdlog/sinks/sink.h index 0905d52f..ef745ca5 100644 --- a/include/spdlog/sinks/sink.h +++ b/include/spdlog/sinks/sink.h @@ -24,7 +24,6 @@ #pragma once -#include "../details/log_msg.h" namespace spdlog { @@ -34,7 +33,7 @@ class sink { public: virtual ~sink() {} - virtual void log(const details::log_msg& msg) = 0; + virtual void sink_it(const char* data, size_t size) = 0; }; } }