diff --git a/Core/Base/RingBuffer.hpp b/Core/Base/RingBuffer.hpp index 78efb5f..c08d845 100644 --- a/Core/Base/RingBuffer.hpp +++ b/Core/Base/RingBuffer.hpp @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -32,7 +33,7 @@ template class Base_RingBuffer { static_assert(std::is_unsigned_v, "Size_T must be unsigned"); public: - Base_RingBuffer() = default; + explicit Base_RingBuffer(std::pmr::memory_resource* resource = std::pmr::get_default_resource()) : buf_(resource) {} void init(Size_T min_capacity) { // 防御:min_capacity + 1 溢出 if (min_capacity >= std::numeric_limits::max()) { @@ -95,13 +96,14 @@ protected: Size_T capacity = 0; Size_T head = 0; Size_T tail = 0; - std::vector buf_{}; + std::pmr::vector buf_; Size_T mask_ = 0; }; // -------------------- Packet mode (length-prefixed) -------------------- template class RingBuffer : public Base_RingBuffer { public: + using Base_RingBuffer::Base_RingBuffer; static constexpr Size_T HEADER_SIZE = sizeof(Size_T); // 包模式:空间不够则整体失败(不拆包) bool write(const void* src, Size_T len) { @@ -213,6 +215,7 @@ public: template class Stream_RingBuffer : public Base_RingBuffer { public: + using Base_RingBuffer::Base_RingBuffer; bool write(const void* src, Size_T len) { if (len == 0) return true; diff --git a/Core/spdlog/export.cpp b/Core/spdlog/export.cpp index 98a6d8a..1a02ff0 100644 --- a/Core/spdlog/export.cpp +++ b/Core/spdlog/export.cpp @@ -19,6 +19,7 @@ std::string handle_fileName(std::string_view utf8_str) { #endif #include #include +#include #include #include #include @@ -121,6 +122,34 @@ void pure_log(std::string_view pure_path, std::string_view info, logger->flush(); } } +void csv_log(std::string_view pure_path, std::string_view header, std::string_view line) { + auto path = Psc::get_abs_path(pure_path); + static std::map> file_logger_map; + static std::mutex mtx; + static std::once_flag pool_flag; + std::call_once(pool_flag, []() { + if (!spdlog::thread_pool()) + spdlog::init_thread_pool(1024 * 1024, 1); + spdlog::flush_every(std::chrono::seconds(1)); + }); + std::shared_ptr logger; + { + std::lock_guard lock(mtx); + auto iter = file_logger_map.find(path); + if (iter == file_logger_map.end()) { + Psc::create_file_if_not_exists(path); + std::string platform_path = Psc::utf8_2_platform(path); + auto sink = std::make_shared(platform_path, true); + auto created = std::make_shared(platform_path, sink, spdlog::thread_pool(), spdlog::async_overflow_policy::overrun_oldest); + auto formatter = std::make_unique("%v", spdlog::pattern_time_type::local, "\n", spdlog::pattern_formatter::custom_flags()); + created->set_formatter(std::move(formatter)); + created->info(header); + iter = file_logger_map.insert({path, created}).first; + } + logger = iter->second; + } + logger->info(line); +} Log_Type& Log_Type::append(std::string_view key) { list.push_back(std::string(key)); return *this; diff --git a/Core/spdlog/export.h b/Core/spdlog/export.h index 8afc019..bbb023d 100644 --- a/Core/spdlog/export.h +++ b/Core/spdlog/export.h @@ -10,6 +10,7 @@ void rotating_log(std::string_view fileName, std::string_view info, int size = 10 * 1024 * 1024, int num = 10); // 自带换行符 void pure_log(std::string_view path, std::string_view info, bool close_after_write = false); +void csv_log(std::string_view path, std::string_view header, std::string_view line); struct Log_Type { Log_Type() = default; explicit Log_Type(std::vector list);