Skip to content

Commit 6d85788

Browse files
committed
Fix
Signed-off-by: JCW <[email protected]>
1 parent 9262039 commit 6d85788

File tree

2 files changed

+102
-86
lines changed

2 files changed

+102
-86
lines changed

include/xrpl/beast/utility/Journal.h

Lines changed: 83 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@
2222

2323
#include <xrpl/beast/utility/instrumentation.h>
2424

25+
#include <boost/asio/execution/allocator.hpp>
26+
#include <boost/coroutine/attributes.hpp>
27+
#include <boost/system/result.hpp>
28+
2529
#include <rapidjson/document.h>
30+
#include <rapidjson/stringbuffer.h>
31+
#include <rapidjson/writer.h>
2632

2733
#include <deque>
2834
#include <optional>
@@ -174,23 +180,27 @@ class Journal
174180

175181
class JsonLogContext
176182
{
177-
rapidjson::Value messageParams_;
178-
rapidjson::MemoryPoolAllocator<> allocator_;
183+
rapidjson::StringBuffer buffer_;
184+
rapidjson::Writer<rapidjson::StringBuffer> messageParamsWriter_;
179185
std::optional<std::string> journalAttributesJson_;
180186

181187
public:
182-
JsonLogContext() = default;
188+
JsonLogContext()
189+
: messageParamsWriter_(buffer_)
190+
{
183191

184-
rapidjson::MemoryPoolAllocator<>&
185-
allocator()
192+
}
193+
194+
rapidjson::Writer<rapidjson::StringBuffer>&
195+
writer()
186196
{
187-
return allocator_;
197+
return messageParamsWriter_;
188198
}
189199

190-
rapidjson::Value&
200+
char const*
191201
messageParams()
192202
{
193-
return messageParams_;
203+
return buffer_.GetString();
194204
}
195205

196206
std::optional<std::string>&
@@ -461,9 +471,28 @@ class Journal
461471
/** Journal has no default constructor. */
462472
Journal() = delete;
463473

474+
template <typename TAttributesFactory>
464475
Journal(
465476
Journal const& other,
466-
std::optional<JsonLogAttributes> attributes = std::nullopt);
477+
TAttributesFactory&& attributesFactory = nullptr)
478+
: m_attributes(other.m_attributes)
479+
, m_sink(other.m_sink)
480+
, m_attributesJson(other.m_attributesJson)
481+
{
482+
/*
483+
if constexpr (!std::is_same_v<std::decay_t<TAttributesFactory>, std::nullptr_t>)
484+
{
485+
if (attributes.has_value())
486+
{
487+
if (m_attributes)
488+
m_attributes->combine(attributes->contextValues_);
489+
else
490+
m_attributes = std::move(attributes);
491+
}
492+
rebuildAttributeJson();
493+
}
494+
*/
495+
}
467496

