22
22
23
23
#include < xrpl/beast/utility/instrumentation.h>
24
24
25
+ #include < boost/asio/execution/allocator.hpp>
26
+ #include < boost/coroutine/attributes.hpp>
27
+ #include < boost/system/result.hpp>
28
+
25
29
#include < rapidjson/document.h>
30
+ #include < rapidjson/stringbuffer.h>
31
+ #include < rapidjson/writer.h>
26
32
27
33
#include < deque>
28
34
#include < optional>
@@ -174,23 +180,27 @@ class Journal
174
180
175
181
class JsonLogContext
176
182
{
177
- rapidjson::Value messageParams_ ;
178
- rapidjson::MemoryPoolAllocator<> allocator_ ;
183
+ rapidjson::StringBuffer buffer_ ;
184
+ rapidjson::Writer<rapidjson::StringBuffer> messageParamsWriter_ ;
179
185
std::optional<std::string> journalAttributesJson_;
180
186
181
187
public:
182
- JsonLogContext () = default ;
188
+ JsonLogContext ()
189
+ : messageParamsWriter_(buffer_)
190
+ {
183
191
184
- rapidjson::MemoryPoolAllocator<>&
185
- allocator ()
192
+ }
193
+
194
+ rapidjson::Writer<rapidjson::StringBuffer>&
195
+ writer ()
186
196
{
187
- return allocator_ ;
197
+ return messageParamsWriter_ ;
188
198
}
189
199
190
- rapidjson::Value&
200
+ char const *
191
201
messageParams ()
192
202
{
193
- return messageParams_ ;
203
+ return buffer_. GetString () ;
194
204
}
195
205
196
206
std::optional<std::string>&
@@ -461,9 +471,28 @@ class Journal
461
471
/* * Journal has no default constructor. */
462
472
Journal () = delete ;
463
473
474
+ template <typename TAttributesFactory>
464
475
Journal (
465
476
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
+ }
467
496
468
497
/* * Create a journal that writes to the specified sink. */
469
498
explicit Journal (
@@ -699,39 +728,59 @@ using logwstream = basic_logstream<wchar_t>;
699
728
namespace ripple ::log {
700
729
701
730
namespace detail {
702
- template <typename T>
731
+ template <typename T, typename OutputStream >
703
732
void
704
733
setJsonValue (
705
- rapidjson::Value& object,
706
- rapidjson::MemoryPoolAllocator<>& allocator,
734
+ rapidjson::Writer<OutputStream>& writer,
707
735
char const * name,
708
736
T&& value,
709
737
std::ostream* outStream)
710
738
{
711
739
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 >)
717
766
{
718
- jsonValue = rapidjson::Value{ value, allocator} ;
767
+ writer. Bool ( value) ;
719
768
if (outStream)
720
769
{
721
770
(*outStream) << value;
722
771
}
723
772
}
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 * >)
725
774
{
726
- jsonValue = rapidjson::Value{ value} ;
775
+ writer. String ( value) ;
727
776
if (outStream)
728
777
{
729
778
(*outStream) << value;
730
779
}
731
780
}
732
- else if constexpr (std::same_as <ValueType, std::string>)
781
+ else if constexpr (std::is_same_v <ValueType, std::string>)
733
782
{
734
- jsonValue = rapidjson::Value{ value.c_str (), allocator} ;
783
+ writer. String ( value.c_str (), value. length ()) ;
735
784
if (outStream)
736
785
{
737
786
(*outStream) << value;
@@ -742,17 +791,13 @@ setJsonValue(
742
791
std::ostringstream oss;
743
792
oss << value;
744
793
745
- jsonValue = rapidjson::Value{ oss.str ().c_str (), allocator} ;
794
+ writer. String ( oss.str ().c_str (), oss. str (). length ()) ;
746
795
747
796
if (outStream)
748
797
{
749
798
(*outStream) << oss.str ();
750
799
}
751
800
}
752
-
753
- object.RemoveMember (name);
754
- object.AddMember (
755
- rapidjson::StringRef (name), std::move (jsonValue), allocator);
756
801
}
757
802
} // namespace detail
758
803
@@ -763,8 +808,7 @@ operator<<(std::ostream& os, LogParameter<T> const& param)
763
808
if (!beast::Journal::m_jsonLogsEnabled)
764
809
return os;
765
810
detail::setJsonValue (
766
- beast::Journal::currentJsonLogContext_.messageParams (),
767
- beast::Journal::currentJsonLogContext_.allocator (),
811
+ beast::Journal::currentJsonLogContext_.writer (),
768
812
param.name_ ,
769
813
param.value_ ,
770
814
&os);
@@ -778,8 +822,7 @@ operator<<(std::ostream& os, LogField<T> const& param)
778
822
if (!beast::Journal::m_jsonLogsEnabled)
779
823
return os;
780
824
detail::setJsonValue (
781
- beast::Journal::currentJsonLogContext_.messageParams (),
782
- beast::Journal::currentJsonLogContext_.allocator (),
825
+ beast::Journal::currentJsonLogContext_.writer (),
783
826
param.name_ ,
784
827
param.value_ ,
785
828
nullptr );
@@ -801,20 +844,17 @@ field(char const* name, T&& value)
801
844
}
802
845
803
846
template <typename ... Pair>
804
- [[nodiscard]] beast::Journal::JsonLogAttributes
847
+ [[nodiscard]] auto
805
848
attributes (Pair&&... pairs)
806
849
{
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
+ };
818
858
}
819
859
820
860
template <typename T>
0 commit comments