Fix error

Signed-off-by: JCW <a1q123456@users.noreply.github.com>
This commit is contained in:
JCW
2025-09-12 23:27:57 +01:00
parent 129166cda5
commit d1fe8ed31d
2 changed files with 54 additions and 22 deletions

View File

@@ -461,9 +461,14 @@ public:
StringBuffer messageBuffer_; StringBuffer messageBuffer_;
detail::SimpleJsonWriter jsonWriter_; detail::SimpleJsonWriter jsonWriter_;
bool hasMessageParams_ = false; bool hasMessageParams_ = false;
bool messageBufferHandedOut_ = true; std::size_t messageOffset_ = 0;
public: public:
JsonLogContext()
: messageBuffer_(rentFromPool())
, jsonWriter_(&messageBuffer_.str())
{}
StringBuffer StringBuffer
messageBuffer() { return messageBuffer_; } messageBuffer() { return messageBuffer_; }
@@ -493,6 +498,9 @@ public:
return jsonWriter_; return jsonWriter_;
} }
void
reuseJson();
void void
finish(); finish();
@@ -739,11 +747,32 @@ public:
/** Output stream support. */ /** Output stream support. */
/** @{ */ /** @{ */
ScopedStream ScopedStream
operator<<(std::ostream& manip(std::ostream&)) const; operator<<(std::ostream& manip(std::ostream&)) const &&
{
return {*this, manip};
}
template <typename T> template <typename T>
ScopedStream ScopedStream
operator<<(T const& t) const; operator<<(T const& t) const &&
{
return {*this, t};
}
ScopedStream
operator<<(std::ostream& manip(std::ostream&)) const &
{
currentJsonLogContext_.reuseJson();
return {*this, manip};
}
template <typename T>
ScopedStream
operator<<(T const& t) const &
{
currentJsonLogContext_.reuseJson();
return {*this, t};
}
/** @} */ /** @} */
private: private:
@@ -955,13 +984,6 @@ Journal::ScopedStream::operator<<(T const& t) const
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
template <typename T>
Journal::ScopedStream
Journal::Stream::operator<<(T const& t) const
{
return {*this, t};
}
namespace detail { namespace detail {
template <class CharT, class Traits = std::char_traits<CharT>> template <class CharT, class Traits = std::char_traits<CharT>>

View File

@@ -223,19 +223,13 @@ Journal::JsonLogContext::start(
}; };
thread_local ThreadIdStringInitializer const threadId; thread_local ThreadIdStringInitializer const threadId;
if (!messageBufferHandedOut_) messageOffset_ = 0;
{
returnStringBuffer(std::move(messageBuffer_));
messageBufferHandedOut_ = true;
}
messageBuffer_ = rentFromPool();
messageBufferHandedOut_ = false;
messageBuffer_.str().reserve(1024 * 5);
messageBuffer_.str().clear(); messageBuffer_.str().clear();
jsonWriter_ = detail::SimpleJsonWriter{&messageBuffer_.str()}; jsonWriter_ = detail::SimpleJsonWriter{&messageBuffer_.str()};
if (!jsonLogsEnabled_) if (!jsonLogsEnabled_)
{ {
messageBuffer_.str() = journalAttributes;
return; return;
} }
@@ -293,12 +287,28 @@ Journal::JsonLogContext::start(
hasMessageParams_ = false; hasMessageParams_ = false;
} }
void
Journal::JsonLogContext::reuseJson()
{
messageOffset_ = messageBuffer_.str().size();
}
void void
Journal::JsonLogContext::finish() Journal::JsonLogContext::finish()
{ {
messageBufferHandedOut_ = true; if (messageOffset_ != 0)
messageBuffer_ = {}; {
jsonWriter_ = {}; auto buffer = rentFromPool();
std::string_view json{messageBuffer_.str()};
buffer.str() = json.substr(0, messageOffset_);
messageBuffer_ = buffer;
}
else
{
messageBuffer_ = rentFromPool();
}
messageBuffer_.str().reserve(1024 * 5);
jsonWriter_ = detail::SimpleJsonWriter{&messageBuffer_.str()};
} }
void void
@@ -414,8 +424,8 @@ Journal::ScopedStream::~ScopedStream()
s = ""; s = "";
auto messageHandle = formatLog(s); auto messageHandle = formatLog(s);
m_sink.write(m_level, messageHandle);
currentJsonLogContext_.finish(); currentJsonLogContext_.finish();
m_sink.write(m_level, messageHandle);
} }
} }