Skip to content

Commit d1fe8ed

Browse files
committed
Fix error
Signed-off-by: JCW <[email protected]>
1 parent 129166c commit d1fe8ed

File tree

2 files changed

+54
-22
lines changed

2 files changed

+54
-22
lines changed

include/xrpl/beast/utility/Journal.h

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,14 @@ class Journal
461461
StringBuffer messageBuffer_;
462462
detail::SimpleJsonWriter jsonWriter_;
463463
bool hasMessageParams_ = false;
464-
bool messageBufferHandedOut_ = true;
464+
std::size_t messageOffset_ = 0;
465465
public:
466466

467+
JsonLogContext()
468+
: messageBuffer_(rentFromPool())
469+
, jsonWriter_(&messageBuffer_.str())
470+
{}
471+
467472
StringBuffer
468473
messageBuffer() { return messageBuffer_; }
469474

@@ -493,6 +498,9 @@ class Journal
493498
return jsonWriter_;
494499
}
495500

501+
void
502+
reuseJson();
503+
496504
void
497505
finish();
498506

@@ -739,11 +747,32 @@ class Journal
739747
/** Output stream support. */
740748
/** @{ */
741749
ScopedStream
742-
operator<<(std::ostream& manip(std::ostream&)) const;
750+
operator<<(std::ostream& manip(std::ostream&)) const &&
751+
{
752+
return {*this, manip};
753+
}
743754

744755
template <typename T>
745756
ScopedStream
746-
operator<<(T const& t) const;
757+
operator<<(T const& t) const &&
758+
{
759+
return {*this, t};
760+
}
761+
762+
ScopedStream
763+
operator<<(std::ostream& manip(std::ostream&)) const &
764+
{
765+
currentJsonLogContext_.reuseJson();
766+
return {*this, manip};
767+
}
768+
769+
template <typename T>
770+
ScopedStream
771+
operator<<(T const& t) const &
772+
{
773+
currentJsonLogContext_.reuseJson();
774+
return {*this, t};
775+
}
747776
/** @} */
748777

749778
private:
@@ -955,13 +984,6 @@ Journal::ScopedStream::operator<<(T const& t) const
955984

956985
//------------------------------------------------------------------------------
957986

958-
template <typename T>
959-
Journal::ScopedStream
960-
Journal::Stream::operator<<(T const& t) const
961-
{
962-
return {*this, t};
963-
}
964-
965987
namespace detail {
966988

967989
template <class CharT, class Traits = std::char_traits<CharT>>

src/libxrpl/beast/utility/beast_Journal.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -223,19 +223,13 @@ Journal::JsonLogContext::start(
223223
};
224224
thread_local ThreadIdStringInitializer const threadId;
225225

226-
if (!messageBufferHandedOut_)
227-
{
228-
returnStringBuffer(std::move(messageBuffer_));
229-
messageBufferHandedOut_ = true;
230-
}
231-
messageBuffer_ = rentFromPool();
232-
messageBufferHandedOut_ = false;
233-
messageBuffer_.str().reserve(1024 * 5);
226+
messageOffset_ = 0;
234227
messageBuffer_.str().clear();
235228
jsonWriter_ = detail::SimpleJsonWriter{&messageBuffer_.str()};
236229

237230
if (!jsonLogsEnabled_)
238231
{
232+
messageBuffer_.str() = journalAttributes;
239233
return;
240234
}
241235

@@ -293,12 +287,28 @@ Journal::JsonLogContext::start(
293287
hasMessageParams_ = false;
294288
}
295289

290+
void
291+
Journal::JsonLogContext::reuseJson()
292+
{
293+
messageOffset_ = messageBuffer_.str().size();
294+
}
296295
void
297296
Journal::JsonLogContext::finish()
298297
{
299-
messageBufferHandedOut_ = true;
300-
messageBuffer_ = {};
301-
jsonWriter_ = {};
298+
if (messageOffset_ != 0)
299+
{
300+
auto buffer = rentFromPool();
301+
std::string_view json{messageBuffer_.str()};
302+
buffer.str() = json.substr(0, messageOffset_);
303+
messageBuffer_ = buffer;
304+
}
305+
else
306+
{
307+
messageBuffer_ = rentFromPool();
308+
}
309+
310+
messageBuffer_.str().reserve(1024 * 5);
311+
jsonWriter_ = detail::SimpleJsonWriter{&messageBuffer_.str()};
302312
}
303313

304314
void
@@ -414,8 +424,8 @@ Journal::ScopedStream::~ScopedStream()
414424
s = "";
415425

416426
auto messageHandle = formatLog(s);
417-
m_sink.write(m_level, messageHandle);
418427
currentJsonLogContext_.finish();
428+
m_sink.write(m_level, messageHandle);
419429
}
420430
}
421431

0 commit comments

Comments
 (0)