Skip to content

Commit 9262039

Browse files
committed
Optimisation
Signed-off-by: JCW <[email protected]>
1 parent c7f0eb8 commit 9262039

File tree

3 files changed

+187
-147
lines changed

3 files changed

+187
-147
lines changed

include/xrpl/beast/utility/Journal.h

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,9 @@ class Journal
174174

175175
class JsonLogContext
176176
{
177-
rapidjson::Value attributes_;
177+
rapidjson::Value messageParams_;
178178
rapidjson::MemoryPoolAllocator<> allocator_;
179+
std::optional<std::string> journalAttributesJson_;
179180

180181
public:
181182
JsonLogContext() = default;
@@ -189,28 +190,30 @@ class Journal
189190
rapidjson::Value&
190191
messageParams()
191192
{
192-
return attributes_["Params"];
193+
return messageParams_;
193194
}
194195

195-
rapidjson::Value&
196-
attributes()
196+
std::optional<std::string>&
197+
journalAttributesJson()
197198
{
198-
return attributes_;
199+
return journalAttributesJson_;
199200
}
200201

201202
void
202203
reset(
203204
std::source_location location,
204205
severities::Severity severity,
205-
std::optional<JsonLogAttributes> const& attributes) noexcept;
206+
std::optional<std::string> const& journalAttributesJson) noexcept;
206207
};
207208

208209
private:
209210
// Severity level / threshold of a Journal message.
210211
using Severity = severities::Severity;
211212

212213
std::optional<JsonLogAttributes> m_attributes;
214+
std::optional<std::string> m_attributesJson;
213215
static std::optional<JsonLogAttributes> globalLogAttributes_;
216+
static std::optional<std::string> globalLogAttributesJson_;
214217
static std::mutex globalLogAttributesMutex_;
215218
static bool m_jsonLogsEnabled;
216219

@@ -227,6 +230,9 @@ class Journal
227230
static std::string
228231
formatLog(std::string&& message);
229232

233+
void
234+
rebuildAttributeJson();
235+
230236
public:
231237
//--------------------------------------------------------------------------
232238

@@ -457,18 +463,8 @@ class Journal
457463

458464
Journal(
459465
Journal const& other,
460-
std::optional<JsonLogAttributes> attributes = std::nullopt)
461-
: m_attributes(other.m_attributes)
462-
, m_sink(other.m_sink)
463-
{
464-
if (attributes.has_value())
465-
{
466-
if (m_attributes)
467-
m_attributes->combine(attributes->contextValues_);
468-
else
469-
m_attributes = std::move(attributes);
470-
}
471-
}
466+
std::optional<JsonLogAttributes> attributes = std::nullopt);
467+
472468
/** Create a journal that writes to the specified sink. */
473469
explicit Journal(
474470
Sink& sink,
@@ -481,6 +477,7 @@ class Journal
481477
m_attributes = std::move(attributes);
482478
m_attributes->setModuleName(name);
483479
}
480+
rebuildAttributeJson();
484481
}
485482

486483
Journal&
@@ -491,6 +488,7 @@ class Journal
491488

492489
m_sink = other.m_sink;
493490
m_attributes = other.m_attributes;
491+
rebuildAttributeJson();
494492
return *this;
495493
}
496494

@@ -499,6 +497,7 @@ class Journal
499497
{
500498
m_sink = other.m_sink;
501499
m_attributes = std::move(other.m_attributes);
500+
rebuildAttributeJson();
502501
return *this;
503502
}
504503

@@ -586,19 +585,11 @@ class Journal
586585
{
587586
std::lock_guard lock(globalLogAttributesMutex_);
588587
globalLogAttributes_ = std::nullopt;
588+
globalLogAttributesJson_ = std::nullopt;
589589
}
590590

