Skip to content

Commit 6de7802

Browse files
committed
Remove unrelated changes
Signed-off-by: JCW <[email protected]>
1 parent 56a4550 commit 6de7802

File tree

19 files changed

+138
-494
lines changed

19 files changed

+138
-494
lines changed

include/xrpl/basics/Log.h

Lines changed: 17 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,12 @@
2626
#include <boost/beast/core/string.hpp>
2727
#include <boost/filesystem.hpp>
2828

29-
#include <boost/lockfree/queue.hpp>
30-
31-
#include <array>
32-
#include <atomic>
33-
#include <chrono>
34-
#include <condition_variable>
3529
#include <fstream>
3630
#include <map>
3731
#include <memory>
3832
#include <mutex>
39-
#include <span>
40-
#include <thread>
4133
#include <utility>
34+
#include <string_view>
4235

4336
namespace ripple {
4437

@@ -76,10 +69,11 @@ class Logs
7669
operator=(Sink const&) = delete;
7770

7871
void
79-
write(beast::severities::Severity level, beast::Journal::StringBuffer text) override;
72+
write(beast::severities::Severity level, std::string const& text)
73+
override;
8074

8175
void
82-
writeAlways(beast::severities::Severity level, beast::Journal::StringBuffer text)
76+
writeAlways(beast::severities::Severity level, std::string const& text)
8377
override;
8478
};
8579

@@ -137,16 +131,22 @@ class Logs
137131
Does nothing if there is no associated system file.
138132
*/
139133
void
140-
write(std::string const& str);
134+
write(std::string_view text);
135+
136+
/** write to the log file and append an end of line marker.
137+
Does nothing if there is no associated system file.
138+
*/
139+
void
140+
writeln(std::string_view text);
141141

142142
/** @} */
143143

144144
private:
145-
std::optional<std::ofstream> m_stream;
145+
std::unique_ptr<std::ofstream> m_stream;
146146
boost::filesystem::path m_path;
147147
};
148148

149-
std::mutex mutable sinkSetMutex_;
149+
std::mutex mutable mutex_;
150150
std::map<
151151
std::string,
152152
std::unique_ptr<beast::Journal::Sink>,
@@ -156,32 +156,14 @@ class Logs
156156
File file_;
157157
bool silent_ = false;
158158

159-
// Batching members
160-
mutable std::mutex batchMutex_;
161-
boost::lockfree::queue<beast::Journal::StringBuffer, boost::lockfree::capacity<100>> messages_;
162-
static constexpr size_t BATCH_BUFFER_SIZE = 64 * 1024; // 64KB buffer
163-
std::array<char, BATCH_BUFFER_SIZE> batchBuffer_{};
164-
std::span<char> writeBuffer_; // Points to available write space
165-
std::span<char> readBuffer_; // Points to data ready to flush
166-
167-
// Log thread members
168-
std::thread logThread_;
169-
std::atomic<bool> stopLogThread_;
170-
std::mutex logMutex_;
171-
std::condition_variable logCondition_;
172-
173-
private:
174-
std::chrono::steady_clock::time_point lastFlush_ =
175-
std::chrono::steady_clock::now();
176-
177159
public:
178160
Logs(beast::severities::Severity level);
179161

180162
Logs(Logs const&) = delete;
181163
Logs&
182164
operator=(Logs const&) = delete;
183165

184-
virtual ~Logs(); // Need to flush on destruction
166+
virtual ~Logs() = default;
185167

186168
bool
187169
open(boost::filesystem::path const& pathToLogFile);
@@ -201,10 +183,7 @@ class Logs
201183
}
202184

203185
beast::Journal
204-
journal(std::string const& name)
205-
{
206-
return beast::Journal{get(name), name};
207-
}
186+
journal(std::string const& name);
208187

209188
beast::severities::Severity
210189
threshold() const;
@@ -219,15 +198,12 @@ class Logs
219198
write(
220199
beast::severities::Severity level,
221200
std::string const& partition,
222-
beast::Journal::StringBuffer text,
201+
std::string const& text,
223202
bool console);
224203

225204
std::string
226205
rotate();
227206

