Line data Source code
1 : // Copyright 2024 Accenture.
2 :
3 : #pragma once
4 :
5 : #include "logger/IEntryFormatter.h"
6 : #include "logger/ILoggerTime.h"
7 :
8 : #include <util/format/PrintfFormatter.h>
9 : #include <util/format/StringWriter.h>
10 : #include <util/format/Vt100AttributedStringFormatter.h>
11 : #include <util/logger/ComponentInfo.h>
12 : #include <util/logger/LevelInfo.h>
13 :
14 : namespace logger
15 : {
16 : template<class E = uint32_t, class Timestamp = uint32_t>
17 : class DefaultEntryFormatter : public IEntryFormatter<E, Timestamp>
18 : {
19 : public:
20 : explicit DefaultEntryFormatter(ILoggerTime<Timestamp>& loggerTime);
21 :
22 : void formatEntry(
23 : ::util::stream::IOutputStream& outputStream,
24 : E entryIndex,
25 : Timestamp timestamp,
26 : ::util::logger::ComponentInfo const& componentInfo,
27 : ::util::logger::LevelInfo const& levelInfo,
28 : char const* str,
29 : ::util::format::IPrintfArgumentReader& argReader) const override;
30 :
31 : private:
32 : ILoggerTime<Timestamp>& _loggerTime;
33 : };
34 :
35 : template<class E, class Timestamp>
36 1 : DefaultEntryFormatter<E, Timestamp>::DefaultEntryFormatter(ILoggerTime<Timestamp>& loggerTime)
37 1 : : IEntryFormatter<E, Timestamp>(), _loggerTime(loggerTime)
38 1 : {}
39 :
40 : template<class E, class Timestamp>
41 1 : void DefaultEntryFormatter<E, Timestamp>::formatEntry(
42 : ::util::stream::IOutputStream& outputStream,
43 : E const /* entryIndex */,
44 : Timestamp const timestamp,
45 : ::util::logger::ComponentInfo const& componentInfo,
46 : ::util::logger::LevelInfo const& levelInfo,
47 : char const* const str,
48 : ::util::format::IPrintfArgumentReader& argReader) const
49 : {
50 1 : ::util::format::StringWriter writer(outputStream);
51 1 : ::util::format::Vt100AttributedStringFormatter vt100Formatter;
52 1 : _loggerTime.formatTimestamp(outputStream, timestamp);
53 :
54 1 : auto const compFormat = vt100Formatter.write(componentInfo.getName());
55 1 : auto const levelFormat = vt100Formatter.write(levelInfo.getName());
56 1 : (void)writer.write(": ")
57 1 : .apply(compFormat)
58 1 : .write(": ")
59 1 : .apply(levelFormat)
60 1 : .write(": ")
61 1 : .vprintf(str, argReader);
62 1 : }
63 :
64 : } // namespace logger
|