468497
/** Create a journal that writes to the specified sink. */
469498
explicit Journal(
@@ -699,39 +728,59 @@ using logwstream = basic_logstream<wchar_t>;
699728
namespace ripple::log {
700729

701730
namespace detail {
702-
template <typename T>
731+
template <typename T, typename OutputStream>
703732
void
704733
setJsonValue(
705-
rapidjson::Value& object,
706-
rapidjson::MemoryPoolAllocator<>& allocator,
734+
rapidjson::Writer<OutputStream>& writer,
707735
char const* name,
708736
T&& value,
709737
std::ostream* outStream)
710738
{
711739
using ValueType = std::decay_t<T>;
712-
rapidjson::Value jsonValue;
713-
if constexpr (std::constructible_from<
714-
rapidjson::Value,
715-
ValueType,
716-
rapidjson::MemoryPoolAllocator<>&>)
740+
writer.Key(name);
741+
if constexpr (std::is_integral_v<ValueType>)
742+
{
743+
if constexpr (std::is_signed_v<ValueType>)
744+
{
745+
writer.Int64(value);
746+
}
747+
else
748+
{
749+
writer.Uint64(value);
750+
}
751+
if (outStream)
752+
{
753+
(*outStream) << value;
754+
}
755+
}
756+
else if constexpr (std::is_floating_point_v<ValueType>)
757+
{
758+
writer.Double(value);
759+
760+
if (outStream)
761+
{
762+
(*outStream) << value;
763+
}
764+
}
765+
else if constexpr (std::is_same_v<ValueType, bool>)
717766
{
718-
jsonValue = rapidjson::Value{value, allocator};
767+
writer.Bool(value);
719768
if (outStream)
720769
{
721770
(*outStream) << value;
722771
}
723772
}
724-
else if constexpr (std::constructible_from<rapidjson::Value, ValueType>)
773+
else if constexpr (std::is_same_v<ValueType, char const*> || std::is_same_v<ValueType, char*>)
725774
{
726-
jsonValue = rapidjson::Value{value};
775+
writer.String(value);
727776
if (outStream)
728777
{
729778
(*outStream) << value;
730779
}
731780
}
732-
else if constexpr (std::same_as<ValueType, std::string>)
781+
else if constexpr (std::is_same_v<ValueType, std::string>)
733782
{
734-
jsonValue = rapidjson::Value{value.c_str(), allocator};
783+
writer.String(value.c_str(), value.length());
735784
if (outStream)
736785
{
737786
(*outStream) << value;
@@ -742,17 +791,13 @@ setJsonValue(
742791
std::ostringstream oss;
743792
oss << value;
744793

745-
jsonValue = rapidjson::Value{oss.str().c_str(), allocator};
794+
writer.String(oss.str().c_str(), oss.str().length());
746795

747796
if (outStream)
748797
{
749798
(*outStream) << oss.str();
750799
}
751800
}
752-
753-
object.RemoveMember(name);
754-
object.AddMember(
755-
rapidjson::StringRef(name), std::move(jsonValue), allocator);
756801
}
757802
} // namespace detail
758803

@@ -763,8 +808,7 @@ operator<<(std::ostream& os, LogParameter<T> const& param)
763808
if (!beast::Journal::m_jsonLogsEnabled)
764809
return os;
765810
detail::setJsonValue(
766-
beast::Journal::currentJsonLogContext_.messageParams(),
767-
beast::Journal::currentJsonLogContext_.allocator(),
811+
beast::Journal::currentJsonLogContext_.writer(),
768812
param.name_,
769813
param.value_,
770814
&os);
@@ -778,8 +822,7 @@ operator<<(std::ostream& os, LogField<T> const& param)
778822
if (!beast::Journal::m_jsonLogsEnabled)
779823
return os;
780824
detail::setJsonValue(
781-
beast::Journal::currentJsonLogContext_.messageParams(),
782-
beast::Journal::currentJsonLogContext_.allocator(),
825+
beast::Journal::currentJsonLogContext_.writer(),
783826
param.name_,
784827
param.value_,
785828
nullptr);
@@ -801,20 +844,17 @@ field(char const* name, T&& value)
801844
}
802845

803846
template <typename... Pair>
804-
[[nodiscard]] beast::Journal::JsonLogAttributes
847+
[[nodiscard]] auto
805848
attributes(Pair&&... pairs)
806849
{
807-
beast::Journal::JsonLogAttributes result;
808-
809-
(detail::setJsonValue(
810-
result.contextValues(),
811-
result.allocator(),
812-
pairs.first,
813-
pairs.second,
814-
nullptr),
815-
...);
816-
817-
return result;
850+
return [&](rapidjson::Writer<rapidjson::Writer<char>>& writer) {
851+
(detail::setJsonValue(
852+
writer,
853+
pairs.first,
854+
pairs.second,
855+
nullptr),
856+
...);
857+
};
818858
}
819859

820860
template <typename T>

src/libxrpl/beast/utility/beast_Journal.cpp

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -171,21 +171,6 @@ Journal::JsonLogAttributes::combine(
171171
}
172172
}
173173

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-
189174
void
190175
Journal::addGlobalAttributes(JsonLogAttributes globalLogAttributes)
191176
{
@@ -224,40 +209,30 @@ Journal::JsonLogContext::reset(
224209

225210
journalAttributesJson_ = journalAttributesJson;
226211

227-
messageParams_.SetObject();
212+
buffer_.Clear();
228213

229-
messageParams_.AddMember(
230-
rapidjson::StringRef("Function"),
231-
rapidjson::Value{location.function_name(), allocator_},
232-
allocator_);
214+
writer().StartObject();
233215

234-
messageParams_.AddMember(
235-
rapidjson::StringRef("File"),
236-
rapidjson::Value{location.file_name(), allocator_},
237-
allocator_);
216+
writer().Key("Function");
217+
writer().String(location.function_name());
238218

239-
messageParams_.AddMember(
240-
rapidjson::StringRef("Line"),
241-
location.line(),
242-
allocator_);
219+
writer().Key("File");
220+
writer().String(location.file_name());
243221

244-
messageParams_.AddMember(
245-
rapidjson::StringRef("ThreadId"),
246-
rapidjson::Value{threadId.value.c_str(), allocator_},
247-
allocator_);
222+
writer().Key("Line");
223+
writer().Int64(location.line());
224+
225+
writer().Key("ThreadId");
226+
writer().String(threadId.value.c_str());
248227

249228
auto severityStr = to_string(severity);
250-
messageParams_.AddMember(
251-
rapidjson::StringRef("Level"),
252-
rapidjson::Value{severityStr.c_str(), allocator_},
253-
allocator_);
229+
writer().Key("Level");
230+
writer().String(severityStr.c_str());
254231

255-
messageParams_.AddMember(
256-
rapidjson::StringRef("Time"),
257-
std::chrono::duration_cast<std::chrono::milliseconds>(
232+
writer().Key("Time");
233+
writer().Uint64(std::chrono::duration_cast<std::chrono::milliseconds>(
258234
std::chrono::system_clock::now().time_since_epoch())
259-
.count(),
260-
allocator_);
235+
.count());
261236
}
262237

263238
void
@@ -276,7 +251,8 @@ Journal::formatLog(std::string&& message)
276251
return message;
277252
}
278253

279-
auto& messageParams = currentJsonLogContext_.messageParams();
254+
currentJsonLogContext_.writer().EndObject();
255+
auto messageParams = currentJsonLogContext_.messageParams();
280256

281257
rapidjson::StringBuffer buffer;
282258
rapidjson::Writer writer(buffer);
@@ -301,7 +277,7 @@ Journal::formatLog(std::string&& message)
301277
}
302278

303279
writer.Key("MessageParams");
304-
messageParams.Accept(writer);
280+
writer.RawValue(messageParams, std::strlen(messageParams), rapidjson::kObjectType);
305281

306282
writer.Key("Message");
307283
writer.String(message.c_str(), message.length());

0 commit comments

Comments
 (0)