mirror of
https://github.com/XRPLF/rippled.git
synced 2026-04-29 15:37:57 +00:00
Remove unrelated changes
Signed-off-by: JCW <a1q123456@users.noreply.github.com>
This commit is contained in:
@@ -26,19 +26,12 @@
|
||||
#include <boost/beast/core/string.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
#include <boost/lockfree/queue.hpp>
|
||||
|
||||
#include <array>
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <condition_variable>
|
||||
#include <fstream>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <span>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
#include <string_view>
|
||||
|
||||
namespace ripple {
|
||||
|
||||
@@ -76,10 +69,11 @@ private:
|
||||
operator=(Sink const&) = delete;
|
||||
|
||||
void
|
||||
write(beast::severities::Severity level, beast::Journal::StringBuffer text) override;
|
||||
write(beast::severities::Severity level, std::string const& text)
|
||||
override;
|
||||
|
||||
void
|
||||
writeAlways(beast::severities::Severity level, beast::Journal::StringBuffer text)
|
||||
writeAlways(beast::severities::Severity level, std::string const& text)
|
||||
override;
|
||||
};
|
||||
|
||||
@@ -137,16 +131,22 @@ private:
|
||||
Does nothing if there is no associated system file.
|
||||
*/
|
||||
void
|
||||
write(std::string const& str);
|
||||
write(std::string_view text);
|
||||
|
||||
/** write to the log file and append an end of line marker.
|
||||
Does nothing if there is no associated system file.
|
||||
*/
|
||||
void
|
||||
writeln(std::string_view text);
|
||||
|
||||
/** @} */
|
||||
|
||||
private:
|
||||
std::optional<std::ofstream> m_stream;
|
||||
std::unique_ptr<std::ofstream> m_stream;
|
||||
boost::filesystem::path m_path;
|
||||
};
|
||||
|
||||
std::mutex mutable sinkSetMutex_;
|
||||
std::mutex mutable mutex_;
|
||||
std::map<
|
||||
std::string,
|
||||
std::unique_ptr<beast::Journal::Sink>,
|
||||
@@ -156,24 +156,6 @@ private:
|
||||
File file_;
|
||||
bool silent_ = false;
|
||||
|
||||
// Batching members
|
||||
mutable std::mutex batchMutex_;
|
||||
boost::lockfree::queue<beast::Journal::StringBuffer, boost::lockfree::capacity<100>> messages_;
|
||||
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
|
||||
|
||||
// Log thread members
|
||||
std::thread logThread_;
|
||||
std::atomic<bool> stopLogThread_;
|
||||
std::mutex logMutex_;
|
||||
std::condition_variable logCondition_;
|
||||
|
||||
private:
|
||||
std::chrono::steady_clock::time_point lastFlush_ =
|
||||
std::chrono::steady_clock::now();
|
||||
|
||||
public:
|
||||
Logs(beast::severities::Severity level);
|
||||
|
||||
@@ -181,7 +163,7 @@ public:
|
||||
Logs&
|
||||
operator=(Logs const&) = delete;
|
||||
|
||||
virtual ~Logs(); // Need to flush on destruction
|
||||
virtual ~Logs() = default;
|
||||
|
||||
bool
|
||||
open(boost::filesystem::path const& pathToLogFile);
|
||||
@@ -201,10 +183,7 @@ public:
|
||||
}
|
||||
|
||||
beast::Journal
|
||||
journal(std::string const& name)
|
||||
{
|
||||
return beast::Journal{get(name), name};
|
||||
}
|
||||
journal(std::string const& name);
|
||||
|
||||
beast::severities::Severity
|
||||
threshold() const;
|
||||
@@ -219,15 +198,12 @@ public:
|
||||
write(
|
||||
beast::severities::Severity level,
|
||||
std::string const& partition,
|
||||
beast::Journal::StringBuffer text,
|
||||
std::string const& text,
|
||||
bool console);
|
||||
|
||||
std::string
|
||||
rotate();
|
||||
|
||||
void
|
||||
flushBatch();
|
||||
|
||||
/**
|
||||
* Set flag to write logs to stderr (false) or not (true).
|
||||
*
|
||||
@@ -260,7 +236,7 @@ public:
|
||||
static void
|
||||
format(
|
||||
std::string& output,
|
||||
std::string_view message,
|
||||
std::string const& message,
|
||||
beast::severities::Severity severity,
|
||||
std::string const& partition);
|
||||
|
||||
@@ -270,12 +246,6 @@ private:
|
||||
// If the message exceeds this length it will be truncated with elipses.
|
||||
maximumMessageCharacters = 12 * 1024
|
||||
};
|
||||
|
||||
void
|
||||
flushBatchUnsafe();
|
||||
|
||||
void
|
||||
logThreadWorker();
|
||||
};
|
||||
|
||||
// Wraps a Journal::Stream to skip evaluation of
|
||||
|
||||
@@ -22,12 +22,10 @@
|
||||
|
||||
#include <xrpl/beast/utility/instrumentation.h>
|
||||
|
||||
#include <thread>
|
||||
#include <deque>
|
||||
#include <atomic>
|
||||
#include <charconv>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <shared_mutex>
|
||||
#include <source_location>
|
||||
@@ -330,146 +328,21 @@ public:
|
||||
|
||||
class Sink;
|
||||
|
||||
class StringBufferPool {
|
||||
public:
|
||||
static constexpr std::uint32_t kEmptyIdx = std::numeric_limits<std::uint32_t>::max();
|
||||
|
||||
struct Head {
|
||||
std::uint32_t tag;
|
||||
std::uint32_t idx; // kEmptyIdx means empty
|
||||
};
|
||||
|
||||
struct Node {
|
||||
std::uint32_t next_idx{kEmptyIdx};
|
||||
std::uint32_t self_idx{kEmptyIdx};
|
||||
std::string buf{};
|
||||
};
|
||||
|
||||
class StringBuffer
|
||||
{
|
||||
public:
|
||||
StringBuffer() = default;
|
||||
|
||||
std::string&
|
||||
str() { return node_->buf; }
|
||||
|
||||
private:
|
||||
StringBuffer(StringBufferPool* owner, Node* node)
|
||||
: owner_(owner), node_(node) {}
|
||||
|
||||
StringBufferPool* owner_ = nullptr;
|
||||
Node* node_ = nullptr;
|
||||
|
||||
friend class StringBufferPool;
|
||||
};
|
||||
|
||||
explicit StringBufferPool(std::uint32_t grow_by = 20)
|
||||
: growBy_(grow_by), head_({0, kEmptyIdx}) {}
|
||||
|
||||
// Rent a buffer; grows on demand. Returns move-only RAII handle.
|
||||
StringBuffer rent() {
|
||||
for (;;) {
|
||||
auto old = head_.load(std::memory_order_acquire);
|
||||
if (old.idx == kEmptyIdx) { grow(); continue; } // rare slow path
|
||||
|
||||
Node& n = nodes_[old.idx];
|
||||
std::uint32_t next = n.next_idx;
|
||||
|
||||
Head neu{ old.tag + 1, next };
|
||||
if (head_.compare_exchange_weak(old, neu,
|
||||
std::memory_order_acq_rel,
|
||||
std::memory_order_acquire)) {
|
||||
return {this, &n};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Only the pool/handle can call this
|
||||
void giveBack(StringBuffer&& h) noexcept {
|
||||
Node* node = h.node_;
|
||||
if (!node) return; // already invalid
|
||||
const std::uint32_t idx = node->self_idx;
|
||||
|
||||
for (;;) {
|
||||
auto old = head_.load(std::memory_order_acquire);
|
||||
|
||||
node->next_idx = old.idx;
|
||||
|
||||
Head neu{ std::uint32_t(old.tag + 1), idx };
|
||||
if (head_.compare_exchange_weak(old, neu,
|
||||
std::memory_order_acq_rel,
|
||||
std::memory_order_acquire)) {
|
||||
// Invalidate handle (prevents double return)
|
||||
h.owner_ = nullptr;
|
||||
h.node_ = nullptr;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void grow() {
|
||||
if (head_.load(std::memory_order_acquire).idx != kEmptyIdx) return;
|
||||
std::scoped_lock lk(growMutex_);
|
||||
if (head_.load(std::memory_order_acquire).idx != kEmptyIdx) return;
|
||||
|
||||
auto base = static_cast<std::uint32_t>(nodes_.size());
|
||||
nodes_.resize(base + growBy_);
|
||||
|
||||
// Init nodes and local chain
|
||||
for (std::uint32_t i = 0; i < growBy_; ++i) {
|
||||
std::uint32_t idx = base + i;
|
||||
Node& n = nodes_[idx];
|
||||
n.self_idx = idx;
|
||||
n.next_idx = (i + 1 < growBy_) ? (idx + 1) : kEmptyIdx;
|
||||
}
|
||||
|
||||
// Splice chain onto global head: [base .. base+grow_by_-1]
|
||||
const std::uint32_t chain_head = base;
|
||||
const std::uint32_t chain_tail = base + growBy_ - 1;
|
||||
|
||||
for (;;) {
|
||||
auto old = head_.load(std::memory_order_acquire);
|
||||
|
||||
nodes_[chain_tail].next_idx = old.idx; // tail -> old head
|
||||
Head neu{ std::uint32_t(old.tag + 1), chain_head };
|
||||
|
||||
if (head_.compare_exchange_weak(old, neu,
|
||||
std::memory_order_acq_rel,
|
||||
std::memory_order_acquire)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const std::uint32_t growBy_;
|
||||
|
||||
// single 64-bit CAS
|
||||
std::atomic<Head> head_;
|
||||
|
||||
// only during growth
|
||||
std::mutex growMutex_;
|
||||
|
||||
// stable storage for nodes/strings
|
||||
std::deque<Node> nodes_;
|
||||
};
|
||||
using StringBuffer = StringBufferPool::StringBuffer;
|
||||
|
||||
class JsonLogContext
|
||||
{
|
||||
StringBuffer messageBuffer_;
|
||||
std::string messageBuffer_;
|
||||
detail::SimpleJsonWriter jsonWriter_;
|
||||
bool hasMessageParams_ = false;
|
||||
std::size_t messageOffset_ = 0;
|
||||
public:
|
||||
|
||||
JsonLogContext()
|
||||
: messageBuffer_(rentFromPool())
|
||||
, jsonWriter_(&messageBuffer_.str())
|
||||
{}
|
||||
: jsonWriter_(&messageBuffer_)
|
||||
{
|
||||
messageBuffer_.reserve(4 * 1024);
|
||||
}
|
||||
|
||||
StringBuffer
|
||||
std::string&
|
||||
messageBuffer() { return messageBuffer_; }
|
||||
|
||||
void
|
||||
@@ -522,7 +395,6 @@ private:
|
||||
static std::shared_mutex globalLogAttributesMutex_;
|
||||
static bool jsonLogsEnabled_;
|
||||
|
||||
static StringBufferPool messagePool_;
|
||||
static thread_local JsonLogContext currentJsonLogContext_;
|
||||
|
||||
// Invariant: m_sink always points to a valid Sink
|
||||
@@ -533,21 +405,12 @@ private:
|
||||
std::source_location location,
|
||||
severities::Severity severity) const;
|
||||
|
||||
static StringBuffer
|
||||
static std::string&
|
||||
formatLog(std::string const& message);
|
||||
|
||||
public:
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
static StringBuffer
|
||||
rentFromPool()
|
||||
{
|
||||
return messagePool_.rent();
|
||||
}
|
||||
|
||||
static void
|
||||
returnStringBuffer(StringBuffer&& node) { messagePool_.giveBack(std::move(node)); }
|
||||
|
||||
static void
|
||||
enableStructuredJournal();
|
||||
|
||||
@@ -597,7 +460,7 @@ public:
|
||||
level is below the current threshold().
|
||||
*/
|
||||
virtual void
|
||||
write(Severity level, StringBuffer text) = 0;
|
||||
write(Severity level, std::string const& text) = 0;
|
||||
|
||||
/** Bypass filter and write text to the sink at the specified severity.
|
||||
* Always write the message, but maintain the same formatting as if
|
||||
@@ -607,7 +470,7 @@ public:
|
||||
* @param text Text to write to sink.
|
||||
*/
|
||||
virtual void
|
||||
writeAlways(Severity level, StringBuffer text) = 0;
|
||||
writeAlways(Severity level, std::string const& text) = 0;
|
||||
|
||||
private:
|
||||
Severity thresh_;
|
||||
|
||||
@@ -88,19 +88,17 @@ public:
|
||||
}
|
||||
|
||||
void
|
||||
write(beast::severities::Severity level, Journal::StringBuffer text) override
|
||||
write(beast::severities::Severity level, std::string const& text) override
|
||||
{
|
||||
using beast::Journal;
|
||||
text.str() = prefix_ + text.str();
|
||||
sink_.write(level, text);
|
||||
sink_.write(level, prefix_ + text);
|
||||
}
|
||||
|
||||
void
|
||||
writeAlways(severities::Severity level, Journal::StringBuffer text) override
|
||||
writeAlways(severities::Severity level, std::string const& text) override
|
||||
{
|
||||
using beast::Journal;
|
||||
text.str() = prefix_ + text.str();
|
||||
sink_.writeAlways(level, text);
|
||||
sink_.writeAlways(level, prefix_ + text);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user