@@ -144,8 +144,8 @@ class Journal
144
144
void
145
145
setModuleName (std::string const & name);
146
146
147
- [[nodiscard]] static JsonLogAttributes
148
- combine (AttributeFields const & a, AttributeFields const & b );
147
+ void
148
+ combine (AttributeFields const & from );
149
149
150
150
AttributeFields&
151
151
contextValues ()
@@ -172,22 +172,37 @@ class Journal
172
172
friend class Journal ;
173
173
};
174
174
175
- struct JsonLogContext
175
+ class JsonLogContext
176
176
{
177
- std::source_location location = {};
178
- rapidjson::Value messageParams;
179
- rapidjson::MemoryPoolAllocator<> allocator;
177
+ rapidjson::Value attributes_;
178
+ rapidjson::MemoryPoolAllocator<> allocator_;
180
179
180
+ public:
181
181
JsonLogContext () = default ;
182
182
183
- void
184
- reset (std::source_location location_) noexcept
183
+ rapidjson::MemoryPoolAllocator<>&
184
+ allocator ()
185
+ {
186
+ return allocator_;
187
+ }
188
+
189
+ rapidjson::Value&
190
+ messageParams ()
185
191
{
186
- location = location_;
187
- messageParams = rapidjson::Value{};
188
- messageParams.SetObject ();
189
- allocator.Clear ();
192
+ return attributes_[" Params" ];
190
193
}
194
+
195
+ rapidjson::Value&
196
+ attributes ()
197
+ {
198
+ return attributes_;
199
+ }
200
+
201
+ void
202
+ reset (
203
+ std::source_location location,
204
+ severities::Severity severity,
205
+ std::optional<JsonLogAttributes> const & attributes) noexcept ;
191
206
};
192
207
193
208
private:
@@ -204,14 +219,13 @@ class Journal
204
219
// Invariant: m_sink always points to a valid Sink
205
220
Sink* m_sink = nullptr ;
206
221
207
- static void
208
- initMessageContext (std::source_location location);
222
+ void
223
+ initMessageContext (
224
+ std::source_location location,
225
+ severities::Severity severity) const ;
209
226
210
227
static std::string
211
- formatLog (
212
- std::string const & message,
213
- severities::Severity severity,
214
- std::optional<JsonLogAttributes> const & attributes = std::nullopt );
228
+ formatLog (std::string const & message);
215
229
216
230
public:
217
231
// --------------------------------------------------------------------------
@@ -304,25 +318,16 @@ class Journal
304
318
{
305
319
public:
306
320
ScopedStream (ScopedStream const & other)
307
- : ScopedStream(other.m_attributes, other. m_sink, other.m_level)
321
+ : ScopedStream(other.m_sink, other.m_level)
308
322
{
309
323
}
310
324
311
- ScopedStream (
312
- std::optional<JsonLogAttributes> attributes,
313
- Sink& sink,
314
- Severity level);
325
+ ScopedStream (Sink& sink, Severity level);
315
326
316
327
template <typename T>
317
- ScopedStream (
318
- std::optional<JsonLogAttributes> attributes,
319
- Stream const & stream,
320
- T const & t);
328
+ ScopedStream (Stream const & stream, T const & t);
321
329
322
- ScopedStream (
323
- std::optional<JsonLogAttributes> attributes,
324
- Stream const & stream,
325
- std::ostream& manip (std::ostream&));
330
+ ScopedStream (Stream const & stream, std::ostream& manip (std::ostream&));
326
331
327
332
ScopedStream&
328
333
operator =(ScopedStream const &) = delete ;
@@ -343,7 +348,6 @@ class Journal
343
348
operator <<(T const & t) const ;
344
349
345
350
private:
346
- std::optional<JsonLogAttributes> m_attributes;
347
351
Sink& m_sink;
348
352
Severity const m_level;
349
353
std::ostringstream mutable m_ostream;
@@ -378,20 +382,15 @@ class Journal
378
382
379
383
Constructor is inlined so checking active() very inexpensive.
380
384
*/
381
- Stream (
382
- std::optional<JsonLogAttributes> attributes,
383
- Sink& sink,
384
- Severity level)
385
- : m_attributes(std::move(attributes)), m_sink(sink), m_level(level)
385
+ Stream (Sink& sink, Severity level) : m_sink(sink), m_level(level)
386
386
{
387
387
XRPL_ASSERT (
388
388
m_level < severities::kDisabled ,
389
389
" beast::Journal::Stream::Stream : maximum level" );
390
390
}
391
391
392
392
/* * Construct or copy another Stream. */
393
- Stream (Stream const & other)
394
- : Stream(other.m_attributes, other.m_sink, other.m_level)
393
+ Stream (Stream const & other) : Stream(other.m_sink, other.m_level)
395
394
{
396
395
}
397
396
@@ -438,7 +437,6 @@ class Journal
438
437
/* * @} */
439
438
440
439
private:
441
- std::optional<JsonLogAttributes> m_attributes;
442
440
Sink& m_sink;
443
441
Severity m_level;
444
442
};
@@ -467,9 +465,8 @@ class Journal
467
465
if (other.m_attributes .has_value ())
468
466
{
469
467
if (m_attributes.has_value ())
470
- m_attributes = JsonLogAttributes::combine (
471
- other.m_attributes ->contextValues_ ,
472
- m_attributes->contextValues_ );
468
+ m_attributes->combine (
469
+ other.m_attributes ->contextValues_ );
473
470
else
474
471
m_attributes = other.m_attributes ;
475
472
}
@@ -516,9 +513,11 @@ class Journal
516
513
517
514
/* * Returns a stream for this sink, with the specified severity level. */
518
515
Stream
519
- stream (Severity level) const
516
+ stream (Severity level, std::source_location location = std::source_location::current() ) const
520
517
{
521
- return Stream (m_attributes, *m_sink, level);
518
+ if (m_jsonLogsEnabled)
519
+ initMessageContext (location, level);
520
+ return Stream (*m_sink, level);
522
521
}
523
522
524
523
/* * Returns `true` if any message would be logged at this severity level.
@@ -537,24 +536,24 @@ class Journal
537
536
trace (std::source_location location = std::source_location::current()) const
538
537
{
539
538
if (m_jsonLogsEnabled)
540
- initMessageContext (location);
541
- return {m_attributes, *m_sink, severities::kTrace };
539
+ initMessageContext (location, severities:: kTrace );
540
+ return {*m_sink, severities::kTrace };
542
541
}
543
542
544
543
Stream
545
544
debug (std::source_location location = std::source_location::current()) const
546
545
{
547
546
if (m_jsonLogsEnabled)
548
- initMessageContext (location);
549
- return {m_attributes, *m_sink, severities::kDebug };
547
+ initMessageContext (location, severities:: kDebug );
548
+ return {*m_sink, severities::kDebug };
550
549
}
551
550
552
551
Stream
553
552
info (std::source_location location = std::source_location::current()) const
554
553
{
555
554
if (m_jsonLogsEnabled)
556
- initMessageContext (location);
557
- return {m_attributes, *m_sink, severities::kInfo };
555
+ initMessageContext (location, severities:: kInfo );
556
+ return {*m_sink, severities::kInfo };
558
557
}
559
558
560
559
Stream
@@ -563,24 +562,24 @@ class Journal
563
562
char const * a = " a" ;
564
563
rapidjson::Value v{a, 1 };
565
564
if (m_jsonLogsEnabled)
566
- initMessageContext (location);
567
- return {m_attributes, *m_sink, severities::kWarning };
565
+ initMessageContext (location, severities:: kWarning );
566
+ return {*m_sink, severities::kWarning };
568
567
}
569
568
570
569
Stream
571
570
error (std::source_location location = std::source_location::current()) const
572
571
{
573
572
if (m_jsonLogsEnabled)
574
- initMessageContext (location);
575
- return {m_attributes, *m_sink, severities::kError };
573
+ initMessageContext (location, severities:: kError );
574
+ return {*m_sink, severities::kError };
576
575
}
577
576
578
577
Stream
579
578
fatal (std::source_location location = std::source_location::current()) const
580
579
{
581
580
if (m_jsonLogsEnabled)
582
- initMessageContext (location);
583
- return {m_attributes, *m_sink, severities::kFatal };
581
+ initMessageContext (location, severities:: kFatal );
582
+ return {*m_sink, severities::kFatal };
584
583
}
585
584
/* * @} */
586
585
@@ -599,8 +598,7 @@ class Journal
599
598
{
600
599
globalLogAttributes_ = JsonLogAttributes{};
601
600
}
602
- globalLogAttributes_ = JsonLogAttributes::combine (
603
- globalLogAttributes_->contextValues (),
601
+ globalLogAttributes_->combine (
604
602
globalLogAttributes.contextValues ());
605
603
}
606
604
};
@@ -617,11 +615,8 @@ static_assert(std::is_nothrow_destructible<Journal>::value == true, "");
617
615
// ------------------------------------------------------------------------------
618
616
619
617
template <typename T>
620
- Journal::ScopedStream::ScopedStream (
621
- std::optional<JsonLogAttributes> attributes,
622
- Stream const & stream,
623
- T const & t)
624
- : ScopedStream(std::move(attributes), stream.sink(), stream.level())
618
+ Journal::ScopedStream::ScopedStream (Stream const & stream, T const & t)
619
+ : ScopedStream(stream.sink(), stream.level())
625
620
{
626
621
m_ostream << t;
627
622
}
@@ -640,7 +635,7 @@ template <typename T>
640
635
Journal::ScopedStream
641
636
Journal::Stream::operator <<(T const & t) const
642
637
{
643
- return {m_attributes, *this , t};
638
+ return {*this , t};
644
639
}
645
640
646
641
namespace detail {
@@ -766,6 +761,7 @@ setJsonValue(
766
761
}
767
762
}
768
763
764
+ object.RemoveMember (name);
769
765
object.AddMember (
770
766
rapidjson::StringRef (name), std::move (jsonValue), allocator);
771
767
}
@@ -778,8 +774,8 @@ operator<<(std::ostream& os, LogParameter<T> const& param)
778
774
if (!beast::Journal::m_jsonLogsEnabled)
779
775
return os;
780
776
detail::setJsonValue (
781
- beast::Journal::currentJsonLogContext_.messageParams ,
782
- beast::Journal::currentJsonLogContext_.allocator ,
777
+ beast::Journal::currentJsonLogContext_.messageParams () ,
778
+ beast::Journal::currentJsonLogContext_.allocator () ,
783
779
param.name_ ,
784
780
param.value_ ,
785
781
&os);
@@ -793,8 +789,8 @@ operator<<(std::ostream& os, LogField<T> const& param)
793
789
if (!beast::Journal::m_jsonLogsEnabled)
794
790
return os;
795
791
detail::setJsonValue (
796
- beast::Journal::currentJsonLogContext_.messageParams ,
797
- beast::Journal::currentJsonLogContext_.allocator ,
792
+ beast::Journal::currentJsonLogContext_.messageParams () ,
793
+ beast::Journal::currentJsonLogContext_.allocator () ,
798
794
param.name_ ,
799
795
param.value_ ,
800
796
nullptr );
0 commit comments