olp-cpp-sdk  1.22.0
Log.h
Go to the documentation of this file.
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 <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>
32 
33 #include <boost/optional.hpp>
34 
43 #ifdef LOGGING_DISABLE_LOCATION
44 
45 #define OLP_SDK_LOG_FUNCTION_SIGNATURE ""
46 #define OLP_SDK_LOG_FILE ""
47 #define OLP_SDK_LOG_LINE 0
48 #define OLP_SDK_LOG_FUNCTION ""
49 
50 #else // LOGGING_DISABLE_LOCATION
51 
52 #if __GNUC__ >= 3 || defined(__clang__)
53 #define OLP_SDK_LOG_FUNCTION_SIGNATURE __PRETTY_FUNCTION__
54 #elif defined(_MSC_VER)
55 #define OLP_SDK_LOG_FUNCTION_SIGNATURE __FUNCSIG__
56 #else
57 #define OLP_SDK_LOG_FUNCTION_SIGNATURE __FUNCTION__
58 #endif
59 
60 #define OLP_SDK_LOG_FILE __FILE__
61 #define OLP_SDK_LOG_LINE __LINE__
62 #define OLP_SDK_LOG_FUNCTION __FUNCTION__
63 
64 #endif // LOGGING_DISABLE_LOCATION
65 
76 #define OLP_SDK_DO_LOG(level, tag, message) \
77  do { \
78  std::ostringstream __strm; \
79  __strm << message; \
80  ::olp::logging::Log::logMessage( \
81  level, tag, __strm.str(), OLP_SDK_LOG_FILE, OLP_SDK_LOG_LINE, \
82  OLP_SDK_LOG_FUNCTION, OLP_SDK_LOG_FUNCTION_SIGNATURE); \
83  } \
84  OLP_SDK_CORE_LOOP_ONCE()
85 
96 #define OLP_SDK_LOG_CRITICAL(level, tag, message) \
97  OLP_SDK_DO_LOG(level, tag, message)
98 
108 #define OLP_SDK_LOG_CRITICAL_INFO(tag, message) \
109  OLP_SDK_LOG_CRITICAL(::olp::logging::Level::Info, tag, message)
110 
120 #define OLP_SDK_LOG_CRITICAL_WARNING(tag, message) \
121  OLP_SDK_LOG_CRITICAL(::olp::logging::Level::Warning, tag, message)
122 
132 #define OLP_SDK_LOG_CRITICAL_ERROR(tag, message) \
133  OLP_SDK_LOG_CRITICAL(::olp::logging::Level::Error, tag, message)
134 
144 #define OLP_SDK_LOG_FATAL(tag, message) \
145  OLP_SDK_LOG_CRITICAL(::olp::logging::Level::Fatal, tag, message)
146 
154 #define OLP_SDK_LOG_ABORT(tag, message) \
155  do { \
156  OLP_SDK_LOG_FATAL(tag, message); \
157  std::abort(); \
158  } \
159  OLP_SDK_CORE_LOOP_ONCE()
160 
170 #define OLP_SDK_DO_LOG_F(level, tag, ...) \
171  do { \
172  std::string __message = ::olp::logging::format(__VA_ARGS__); \
173  ::olp::logging::Log::logMessage(level, tag, __message, OLP_SDK_LOG_FILE, \
174  OLP_SDK_LOG_LINE, OLP_SDK_LOG_FUNCTION, \
175  OLP_SDK_LOG_FUNCTION_SIGNATURE); \
176  } \
177  OLP_SDK_CORE_LOOP_ONCE()
178 
188 #define OLP_SDK_LOG_CRITICAL_F(level, tag, ...) \
189  OLP_SDK_DO_LOG_F(level, tag, __VA_ARGS__)
190 
199 #define OLP_SDK_LOG_CRITICAL_INFO_F(tag, ...) \
200  OLP_SDK_LOG_CRITICAL_F(::olp::logging::Level::Info, tag, __VA_ARGS__)
201 
210 #define OLP_SDK_LOG_CRITICAL_WARNING_F(tag, ...) \
211  OLP_SDK_LOG_CRITICAL_F(::olp::logging::Level::Warning, tag, __VA_ARGS__)
212 
221 #define OLP_SDK_LOG_CRITICAL_ERROR_F(tag, ...) \
222  OLP_SDK_LOG_CRITICAL_F(::olp::logging::Level::Error, tag, __VA_ARGS__)
223 
233 #define OLP_SDK_LOG_FATAL_F(tag, ...) \
234  OLP_SDK_LOG_CRITICAL_F(::olp::logging::Level::Fatal, tag, __VA_ARGS__)
235 
242 #define OLP_SDK_LOG_ABORT_F(tag, ...) \
243  do { \
244  OLP_SDK_LOG_FATAL_F(tag, __VA_ARGS__); \
245  std::abort(); \
246  } \
247  OLP_SDK_CORE_LOOP_ONCE()
248 
249 #ifdef OLP_SDK_LOGGING_DISABLED
250 #define OLP_SDK_LOG(level, tag, message) \
251  do { \
252  } \
253  OLP_SDK_CORE_LOOP_ONCE()
254 #else
255 
263 #define OLP_SDK_LOG(level, tag, message) \
264  do { \
265  if (::olp::logging::Log::isEnabled(level, tag)) { \
266  OLP_SDK_DO_LOG(level, tag, message); \
267  } \
268  } \
269  OLP_SDK_CORE_LOOP_ONCE()
270 
271 #endif // OLP_SDK_LOGGING_DISABLED
272 
273 #ifdef LOGGING_DISABLE_DEBUG_LEVEL
274 #define OLP_SDK_LOG_TRACE(tag, message) \
275  do { \
276  ::olp::logging::NullLogStream __strm; \
277  __strm << message; \
278  OLP_SDK_CORE_UNUSED(tag, __strm); \
279  } \
280  OLP_SDK_CORE_LOOP_ONCE()
281 
282 #define OLP_SDK_LOG_DEBUG(tag, message) \
283  do { \
284  ::olp::logging::NullLogStream __strm; \
285  __strm << message; \
286  OLP_SDK_CORE_UNUSED(tag, __strm); \
287  } \
288  OLP_SDK_CORE_LOOP_ONCE()
289 
290 #else
297 #define OLP_SDK_LOG_TRACE(tag, message) \
298  OLP_SDK_LOG(::olp::logging::Level::Trace, tag, message)
299 
306 #define OLP_SDK_LOG_DEBUG(tag, message) \
307  OLP_SDK_LOG(::olp::logging::Level::Debug, tag, message)
308 
309 #endif // LOGGING_DISABLE_DEBUG_LEVEL
310 
317 #define OLP_SDK_LOG_INFO(tag, message) \
318  OLP_SDK_LOG(::olp::logging::Level::Info, tag, message)
319 
326 #define OLP_SDK_LOG_WARNING(tag, message) \
327  OLP_SDK_LOG(::olp::logging::Level::Warning, tag, message)
328 
335 #define OLP_SDK_LOG_ERROR(tag, message) \
336  OLP_SDK_LOG(::olp::logging::Level::Error, tag, message)
337 
338 #ifdef OLP_SDK_LOGGING_DISABLED
339 #define OLP_SDK_LOG_F(level, tag, ...) \
340  do { \
341  } \
342  OLP_SDK_CORE_LOOP_ONCE()
343 #else
350 #define OLP_SDK_LOG_F(level, tag, ...) \
351  do { \
352  if (::olp::logging::Log::isEnabled(level, tag)) { \
353  OLP_SDK_DO_LOG_F(level, tag, __VA_ARGS__); \
354  } \
355  } \
356  OLP_SDK_CORE_LOOP_ONCE()
357 
358 #endif // OLP_SDK_LOGGING_DISABLED
359 
360 #ifdef LOGGING_DISABLE_DEBUG_LEVEL
361 #define OLP_SDK_LOG_TRACE_F(tag, ...) OLP_SDK_CORE_UNUSED(tag, __VA_ARGS__)
362 #define OLP_SDK_LOG_DEBUG_F(tag, ...) OLP_SDK_CORE_UNUSED(tag, __VA_ARGS__)
363 #else
369 #define OLP_SDK_LOG_TRACE_F(tag, ...) \
370  OLP_SDK_LOG_F(::olp::logging::Level::Trace, tag, __VA_ARGS__)
371 
377 #define OLP_SDK_LOG_DEBUG_F(tag, ...) \
378  OLP_SDK_LOG_F(::olp::logging::Level::Debug, tag, __VA_ARGS__)
379 
380 #endif // LOGGING_DISABLE_DEBUG_LEVEL
381 
387 #define OLP_SDK_LOG_INFO_F(tag, ...) \
388  OLP_SDK_LOG_F(::olp::logging::Level::Info, tag, __VA_ARGS__)
389 
395 #define OLP_SDK_LOG_WARNING_F(tag, ...) \
396  OLP_SDK_LOG_F(::olp::logging::Level::Warning, tag, __VA_ARGS__)
397 
403 #define OLP_SDK_LOG_ERROR_F(tag, ...) \
404  OLP_SDK_LOG_F(::olp::logging::Level::Error, tag, __VA_ARGS__)
405 
409 namespace olp {
410 namespace logging {
411 class Configuration;
412 class FilterGroup;
413 
418  public:
419  template <typename T>
420 
425  return *this;
426  }
427 };
428 
432 class CORE_API Log {
433  public:
442  static bool configure(Configuration configuration);
443 
452 
461  static void setLevel(Level level);
462 
468  static Level getLevel();
469 
479  static void setLevel(Level level, const std::string& tag);
480 
490  static void setLevel(const std::string& level, const std::string& tag);
491 
501  static boost::optional<Level> getLevel(const std::string& tag);
502 
509  static void clearLevel(const std::string& tag);
510 
515  static void clearLevels();
516 
526  static void applyFilterGroup(const FilterGroup& filters);
527 
535  static bool isEnabled(Level level);
536 
545  static bool isEnabled(Level level, const std::string& tag);
546 
562  static void logMessage(Level level, const std::string& tag,
563  const std::string& message, const char* file,
564  unsigned int line, const char* function,
565  const char* fullFunction);
566 };
567 } // namespace logging
568 } // 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:40
A primary interface for log messages.
Definition: Log.h:432
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 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 boost::optional< Level > getLevel(const std::string &tag)
Gets the log level for a tag.
static Configuration getConfiguration()
Gets a copy of the current configuration.
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:417
NullLogStream & operator<<(const T &)
The stream operator to print or serialize the given log stream.
Definition: Log.h:424
Rules all the other namespaces.
Definition: AppleSignInProperties.h:24