Signed-off-by: JCW <a1q123456@users.noreply.github.com>
This commit is contained in:
JCW
2025-09-04 16:31:39 +01:00
parent e898d98e0a
commit 2491b28fcd
8 changed files with 109 additions and 123 deletions

View File

@@ -26,14 +26,14 @@
#include <boost/beast/core/string.hpp>
#include <boost/filesystem.hpp>
#include <array>
#include <chrono>
#include <fstream>
#include <map>
#include <memory>
#include <mutex>
#include <utility>
#include <chrono>
#include <array>
#include <span>
#include <utility>
namespace ripple {
@@ -150,14 +150,15 @@ private:
beast::severities::Severity thresh_;
File file_;
bool silent_ = false;
// Batching members
mutable std::mutex batchMutex_;
static constexpr size_t BATCH_BUFFER_SIZE = 64 * 1024; // 64KB buffer
std::array<char, BATCH_BUFFER_SIZE> batchBuffer_;
std::span<char> writeBuffer_; // Points to available write space
std::span<char> readBuffer_; // Points to data ready to flush
std::chrono::steady_clock::time_point lastFlush_ = std::chrono::steady_clock::now();
std::chrono::steady_clock::time_point lastFlush_ =
std::chrono::steady_clock::now();
public:
Logs(beast::severities::Severity level);
@@ -209,7 +210,7 @@ public:
std::string
rotate();
void
flushBatch();
@@ -255,7 +256,7 @@ private:
// If the message exceeds this length it will be truncated with elipses.
maximumMessageCharacters = 12 * 1024
};
void
flushBatchUnsafe();
};

View File

@@ -27,6 +27,7 @@
#include <cstring>
#include <memory>
#include <mutex>
#include <shared_mutex>
#include <source_location>
#include <sstream>
#include <string>
@@ -101,7 +102,8 @@ public:
endObject() const
{
using namespace std::string_view_literals;
buffer_.pop_back();
if (buffer_.back() == ',')
buffer_.pop_back();
buffer_.append("},"sv);
}
void
@@ -119,7 +121,8 @@ public:
endArray() const
{
using namespace std::string_view_literals;
buffer_.pop_back();
if (buffer_.back() == ',')
buffer_.pop_back();
buffer_.append("],"sv);
}
void
@@ -348,8 +351,8 @@ private:
std::string m_name;
std::string m_attributesJson;
static std::atomic<std::shared_ptr<const std::string>> globalLogAttributesJson_;
static std::size_t m_filePathOffset_;
static std::string globalLogAttributesJson_;
static std::shared_mutex globalLogAttributesMutex_;
static bool m_jsonLogsEnabled;
static thread_local JsonLogContext currentJsonLogContext_;
@@ -368,9 +371,6 @@ private:
public:
//--------------------------------------------------------------------------
static void
setRootPath(std::string_view fullPath, std::string_view relativePath);
static void
enableStructuredJournal();
@@ -742,25 +742,23 @@ public:
static void
resetGlobalAttributes()
{
globalLogAttributesJson_.store(std::make_shared<const std::string>());
std::unique_lock lock(globalLogAttributesMutex_);
globalLogAttributesJson_.clear();
}
template <typename TAttributesFactory>
static void
addGlobalAttributes(TAttributesFactory&& factory)
{
auto current = globalLogAttributesJson_.load();
std::string buffer = current ? *current : std::string{};
buffer.reserve(128);
auto isEmpty = buffer.empty();
detail::SimpleJsonWriter writer{buffer};
std::unique_lock lock(globalLogAttributesMutex_);
globalLogAttributesJson_.reserve(128);
auto isEmpty = globalLogAttributesJson_.empty();
detail::SimpleJsonWriter writer{globalLogAttributesJson_};
if (isEmpty)
{
writer.startObject();
}
factory(writer);
globalLogAttributesJson_.store(std::make_shared<const std::string>(std::move(buffer)));
}
};