228-
void
229-
flushBatch();
230-
231207
/**
232208
* Set flag to write logs to stderr (false) or not (true).
233209
*
@@ -260,7 +236,7 @@ class Logs
260236
static void
261237
format(
262238
std::string& output,
263-
std::string_view message,
239+
std::string const& message,
264240
beast::severities::Severity severity,
265241
std::string const& partition);
266242

@@ -270,12 +246,6 @@ class Logs
270246
// If the message exceeds this length it will be truncated with elipses.
271247
maximumMessageCharacters = 12 * 1024
272248
};
273-
274-
void
275-
flushBatchUnsafe();
276-
277-
void
278-
logThreadWorker();
279249
};
280250

281251
// Wraps a Journal::Stream to skip evaluation of

include/xrpl/beast/utility/Journal.h

Lines changed: 9 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@
2222

2323
#include <xrpl/beast/utility/instrumentation.h>
2424

25-
#include <thread>
2625
#include <deque>
2726
#include <atomic>
2827
#include <charconv>
2928
#include <cstring>
30-
#include <memory>
3129
#include <mutex>
3230
#include <shared_mutex>
3331
#include <source_location>
@@ -330,146 +328,21 @@ class Journal
330328

331329
class Sink;
332330

333-
class StringBufferPool {
334-
public:
335-
static constexpr std::uint32_t kEmptyIdx = std::numeric_limits<std::uint32_t>::max();
336-
337-
struct Head {
338-
std::uint32_t tag;
339-
std::uint32_t idx; // kEmptyIdx means empty
340-
};
341-
342-
struct Node {
343-
std::uint32_t next_idx{kEmptyIdx};
344-
std::uint32_t self_idx{kEmptyIdx};
345-
std::string buf{};
346-
};
347-
348-
class StringBuffer
349-
{
350-
public:
351-
StringBuffer() = default;
352-
353-
std::string&
354-
str() { return node_->buf; }
355-
356-
private:
357-
StringBuffer(StringBufferPool* owner, Node* node)
358-
: owner_(owner), node_(node) {}
359-
360-
StringBufferPool* owner_ = nullptr;
361-
Node* node_ = nullptr;
362-
363-
friend class StringBufferPool;
364-
};
365-
366-
explicit StringBufferPool(std::uint32_t grow_by = 20)
367-
: growBy_(grow_by), head_({0, kEmptyIdx}) {}
368-
369-
// Rent a buffer; grows on demand. Returns move-only RAII handle.
370-
StringBuffer rent() {
371-
for (;;) {
372-
auto old = head_.load(std::memory_order_acquire);
373-
if (old.idx == kEmptyIdx) { grow(); continue; } // rare slow path
374-
375-
Node& n = nodes_[old.idx];
376-
std::uint32_t next = n.next_idx;
377-
378-
Head neu{ old.tag + 1, next };
379-
if (head_.compare_exchange_weak(old, neu,
380-
std::memory_order_acq_rel,
381-
std::memory_order_acquire)) {
382-
return {this, &n};
383-
}
384-
}
385-
}
386-
387-
// Only the pool/handle can call this
388-
void giveBack(StringBuffer&& h) noexcept {
389-
Node* node = h.node_;
390-
if (!node) return; // already invalid
391-
const std::uint32_t idx = node->self_idx;
392-
393-
for (;;) {
394-
auto old = head_.load(std::memory_order_acquire);
395-
396-
node->next_idx = old.idx;
397-
398-
Head neu{ std::uint32_t(old.tag + 1), idx };
399-
if (head_.compare_exchange_weak(old, neu,
400-
std::memory_order_acq_rel,
401-
std::memory_order_acquire)) {
402-
// Invalidate handle (prevents double return)
403-
h.owner_ = nullptr;
404-
h.node_ = nullptr;
405-
return;
406-
}
407-
}
408-
}
409-
410-
private:
411-
412-
void grow() {
413-
if (head_.load(std::memory_order_acquire).idx != kEmptyIdx) return;
414-
std::scoped_lock lk(growMutex_);
415-
if (head_.load(std::memory_order_acquire).idx != kEmptyIdx) return;
416-
417-
auto base = static_cast<std::uint32_t>(nodes_.size());
418-
nodes_.resize(base + growBy_);
419-
420-
// Init nodes and local chain
421-
for (std::uint32_t i = 0; i < growBy_; ++i) {
422-
std::uint32_t idx = base + i;
423-
Node& n = nodes_[idx];
424-
n.self_idx = idx;
425-
n.next_idx = (i + 1 < growBy_) ? (idx + 1) : kEmptyIdx;
426-
}
427-
428-
// Splice chain onto global head: [base .. base+grow_by_-1]
429-
const std::uint32_t chain_head = base;
430-
const std::uint32_t chain_tail = base + growBy_ - 1;
431-
432-
for (;;) {
433-
auto old = head_.load(std::memory_order_acquire);
434-
435-
nodes_[chain_tail].next_idx = old.idx; // tail -> old head
436-
Head neu{ std::uint32_t(old.tag + 1), chain_head };
437-
438-
if (head_.compare_exchange_weak(old, neu,
439-
std::memory_order_acq_rel,
440-
std::memory_order_acquire)) {
441-
break;
442-
}
443-
}
444-
}
445-
446-
const std::uint32_t growBy_;
447-
448-
// single 64-bit CAS
449-
std::atomic<Head> head_;
450-
451-
// only during growth
452-
std::mutex growMutex_;
453-
454-
// stable storage for nodes/strings
455-
std::deque<Node> nodes_;
456-
};
457-
using StringBuffer = StringBufferPool::StringBuffer;
458-
459331
class JsonLogContext
460332
{
461-
StringBuffer messageBuffer_;
333+
std::string messageBuffer_;
462334
detail::SimpleJsonWriter jsonWriter_;
463335
bool hasMessageParams_ = false;
464336
std::size_t messageOffset_ = 0;
465337
public:
466338

467339
JsonLogContext()
468-
: messageBuffer_(rentFromPool())
469-
, jsonWriter_(&messageBuffer_.str())
470-
{}
340+
: jsonWriter_(&messageBuffer_)
341+
{
342+
messageBuffer_.reserve(4 * 1024);
343+
}
471344

472-
StringBuffer
345+
std::string&
473346
messageBuffer() { return messageBuffer_; }
474347

475348
void
@@ -522,7 +395,6 @@ class Journal
522395
static std::shared_mutex globalLogAttributesMutex_;
523396
static bool jsonLogsEnabled_;
524397

525-
static StringBufferPool messagePool_;
526398
static thread_local JsonLogContext currentJsonLogContext_;
527399

528400
// Invariant: m_sink always points to a valid Sink
@@ -533,21 +405,12 @@ class Journal
533405
std::source_location location,
534406
severities::Severity severity) const;
535407

536-
static StringBuffer
408+
static std::string&
537409
formatLog(std::string const& message);
538410

539411
public:
540412
//--------------------------------------------------------------------------
541413

542-
static StringBuffer
543-
rentFromPool()
544-
{
545-
return messagePool_.rent();
546-
}
547-
548-
static void
549-
returnStringBuffer(StringBuffer&& node) { messagePool_.giveBack(std::move(node)); }
550-
551414
static void
552415
enableStructuredJournal();
553416

@@ -597,7 +460,7 @@ class Journal
597460
level is below the current threshold().
598461
*/
599462
virtual void
600-
write(Severity level, StringBuffer text) = 0;
463+
write(Severity level, std::string const& text) = 0;
601464

