iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
logger.hpp
Go to the documentation of this file.
1// Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
3// Copyright (c) 2025 Contributors to the Eclipse Foundation
4//
5// See the NOTICE file(s) distributed with this work for additional
6// information regarding copyright ownership.
7//
8// This program and the accompanying materials are made available under the
9// terms of the Apache Software License 2.0 which is available at
10// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
11// which is available at https://opensource.org/licenses/MIT.
12//
13// SPDX-License-Identifier: Apache-2.0 OR MIT
14
15#ifndef IOX2_BB_REPORTING_LOG_BUILDING_BLOCKS_LOGGER_HPP
16#define IOX2_BB_REPORTING_LOG_BUILDING_BLOCKS_LOGGER_HPP
17
19
20#include <cstdint>
21#include <cstring>
22#include <mutex>
23
24namespace iox2 {
25namespace legacy {
26namespace log {
27class LogStream;
28
30enum class LogLevel : uint8_t {
31 Off = 0,
32 Fatal,
33 Error,
34 Warn,
35 Info,
36 Debug,
37 Trace,
38};
39
43// AXIVION Next Construct AutosarC++19_03-A3.9.1 : This function return a string literal
44// which corresponds to a const char *
45constexpr const char* asStringLiteral(const LogLevel value) noexcept;
46
53template <uint32_t N>
54// NOLINTJUSTIFICATION required for C-style string comparison; safety guaranteed by strncmp
55// NOLINTNEXTLINE(hicpp-avoid-c-arrays, cppcoreguidelines-avoid-c-arrays)
56bool equalStrings(const char* lhs, const char (&rhs)[N]) noexcept;
57
64LogLevel logLevelFromEnvOr(const LogLevel logLevel) noexcept;
65
66namespace internal {
70template <typename BaseLogger>
71class Logger : public BaseLogger {
72 public:
73 friend class log::LogStream;
74
75 Logger() = default;
76
77 Logger(const Logger&) = delete;
78 Logger(Logger&&) = delete;
79
80 Logger& operator=(const Logger&) = delete;
81 Logger& operator=(Logger&&) = delete;
82
83 ~Logger() = default;
84
87 static Logger& get() noexcept;
88
97
103
104 private:
105 static Logger& activeLogger(Logger* newLogger = nullptr) noexcept;
106
107 void initLoggerInternal(const LogLevel logLevel) noexcept;
108
109 private:
110 concurrent::Atomic<bool> m_isActive { true };
111 concurrent::Atomic<bool> m_isFinalized { false };
112};
113
114} // namespace internal
115} // namespace log
116} // namespace legacy
117} // namespace iox2
118
119#include "iox2/legacy/detail/log/building_blocks/logger.inl"
120
121#endif // IOX2_BB_REPORTING_LOG_BUILDING_BLOCKS_LOGGER_HPP
A thin wrapper for a 'std::atomic' which ensures that all atomic operations are always lock-free in o...
Definition atomic.hpp:32
This class provides the public interface to the logger and is used with the 'IOX2_LOG' macro....
This class acts as common interface for the Logger. It provides the common functionality and inherits...
Definition logger.hpp:71
static void init(const LogLevel logLevel=logLevelFromEnvOr(LogLevel::Info)) noexcept
Initializes the logger.
Logger(const Logger &)=delete
static void setActiveLogger(Logger &newLogger) noexcept
Replaces the default logger with the specified one.
Logger & operator=(Logger &&)=delete
static Logger & get() noexcept
Access to the logger singleton instance.
Logger & operator=(const Logger &)=delete
bool equalStrings(const char *lhs, const char(&rhs)[N]) noexcept
Compares C-style strings with a char array, i.g. string literal for equality.
LogLevel
This enum defines the log levels used for logging.
Definition logger.hpp:30
LogLevel logLevelFromEnvOr(const LogLevel logLevel) noexcept
Tries to get the log level from the 'IOX2_LOG_LEVEL' env variable or uses the specified one if the en...
constexpr const char * asStringLiteral(const LogLevel value) noexcept
converts LogLevel into a string literal
constexpr bool always_false_v
Helper value to bind a static_assert to a type.
void log(LogLevel log_level, const char *origin, const char *message)
Adds a log message to the logger.