591591
static void
592-
addGlobalAttributes(JsonLogAttributes globalLogAttributes)
593-
{
594-
std::lock_guard lock(globalLogAttributesMutex_);
595-
if (!globalLogAttributes_)
596-
{
597-
globalLogAttributes_ = JsonLogAttributes{};
598-
}
599-
globalLogAttributes_->combine(
600-
globalLogAttributes.contextValues());
601-
}
592+
addGlobalAttributes(JsonLogAttributes globalLogAttributes);
602593
};
603594

604595
#ifndef __INTELLISENSE__

src/libxrpl/beast/utility/beast_Journal.cpp

Lines changed: 94 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
namespace beast {
3333

3434
std::optional<Journal::JsonLogAttributes> Journal::globalLogAttributes_;
35+
std::optional<std::string> Journal::globalLogAttributesJson_;
36+
3537
std::mutex Journal::globalLogAttributesMutex_;
3638
bool Journal::m_jsonLogsEnabled = false;
3739
thread_local Journal::JsonLogContext Journal::currentJsonLogContext_{};
@@ -169,11 +171,44 @@ Journal::JsonLogAttributes::combine(
169171
}
170172
}
171173

174+
Journal::Journal(
175+
Journal const& other,
176+
std::optional<JsonLogAttributes> attributes)
177+
: m_attributes(other.m_attributes), m_sink(other.m_sink)
178+
{
179+
if (attributes.has_value())
180+
{
181+
if (m_attributes)
182+
m_attributes->combine(attributes->contextValues_);
183+
else
184+
m_attributes = std::move(attributes);
185+
}
186+
rebuildAttributeJson();
187+
}
188+
189+
void
190+
Journal::addGlobalAttributes(JsonLogAttributes globalLogAttributes)
191+
{
192+
std::lock_guard lock(globalLogAttributesMutex_);
193+
if (!globalLogAttributes_)
194+
{
195+
globalLogAttributes_ = JsonLogAttributes{};
196+
}
197+
globalLogAttributes_->combine(globalLogAttributes.contextValues());
198+
199+
rapidjson::StringBuffer buffer;
200+
rapidjson::Writer writer{buffer};
201+
202+
globalLogAttributes_->contextValues().Accept(writer);
203+
204+
globalLogAttributesJson_ = {buffer.GetString()};
205+
}
206+
172207
void
173208
Journal::JsonLogContext::reset(
174209
std::source_location location,
175210
severities::Severity severity,
176-
std::optional<JsonLogAttributes> const& attributes) noexcept
211+
std::optional<std::string> const& journalAttributesJson) noexcept
177212
{
178213
struct ThreadIdStringInitializer
179214
{
@@ -187,67 +222,37 @@ Journal::JsonLogContext::reset(
187222
};
188223
thread_local ThreadIdStringInitializer const threadId;
189224

190-
attributes_.SetObject();
191-
if (globalLogAttributes_.has_value())
192-
{
193-
attributes_.CopyFrom(globalLogAttributes_->contextValues(), allocator_);
194-
if (attributes.has_value())
195-
{
196-
for (auto const& [key, value] :
197-
attributes->contextValues().GetObject())
198-
{
199-
attributes_.RemoveMember(key);
200-
201-
rapidjson::Value jsonValue;
202-
jsonValue.CopyFrom(value, allocator_);
203-
204-
attributes_.AddMember(
205-
rapidjson::Value{key, allocator_},
206-
rapidjson::Value{value, allocator_},
207-
allocator_);
208-
}
209-
}
210-
}
211-
else if (attributes.has_value())
212-
{
213-
attributes_.CopyFrom(attributes->contextValues(), allocator_);
214-
}
225+
journalAttributesJson_ = journalAttributesJson;
226+
227+
messageParams_.SetObject();
215228

216-
attributes_.RemoveMember("Function");
217-
attributes_.AddMember(
229+
messageParams_.AddMember(
218230
rapidjson::StringRef("Function"),
219231
rapidjson::Value{location.function_name(), allocator_},
220232
allocator_);
221233

222-
attributes_.RemoveMember("File");
223-
attributes_.AddMember(
234+
messageParams_.AddMember(
224235
rapidjson::StringRef("File"),
225236
rapidjson::Value{location.file_name(), allocator_},
226237
allocator_);
227238

228-
attributes_.RemoveMember("Line");
229-
attributes_.AddMember(
239+
messageParams_.AddMember(
230240
rapidjson::StringRef("Line"),
231241
location.line(),
232242
allocator_);
233-
attributes_.RemoveMember("ThreadId");
234-
attributes_.AddMember(
243+
244+
messageParams_.AddMember(
235245
rapidjson::StringRef("ThreadId"),
236246
rapidjson::Value{threadId.value.c_str(), allocator_},
237247
allocator_);
238-
attributes_.RemoveMember("Params");
239-
attributes_.AddMember(
240-
rapidjson::StringRef("Params"),
241-
rapidjson::Value{rapidjson::kObjectType},
242-
allocator_);
248+
243249
auto severityStr = to_string(severity);
244-
attributes_.RemoveMember("Level");
245-
attributes_.AddMember(
250+
messageParams_.AddMember(
246251
rapidjson::StringRef("Level"),
247252
rapidjson::Value{severityStr.c_str(), allocator_},
248253
allocator_);
249-
attributes_.RemoveMember("Time");
250-
attributes_.AddMember(
254+
255+
messageParams_.AddMember(
251256
rapidjson::StringRef("Time"),
252257
std::chrono::duration_cast<std::chrono::milliseconds>(
253258
std::chrono::system_clock::now().time_since_epoch())
@@ -260,7 +265,7 @@ Journal::initMessageContext(
260265
std::source_location location,
261266
severities::Severity severity) const
262267
{
263-
currentJsonLogContext_.reset(location, severity, m_attributes);
268+
currentJsonLogContext_.reset(location, severity, m_attributesJson);
264269
}
265270

266271
std::string
@@ -271,23 +276,59 @@ Journal::formatLog(std::string&& message)
271276
return message;
272277
}
273278

274-
auto& attributes = currentJsonLogContext_.attributes();
275-
276-
attributes.RemoveMember("Message");
277-
attributes.AddMember(
278-
rapidjson::StringRef("Message"),
279-
rapidjson::Value{rapidjson::StringRef(message.c_str()), currentJsonLogContext_.allocator()},
280-
currentJsonLogContext_.allocator()
281-
);
279+
auto& messageParams = currentJsonLogContext_.messageParams();
282280

283281
rapidjson::StringBuffer buffer;
284282
rapidjson::Writer writer(buffer);
285283

286-
attributes.Accept(writer);
284+
writer.StartObject();
285+
if (globalLogAttributesJson_.has_value())
286+
{
287+
writer.Key("GlobalParams");
288+
writer.RawValue(
289+
globalLogAttributesJson_->c_str(),
290+
globalLogAttributesJson_->length(),
291+
rapidjson::kObjectType);
292+
}
293+
294+
if (currentJsonLogContext_.journalAttributesJson().has_value())
295+
{
296+
writer.Key("JournalParams");
297+
writer.RawValue(
298+
currentJsonLogContext_.journalAttributesJson()->c_str(),
299+
currentJsonLogContext_.journalAttributesJson()->length(),
300+
rapidjson::kObjectType);
301+
}
302+
303+
writer.Key("MessageParams");
304+
messageParams.Accept(writer);
305+
306+
writer.Key("Message");
307+
writer.String(message.c_str(), message.length());
308+
309+
writer.EndObject();
287310

288311
return {buffer.GetString()};
289312
}
290313

314+
void
315+
Journal::rebuildAttributeJson()
316+
{
317+
if (m_attributes.has_value())
318+
{
319+
rapidjson::StringBuffer buffer;
320+
rapidjson::Writer writer{buffer};
321+
322+
m_attributes->contextValues().Accept(writer);
323+
324+
m_attributesJson = {buffer.GetString()};
325+
}
326+
else
327+
{
328+
m_attributesJson = {};
329+
}
330+
}
331+
291332
void
292333
Journal::enableStructuredJournal()
293334
{

0 commit comments

Comments
 (0)