602465
/** Bypass filter and write text to the sink at the specified severity.
603466
* Always write the message, but maintain the same formatting as if
@@ -607,7 +470,7 @@ class Journal
607470
* @param text Text to write to sink.
608471
*/
609472
virtual void
610-
writeAlways(Severity level, StringBuffer text) = 0;
473+
writeAlways(Severity level, std::string const& text) = 0;
611474

612475
private:
613476
Severity thresh_;

include/xrpl/beast/utility/WrappedSink.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,17 @@ class WrappedSink : public beast::Journal::Sink
8888
}
8989

9090
void
91-
write(beast::severities::Severity level, Journal::StringBuffer text) override
91+
write(beast::severities::Severity level, std::string const& text) override
9292
{
9393
using beast::Journal;
94-
text.str() = prefix_ + text.str();
95-
sink_.write(level, text);
94+
sink_.write(level, prefix_ + text);
9695
}
9796

9897
void
99-
writeAlways(severities::Severity level, Journal::StringBuffer text) override
98+
writeAlways(severities::Severity level, std::string const& text) override
10099
{
101100
using beast::Journal;
102-
text.str() = prefix_ + text.str();
103-
sink_.writeAlways(level, text);
101+
sink_.writeAlways(level, prefix_ + text);
104102
}
105103
};
106104

0 commit comments

Comments
 (0)