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_;
detail::SimpleJsonWriter jsonWriter_;
bool hasMessageParams_ = false;
bool messageBufferHandedOut_ = true;
std::size_t messageOffset_ = 0;
public:
JsonLogContext()
: messageBuffer_(rentFromPool())
, jsonWriter_(&messageBuffer_.str())
{}
StringBuffer
messageBuffer() { return messageBuffer_; }
@@ -493,6 +498,9 @@ public:
return jsonWriter_;
}
void
reuseJson();
void
finish();
@@ -739,11 +747,32 @@ public:
/** Output stream support. */
/** @{ */
ScopedStream
operator<<(std::ostream& manip(std::ostream&)) const;
operator<<(std::ostream& manip(std::ostream&)) const &&
{
return {*this, manip};
}
template <typename T>
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:
@@ -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 {
template <class CharT, class Traits = std::char_traits<CharT>>

View File

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