Line data Source code
1 : /********************************************************************************
2 : * Copyright (c) 2024 Accenture
3 : *
4 : * This program and the accompanying materials are made available under the
5 : * terms of the Apache License Version 2.0 which is available at
6 : * https://www.apache.org/licenses/LICENSE-2.0
7 : *
8 : * SPDX-License-Identifier: Apache-2.0
9 : ********************************************************************************/
10 :
11 : #pragma once
12 :
13 : #include "logger/IEntryFormatter.h"
14 : #include "logger/ILoggerTime.h"
15 :
16 : #include <util/format/PrintfFormatter.h>
17 : #include <util/format/StringWriter.h>
18 : #include <util/format/Vt100AttributedStringFormatter.h>
19 : #include <util/logger/ComponentInfo.h>
20 : #include <util/logger/LevelInfo.h>
21 :
22 : namespace logger
23 : {
24 : template<class E = uint32_t, class Timestamp = uint32_t>
25 : class DefaultEntryFormatter : public IEntryFormatter<E, Timestamp>
26 : {
27 : public:
28 : explicit DefaultEntryFormatter(ILoggerTime<Timestamp>& loggerTime);
29 :
30 : void formatEntry(
31 : ::util::stream::IOutputStream& outputStream,
32 : E entryIndex,
33 : Timestamp timestamp,
34 : ::util::logger::ComponentInfo const& componentInfo,
35 : ::util::logger::LevelInfo const& levelInfo,
36 : char const* str,
37 : ::util::format::IPrintfArgumentReader& argReader) const override;
38 :
39 : private:
40 : ILoggerTime<Timestamp>& _loggerTime;
41 : };
42 :
43 : template<class E, class Timestamp>
44 1 : DefaultEntryFormatter<E, Timestamp>::DefaultEntryFormatter(ILoggerTime<Timestamp>& loggerTime)
45 1 : : IEntryFormatter<E, Timestamp>(), _loggerTime(loggerTime)
46 1 : {}
47 :
48 : template<class E, class Timestamp>
49 1 : void DefaultEntryFormatter<E, Timestamp>::formatEntry(
50 : ::util::stream::IOutputStream& outputStream,
51 : E const /* entryIndex */,
52 : Timestamp const timestamp,
53 : ::util::logger::ComponentInfo const& componentInfo,
54 : ::util::logger::LevelInfo const& levelInfo,
55 : char const* const str,
56 : ::util::format::IPrintfArgumentReader& argReader) const
57 : {
58 1 : ::util::format::StringWriter writer(outputStream);
59 1 : ::util::format::Vt100AttributedStringFormatter vt100Formatter;
60 1 : _loggerTime.formatTimestamp(outputStream, timestamp);
61 :
62 1 : auto const compFormat = vt100Formatter.write(componentInfo.getName());
63 1 : auto const levelFormat = vt100Formatter.write(levelInfo.getName());
64 1 : (void)writer.write(": ")
65 1 : .apply(compFormat)
66 1 : .write(": ")
67 1 : .apply(levelFormat)
68 1 : .write(": ")
69 1 : .vprintf(str, argReader);
70 1 : }
71 :
72 : } // namespace logger
|