olp-cpp-sdk 1.24.0
Loading...
Searching...
No Matches
Log.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2019-2025 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 <cinttypes>
23#include <cstdlib>
24#include <sstream>
25#include <string>
26
27#include <olp/core/logging/Format.h>
28#include <olp/core/logging/Level.h>
29
30#include <olp/core/CoreApi.h>
31#include <olp/core/porting/optional.h>
33
42#ifdef LOGGING_DISABLE_LOCATION
43
44#define OLP_SDK_LOG_FUNCTION_SIGNATURE ""
45#define OLP_SDK_LOG_FILE ""
46#define OLP_SDK_LOG_LINE 0
47#define OLP_SDK_LOG_FUNCTION ""
48
49#else // LOGGING_DISABLE_LOCATION
50
51#if __GNUC__ >= 3 || defined(__clang__)
52#define OLP_SDK_LOG_FUNCTION_SIGNATURE __PRETTY_FUNCTION__
53#elif defined(_MSC_VER)
54#define OLP_SDK_LOG_FUNCTION_SIGNATURE __FUNCSIG__
55#else
56#define OLP_SDK_LOG_FUNCTION_SIGNATURE __FUNCTION__
57#endif
58
59#define OLP_SDK_LOG_FILE __FILE__
60#define OLP_SDK_LOG_LINE __LINE__
61#define OLP_SDK_LOG_FUNCTION __FUNCTION__
62
63#endif // LOGGING_DISABLE_LOCATION
64
75#define OLP_SDK_DO_LOG(level, tag, message) \
76 do { \
77 std::ostringstream __strm; \
78 __strm << message; \
79 ::olp::logging::Log::logMessage( \
80 level, tag, __strm.str(), OLP_SDK_LOG_FILE, OLP_SDK_LOG_LINE, \
81 OLP_SDK_LOG_FUNCTION, OLP_SDK_LOG_FUNCTION_SIGNATURE); \
82 } \
83 OLP_SDK_CORE_LOOP_ONCE()
84
95#define OLP_SDK_LOG_CRITICAL(level, tag, message) \
96 OLP_SDK_DO_LOG(level, tag, message)
97
107#define OLP_SDK_LOG_CRITICAL_INFO(tag, message) \
108 OLP_SDK_LOG_CRITICAL(::olp::logging::Level::Info, tag, message)
109
119#define OLP_SDK_LOG_CRITICAL_WARNING(tag, message) \
120 OLP_SDK_LOG_CRITICAL(::olp::logging::Level::Warning, tag, message)
121
131#define OLP_SDK_LOG_CRITICAL_ERROR(tag, message) \
132 OLP_SDK_LOG_CRITICAL(::olp::logging::Level::Error, tag, message)
133
143#define OLP_SDK_LOG_FATAL(tag, message) \
144 OLP_SDK_LOG_CRITICAL(::olp::logging::Level::Fatal, tag, message)
145
153#define OLP_SDK_LOG_ABORT(tag, message) \
154 do { \
155 OLP_SDK_LOG_FATAL(tag, message); \
156 std::abort(); \
157 } \
158 OLP_SDK_CORE_LOOP_ONCE()
159
169#define OLP_SDK_DO_LOG_F(level, tag, ...) \
170 do { \
171 std::string __message = ::olp::logging::format(__VA_ARGS__); \
172 ::olp::logging::Log::logMessage(level, tag, __message, OLP_SDK_LOG_FILE, \
173 OLP_SDK_LOG_LINE, OLP_SDK_LOG_FUNCTION, \
174 OLP_SDK_LOG_FUNCTION_SIGNATURE); \
175 } \
176 OLP_SDK_CORE_LOOP_ONCE()
177
187#define OLP_SDK_LOG_CRITICAL_F(level, tag, ...) \
188 OLP_SDK_DO_LOG_F(level, tag, __VA_ARGS__)
189
198#define OLP_SDK_LOG_CRITICAL_INFO_F(tag, ...) \
199 OLP_SDK_LOG_CRITICAL_F(::olp::logging::Level::Info, tag, __VA_ARGS__)
200
209#define OLP_SDK_LOG_CRITICAL_WARNING_F(tag, ...) \
210 OLP_SDK_LOG_CRITICAL_F(::olp::logging::Level::Warning, tag, __VA_ARGS__)
211
220#define OLP_SDK_LOG_CRITICAL_ERROR_F(tag, ...) \
221 OLP_SDK_LOG_CRITICAL_F(::olp::logging::Level::Error, tag, __VA_ARGS__)
222
232#define OLP_SDK_LOG_FATAL_F(tag, ...) \
233 OLP_SDK_LOG_CRITICAL_F(::olp::logging::Level::Fatal, tag, __VA_ARGS__)
234
241#define OLP_SDK_LOG_ABORT_F(tag, ...) \
242 do { \
243 OLP_SDK_LOG_FATAL_F(tag, __VA_ARGS__); \
244 std::abort(); \
245 } \
246 OLP_SDK_CORE_LOOP_ONCE()
247
248#ifdef OLP_SDK_LOGGING_DISABLED
249#define OLP_SDK_LOG(level, tag, message) \
250 do { \
251 } \
252 OLP_SDK_CORE_LOOP_ONCE()
253#else
254
262#define OLP_SDK_LOG(level, tag, message) \
263 do { \
264 if (::olp::logging::Log::isEnabled(level, tag)) { \
265 OLP_SDK_DO_LOG(level, tag, message); \
266 } \
267 } \
268 OLP_SDK_CORE_LOOP_ONCE()
269
270#endif // OLP_SDK_LOGGING_DISABLED
271
272#ifdef LOGGING_DISABLE_DEBUG_LEVEL
273#define OLP_SDK_LOG_TRACE(tag, message) \
274 do { \
275 ::olp::logging::NullLogStream __strm; \
276 __strm << message; \
277 OLP_SDK_CORE_UNUSED(tag, __strm); \
278 } \
279 OLP_SDK_CORE_LOOP_ONCE()
280
281#define OLP_SDK_LOG_DEBUG(tag, message) \
282 do { \
283 ::olp::logging::NullLogStream __strm; \
284 __strm << message; \
285 OLP_SDK_CORE_UNUSED(tag, __strm); \
286 } \
287 OLP_SDK_CORE_LOOP_ONCE()
288
289#else
296#define OLP_SDK_LOG_TRACE(tag, message) \
297 OLP_SDK_LOG(::olp::logging::Level::Trace, tag, message)
298
305#define OLP_SDK_LOG_DEBUG(tag, message) \
306 OLP_SDK_LOG(::olp::logging::Level::Debug, tag, message)
307
308#endif // LOGGING_DISABLE_DEBUG_LEVEL
309
316#define OLP_SDK_LOG_INFO(tag, message) \
317 OLP_SDK_LOG(::olp::logging::Level::Info, tag, message)
318
325#define OLP_SDK_LOG_WARNING(tag, message) \
326 OLP_SDK_LOG(::olp::logging::Level::Warning, tag, message)
327
334#define OLP_SDK_LOG_ERROR(tag, message) \
335 OLP_SDK_LOG(::olp::logging::Level::Error, tag, message)
336
337#ifdef OLP_SDK_LOGGING_DISABLED
338#define OLP_SDK_LOG_F(level, tag, ...) \
339 do { \
340 } \
341 OLP_SDK_CORE_LOOP_ONCE()
342#else
349#define OLP_SDK_LOG_F(level, tag, ...) \
350 do { \
351 if (::olp::logging::Log::isEnabled(level, tag)) { \
352 OLP_SDK_DO_LOG_F(level, tag, __VA_ARGS__); \
353 } \
354 } \
355 OLP_SDK_CORE_LOOP_ONCE()
356
357#endif // OLP_SDK_LOGGING_DISABLED
358
359#ifdef LOGGING_DISABLE_DEBUG_LEVEL
360#define OLP_SDK_LOG_TRACE_F(tag, ...) OLP_SDK_CORE_UNUSED(tag, __VA_ARGS__)
361#define OLP_SDK_LOG_DEBUG_F(tag, ...) OLP_SDK_CORE_UNUSED(tag, __VA_ARGS__)
362#else
368#define OLP_SDK_LOG_TRACE_F(tag, ...) \
369 OLP_SDK_LOG_F(::olp::logging::Level::Trace, tag, __VA_ARGS__)
370
376#define OLP_SDK_LOG_DEBUG_F(tag, ...) \
377 OLP_SDK_LOG_F(::olp::logging::Level::Debug, tag, __VA_ARGS__)
378
379#endif // LOGGING_DISABLE_DEBUG_LEVEL
380
386#define OLP_SDK_LOG_INFO_F(tag, ...) \
387 OLP_SDK_LOG_F(::olp::logging::Level::Info, tag, __VA_ARGS__)
388
394#define OLP_SDK_LOG_WARNING_F(tag, ...) \
395 OLP_SDK_LOG_F(::olp::logging::Level::Warning, tag, __VA_ARGS__)
396
402#define OLP_SDK_LOG_ERROR_F(tag, ...) \
403 OLP_SDK_LOG_F(::olp::logging::Level::Error, tag, __VA_ARGS__)
404
408namespace olp {
409namespace logging {
410class Configuration;
411class FilterGroup;
412
417 public:
418 template <typename T>
419
424 return *this;
425 }
426};
427
431class CORE_API Log {
432 public:
441 static bool configure(Configuration configuration);
442
451
460 static void setLevel(Level level);
461
467 static Level getLevel();
468
478 static void setLevel(Level level, const std::string& tag);
479
489 static void setLevel(const std::string& level, const std::string& tag);
490
500 static porting::optional<Level> getLevel(const std::string& tag);
501
508 static void clearLevel(const std::string& tag);
509
514 static void clearLevels();
515
525 static void applyFilterGroup(const FilterGroup& filters);
526
534 static bool isEnabled(Level level);
535
544 static bool isEnabled(Level level, const std::string& tag);
545
561 static void logMessage(Level level, const std::string& tag,
562 const std::string& message, const char* file,
563 unsigned int line, const char* function,
564 const char* fullFunction);
565
574 static void addCensor(const std::string& message);
575
584 static void removeCensor(const std::string& message);
585};
586} // namespace logging
587} // namespace olp
Contains utilities used to work around compiler warnings.
Configures appenders and loggers available in the logging system.
Definition Configuration.h:36
Groups together log levels for different tags.
Definition FilterGroup.h:38
A primary interface for log messages.
Definition Log.h:431
static bool isEnabled(Level level)
Checks whether a level is enabled by default.
static void setLevel(Level level, const std::string &tag)
Sets the log level for a tag.
static bool isEnabled(Level level, const std::string &tag)
Checks whether a log tag is enabled for a level.
static void removeCensor(const std::string &message)
Removes a line from censoring out from the log.
static bool configure(Configuration configuration)
Configures the log system.
static void setLevel(Level level)
Sets the default log level.
static Level getLevel()
Gets the default log level.
static void addCensor(const std::string &message)
Adds a line to be censored out from the log.
static Configuration getConfiguration()
Gets a copy of the current configuration.
static porting::optional< Level > getLevel(const std::string &tag)
Gets the log level for a tag.
static void clearLevel(const std::string &tag)
Clears the log level for a tag and sets it to the default value.
static void clearLevels()
Clears the log levels for all tags and sets them to the default value.
static void applyFilterGroup(const FilterGroup &filters)
Applies a filter group.
static void logMessage(Level level, const std::string &tag, const std::string &message, const char *file, unsigned int line, const char *function, const char *fullFunction)
Logs a message to the registered appenders.
static void setLevel(const std::string &level, const std::string &tag)
Sets the log level for a tag.
Used for disabled logs at compile time.
Definition Log.h:416
NullLogStream & operator<<(const T &)
The stream operator to print or serialize the given log stream.
Definition Log.h:423
Rules all the other namespaces.
Definition AppleSignInProperties.h:24