iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
console_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_CONSOLE_LOGGER_HPP
16#define IOX2_BB_REPORTING_LOG_BUILDING_BLOCKS_CONSOLE_LOGGER_HPP
17
21
22#include <cstdint>
23#include <cstdio>
24#include <mutex>
25
26namespace iox2 {
27namespace legacy {
28namespace log {
31 public:
38
42
44
47
50
53
55
56 virtual void createLogMessageHeader(const bb::detail::SourceLocation location, const LogLevel logLevel) noexcept;
57
59
61
63
64 // AXIVION Next Construct AutosarC++19_03-A3.9.1 : Not used as an integer but a low-level C-style string
66
67 void logChar(const char value) noexcept;
68
69 void logBool(const bool value) noexcept;
70
71 template <typename T, typename std::enable_if_t<std::is_arithmetic<T>::value, bool> = 0>
72 void logDec(const T value) noexcept;
73
75 typename std::enable_if_t<(std::is_integral<T>::value && std::is_unsigned<T>::value)
76 || std::is_floating_point<T>::value || std::is_pointer<T>::value,
77 bool> = 0>
78 void logHex(const T value) noexcept;
79
80 template <typename T, typename std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value, bool> = 0>
81 void logOct(const T value) noexcept;
82
83 template <typename T, typename std::enable_if_t<std::is_integral<T>::value && std::is_unsigned<T>::value, bool> = 0>
84 void logBin(const T value) noexcept;
85
87
88 private:
89 // AXIVION Next Construct AutosarC++19_03-A3.9.1 : Not used as an integer but as actual character
90 // AXIVION Next Construct AutosarC++19_03-A18.1.1 : C-style array is used to acquire size of the array safely. Safe
91 // access is guaranteed since the char array is not accessed but only the size is obtained
93 // NOLINTNEXTLINE(hicpp-avoid-c-arrays, cppcoreguidelines-avoid-c-arrays)
94 static constexpr uint32_t bufferSize(const char (&)[N]) noexcept;
95
97 static constexpr void unused(T&&) noexcept;
98
99 // AXIVION Next Construct AutosarC++19_03-A3.9.1 : Not used as an integer but format string literal
101 static void logArithmetic(const T value, const char* format) noexcept;
102
103 struct ThreadLocalData final {
104 ThreadLocalData() noexcept = default;
105 ~ThreadLocalData() = default;
106
107 ThreadLocalData(const ThreadLocalData&) = delete;
108 ThreadLocalData(ThreadLocalData&&) = delete;
109
110 ThreadLocalData& operator=(const ThreadLocalData&) = delete;
111 ThreadLocalData& operator=(ThreadLocalData&&) = delete;
112
114 static constexpr uint32_t BUFFER_SIZE { 1024 };
115 static constexpr uint32_t NULL_TERMINATED_BUFFER_SIZE { BUFFER_SIZE + 1 };
116
117 // AXIVION Next Construct AutosarC++19_03-A3.9.1 : Not used as an integer but as actual character
118 // AXIVION Next Construct AutosarC++19_03-A18.1.1 : This is a low-level component with minimal dependencies.
119 // Safe access is guaranteed since the char array is wrapped inside the class
120 // NOLINTNEXTLINE(hicpp-avoid-c-arrays, cppcoreguidelines-avoid-c-arrays)
121 char buffer[NULL_TERMINATED_BUFFER_SIZE];
122 uint32_t bufferWriteIndex; // initialized in corresponding cpp file
123 LogLevel logLevel { LogLevel::Trace }; // required for a temporary workaround to print only LogLevel::Error and
124 // LogLevel::Fatal with the ConsoleLogger
126 };
127
128 static ThreadLocalData& getThreadLocalData() noexcept;
129};
130
131} // namespace log
132} // namespace legacy
133} // namespace iox2
134
135#include "iox2/legacy/detail/log/building_blocks/console_logger.inl"
136
137#endif // IOX2_BB_REPORTING_LOG_BUILDING_BLOCKS_CONSOLE_LOGGER_HPP
A minimal logger implementation which outputs the log messages to the console.
virtual void initLogger(const LogLevel) noexcept
static void setLogLevel(const LogLevel logLevel) noexcept
Sets a new log level.
void logRaw(const void *const data, const uint64_t size) noexcept
virtual void flush() noexcept
LogBuffer getLogBuffer() const noexcept
void logBool(const bool value) noexcept
void logString(const char *message) noexcept
virtual void createLogMessageHeader(const bb::detail::SourceLocation location, const LogLevel logLevel) noexcept
static LogLevel getLogLevel() noexcept
Obtain the current log level.
void logHex(const T value) noexcept
void logChar(const char value) noexcept
void logBin(const T value) noexcept
void logOct(const T value) noexcept
ConsoleLogger & operator=(const ConsoleLogger &)=delete
void logDec(const T value) noexcept
LogLevel
This enum defines the log levels used for logging.
Definition logger.hpp:30
constexpr bool always_false_v
Helper value to bind a static_assert to a type.
constexpr uint64_t size(const UninitializedArray< T, N, Buffer > &) noexcept
Returns N.
void log(LogLevel log_level, const char *origin, const char *message)
Adds a log message to the logger.
Provides access to the log buffer if direct access is required.
Definition logformat.hpp:40