Log size optimise

Signed-off-by: JCW <a1q123456@users.noreply.github.com>
This commit is contained in:
JCW
2025-09-10 01:14:16 +01:00
parent 05b89b7082
commit d8b840ca5c
4 changed files with 46 additions and 27 deletions

View File

@@ -324,6 +324,7 @@ public:
{
std::string buffer_;
detail::SimpleJsonWriter messageParamsWriter_;
bool hasMessageParams_ = false;
public:
JsonLogContext() : messageParamsWriter_(buffer_)
@@ -331,6 +332,26 @@ public:
buffer_.reserve(1024 * 5);
}
void
startMessageParams()
{
if (!hasMessageParams_)
{
writer().writeKey("Data");
writer().startObject();
hasMessageParams_ = true;
}
}
void
endMessageParams()
{
if (hasMessageParams_)
{
writer().endObject();
}
}
detail::SimpleJsonWriter&
writer()
{
@@ -1014,6 +1035,7 @@ operator<<(std::ostream& os, LogParameter<T> const& param)
os << param.value_;
return os;
}
beast::Journal::currentJsonLogContext_.startMessageParams();
detail::setJsonValue(
beast::Journal::currentJsonLogContext_.writer(),
param.name_,
@@ -1028,6 +1050,7 @@ operator<<(std::ostream& os, LogField<T> const& param)
{
if (!beast::Journal::m_jsonLogsEnabled)
return os;
beast::Journal::currentJsonLogContext_.startMessageParams();
detail::setJsonValue(
beast::Journal::currentJsonLogContext_.writer(),
param.name_,

View File

@@ -201,14 +201,14 @@ Logs::write(
result = s;
}
// Console output still immediate for responsiveness
if (!silent_)
std::cerr << result << '\n';
// Add to batch buffer for file output
{
std::lock_guard lock(batchMutex_);
// Console output still immediate for responsiveness
if (!silent_)
std::cerr << result << '\n';
size_t logSize = result.size() + 1; // +1 for newline
// If log won't fit in current write buffer, flush first

View File

@@ -216,7 +216,6 @@ Journal::JsonLogContext::reset(
ThreadIdStringInitializer()
{
std::stringstream threadIdStream;
threadIdStream.imbue(std::locale::classic());
threadIdStream << std::this_thread::get_id();
value = threadIdStream.str();
}
@@ -229,7 +228,7 @@ Journal::JsonLogContext::reset(
if (!journalAttributesJson.empty())
{
writer().writeKey("JournalParams");
writer().writeKey("Jnl");
writer().writeRaw(journalAttributesJson);
writer().endObject();
}
@@ -238,50 +237,45 @@ Journal::JsonLogContext::reset(
std::shared_lock lock(globalLogAttributesMutex_);
if (!globalLogAttributesJson_.empty())
{
writer().writeKey("GlobalParams");
writer().writeKey("Gbl");
writer().writeRaw(globalLogAttributesJson_);
writer().endObject();
}
}
writer().writeKey("ModuleName");
writer().writeString(moduleName);
writer().writeKey("Metadata");
writer().writeKey("Mtd");
writer().startObject();
writer().writeKey("Function");
writer().writeString(location.function_name());
writer().writeKey("Mdl");
writer().writeString(moduleName);
writer().writeKey("File");
writer().writeKey("Fl");
constexpr size_t FILE_NAME_KEEP_CHARS = 20;
std::string_view fileName = location.file_name();
constexpr size_t KEEP_CHARS = 10;
std::string_view trimmedFileName = (fileName.size() > KEEP_CHARS)
? fileName.substr(fileName.size() - KEEP_CHARS)
std::string_view trimmedFileName = (fileName.size() > FILE_NAME_KEEP_CHARS)
? fileName.substr(fileName.size() - FILE_NAME_KEEP_CHARS)
: fileName;
writer().writeString(trimmedFileName);
writer().writeKey("Line");
writer().writeKey("Ln");
writer().writeUInt(location.line());
writer().writeKey("ThreadId");
writer().writeKey("ThId");
writer().writeString(threadId.value);
auto severityStr = to_string(severity);
writer().writeKey("Level");
writer().writeKey("Lv");
writer().writeString(severityStr);
auto nowMs = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::system_clock::now().time_since_epoch())
.count();
writer().writeKey("Timestamp");
writer().writeInt(nowMs);
writer().writeKey("Time");
writer().writeKey("Tm");
writer().writeString(fastTimestampToString(nowMs));
writer().endObject();
writer().writeKey("MessageParams");
writer().startObject();
hasMessageParams_ = false;
}
void
@@ -302,9 +296,9 @@ Journal::formatLog(std::string const& message)
auto& writer = currentJsonLogContext_.writer();
writer.endObject();
currentJsonLogContext_.endMessageParams();
writer.writeKey("Message");
writer.writeKey("Msg");
writer.writeString(message);
writer.endObject();

View File

@@ -1139,8 +1139,10 @@ RclConsensusLogger::~RclConsensusLogger()
buffer.clear();
beast::detail::SimpleJsonWriter writer{buffer};
writer.startObject();
writer.writeKey("Message");
writer.writeKey("Msg");
writer.writeString(outSs.str());
writer.writeKey("Tm");
writer.writeString(to_string(std::chrono::system_clock::now()));
writer.endObject();
j_.sink().writeAlways(beast::severities::kInfo, writer.finish());
}