olp-cpp-sdk 1.24.0
Loading...
Searching...
No Matches
MessageFormatter.h
1/*
2 * Copyright (C) 2019-2024 HERE Europe B.V.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * SPDX-License-Identifier: Apache-2.0
17 * License-Filename: LICENSE
18 */
19
20#pragma once
21
22#include <array>
23#include <string>
24#include <utility>
25#include <vector>
26
27#include <olp/core/CoreApi.h>
28
29#include <olp/core/logging/Format.h>
30#include <olp/core/logging/Level.h>
31#include <olp/core/logging/LogMessage.h>
32
33namespace olp {
34namespace logging {
61class CORE_API MessageFormatter {
62 public:
69 using LevelNameMap = std::array<std::string, levelCount>;
70
74 enum class ElementType {
75 String,
76 Level,
77 Tag,
80 Message,
81 File,
83 Line,
85 Function,
87 FullFunction,
90 Time,
92 TimeMs,
94 ThreadId,
96 ContextValue,
97 };
98
102 struct CORE_API Element {
110 explicit Element(ElementType type_);
111
123 inline Element(ElementType type_, std::string format_, int limit_ = 0);
124
126 Element(const Element&) = default;
127
129 Element& operator=(const Element&) = default;
130
132 inline Element(Element&& other) noexcept;
133
135 inline Element& operator=(Element&& other) noexcept;
136
145 inline bool operator==(const Element& other) const;
146
155 inline bool operator!=(const Element& other) const;
156
161
168 std::string format;
169
178 int limit;
179 };
180
184 enum class Timezone {
185 Local,
186 Utc
187 };
188
195
202
209 inline MessageFormatter();
210
218 explicit inline MessageFormatter(
219 std::vector<Element> elements,
220 LevelNameMap levelNameMap = defaultLevelNameMap(),
221 Timezone timezone = Timezone::Local);
222
225
228
230 inline MessageFormatter(MessageFormatter&& other) noexcept;
231
233 inline MessageFormatter& operator=(MessageFormatter&& other) noexcept;
234
240 inline const std::vector<Element>& getElements() const;
241
247 inline MessageFormatter& setElements(std::vector<Element> elements);
248
254 inline const LevelNameMap& getLevelNameMap() const;
255
261 inline MessageFormatter& setLevelNameMap(LevelNameMap map);
262
268 inline Timezone getTimezone() const;
269
275 inline MessageFormatter& setTimezone(Timezone timezone);
276
284 std::string format(const LogMessage& message) const;
285
286 private:
287 std::vector<Element> m_elements;
288 LevelNameMap m_levelNameMap;
289 Timezone m_timezone;
290};
291
293 std::string format_, int limit_)
294 : type(type_), format(std::move(format_)), limit(limit_) {}
295
297 : type(other.type), format(std::move(other.format)), limit(other.limit) {}
298
300 Element&& other) noexcept {
301 type = other.type;
302 format = std::move(other.format);
303 limit = other.limit;
304 return *this;
305}
306
307inline bool MessageFormatter::Element::operator==(const Element& other) const {
308 return type == other.type && format == other.format && limit == other.limit;
309}
310
311inline bool MessageFormatter::Element::operator!=(const Element& other) const {
312 return !(*this == other);
313}
314
316 : m_levelNameMap(defaultLevelNameMap()), m_timezone(Timezone::Local) {}
317
318inline MessageFormatter::MessageFormatter(std::vector<Element> elements,
319 LevelNameMap levelNameMap,
320 Timezone timezone)
321 : m_elements(std::move(elements)),
322 m_levelNameMap(std::move(levelNameMap)),
323 m_timezone(timezone) {}
324
326 : m_elements(std::move(other.m_elements)),
327 m_levelNameMap(std::move(other.m_levelNameMap)),
328 m_timezone(other.m_timezone) {}
329
331 MessageFormatter&& other) noexcept {
332 m_elements = std::move(other.m_elements);
333 m_levelNameMap = std::move(other.m_levelNameMap);
334 m_timezone = other.m_timezone;
335 return *this;
336}
337
338inline const std::vector<MessageFormatter::Element>&
340 return m_elements;
341}
342
344 std::vector<Element> elements) {
345 m_elements = std::move(elements);
346 return *this;
347}
348
350 const {
351 return m_levelNameMap;
352}
353
355 m_levelNameMap = std::move(map);
356 return *this;
357}
358
360 return m_timezone;
361}
362
364 m_timezone = timezone;
365 return *this;
366}
367
368} // namespace logging
369} // namespace olp
Specifies how messages are formatted.
Definition MessageFormatter.h:61
ElementType
The type of the element to print out.
Definition MessageFormatter.h:74
MessageFormatter & setTimezone(Timezone timezone)
Sets the timezone for timestamps.
Definition MessageFormatter.h:363
MessageFormatter(const MessageFormatter &)=default
The default copy constructor.
std::array< std::string, levelCount > LevelNameMap
Maps the log level to its name.
Definition MessageFormatter.h:69
const LevelNameMap & getLevelNameMap() const
Gets the level name map.
Definition MessageFormatter.h:349
std::string format(const LogMessage &message) const
Formats a log message.
MessageFormatter & setLevelNameMap(LevelNameMap map)
Sets the level name map.
Definition MessageFormatter.h:354
static MessageFormatter createDefault()
Creates the default message formatter.
static const LevelNameMap & defaultLevelNameMap()
Gets the default level name map.
MessageFormatter & setElements(std::vector< Element > elements)
Sets the elements for the format.
Definition MessageFormatter.h:343
const std::vector< Element > & getElements() const
Gets the elements for the format.
Definition MessageFormatter.h:339
Timezone getTimezone() const
Gets the timezone for timestamps.
Definition MessageFormatter.h:359
MessageFormatter()
The default constructor.
Definition MessageFormatter.h:315
Timezone
The timezone used to print timestamps.
Definition MessageFormatter.h:184
@ Local
Prints time in the local time standard.
MessageFormatter & operator=(const MessageFormatter &)=default
The default copy assignment operator.
Rules all the other namespaces.
Definition AppleSignInProperties.h:24
Contains data used for a log message.
Definition LogMessage.h:31
An element to print out in the final formatted message.
Definition MessageFormatter.h:102
Element(const Element &)=default
The default copy constructor.
int limit
The number of characters to limit string types before passing to the formatter.
Definition MessageFormatter.h:178
Element(ElementType type_)
Creates an Element instance with the element type.
bool operator==(const Element &other) const
Checks whether the values of two Element instances are the same.
Definition MessageFormatter.h:307
bool operator!=(const Element &other) const
Checks whether the values of two Element instances are not the same.
Definition MessageFormatter.h:311
Element & operator=(const Element &)=default
The default copy operator.
std::string format
The format for printing out the element.
Definition MessageFormatter.h:168
ElementType type
The type of element to print.
Definition MessageFormatter.h:160