iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
error_reporting_impl.hpp
Go to the documentation of this file.
1// Copyright (c) 2023 by Apex.AI Inc. All rights reserved.
2// Copyright (c) 2025 Contributors to the Eclipse Foundation
3//
4// See the NOTICE file(s) distributed with this work for additional
5// information regarding copyright ownership.
6//
7// This program and the accompanying materials are made available under the
8// terms of the Apache Software License 2.0 which is available at
9// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
10// which is available at https://opensource.org/licenses/MIT.
11//
12// SPDX-License-Identifier: Apache-2.0 OR MIT
13
14#ifndef IOX2_BB_REPORTING_ERROR_REPORTING_CUSTOM_DEFAULT_ERROR_REPORTING_IMPL_HPP
15#define IOX2_BB_REPORTING_ERROR_REPORTING_CUSTOM_DEFAULT_ERROR_REPORTING_IMPL_HPP
16
22
25
26namespace iox2 {
27namespace legacy {
28namespace er {
29
30// The static reporting interface that must be defined to at least do nothing.
31// It should provide a noreturn specification for panic (but since it be assumed that the custom
32// code enforces this, it is enforced at the (non-custom) forwarding level.
33
34// Here, the implementation redirects to the polymorphic handler interface.
35// This adds an additional indirection but is required for switching handlers
36// during operation.
37// This is used for testing but must be done while no errors are reported concurrently,
38// otherwise error notifations could be lost.
39
40// The logging can be extended in the future.
41
42// Custom panic
43[[noreturn]] inline void panic() {
44 auto& h = ErrorHandler::get();
45 h.onPanic();
46 abort();
47}
48
49// Custom panic with location
50[[noreturn]] inline void panic(const bb::detail::SourceLocation& location) {
51 IOX2_ERROR_INTERNAL_LOG_PANIC(location, "[PANIC]");
52 panic();
53}
54
55// Custom panic with location and message
56// note that Message is generic as the logger technically accepts more general loggable constructs
57// beyond const char*
58template <class Message>
59// NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward) false positive, this is used as a universal reference
60[[noreturn]] inline void panic(const bb::detail::SourceLocation& location, Message&& msg) {
61 IOX2_ERROR_INTERNAL_LOG_PANIC(location, "[PANIC] " << msg);
62 panic();
63}
64
65namespace detail {
67 if (stringifiedCondition != nullptr && strnlen(stringifiedCondition, 1) != 0) {
68 stream << "Condition: \"" << stringifiedCondition << "\" ";
69 }
70 return stream;
71}
72} // namespace detail
73
74// Report any error, general version.
75template <class Kind, class Error>
76inline void
77report(const bb::detail::SourceLocation& location, Kind, const Error& error, const char* stringifiedCondition) {
78 auto code = toCode(error);
79 auto module = toModule(error);
80 auto moduleName = toModuleName(error);
82
84 [stringifiedCondition](auto& stream) -> auto& {
86 } << "["
87 << errorName << " (code = " << code.value << ")] in module [" << moduleName
88 << " (id = " << module.value << ")]");
89 auto& h = ErrorHandler::get();
90 h.onReportError(ErrorDescriptor(location, code, module));
91}
92
93// Report any error, specialization for specific types overrides the general version.
94// Any behaviour for specific error types (and kinds) has to be defined like this.
95//
96// Here the logging is subtly different and does not easily allow to factor out common parts.
97
98template <class Error>
99inline void report(const bb::detail::SourceLocation& location,
101 const Error& error,
102 const char* stringifiedCondition) {
103 auto code = toCode(error);
104 auto module = toModule(error);
105 auto moduleName = toModuleName(error);
107
109 [stringifiedCondition](auto& stream) -> auto& {
111 } << "["
112 << kind.name << "] [" << errorName << " (code = " << code.value << ")] in module ["
113 << moduleName << " (id = " << module.value << ")]");
114 auto& h = ErrorHandler::get();
115 h.onReportError(ErrorDescriptor(location, code, module));
116}
117
118namespace detail {
119template <class Kind, class Error>
120inline void
121// NOLINTNEXTLINE(readability-function-size) Not used directly but via a macro which hides the number of parameter away
123 auto code = toCode(error);
124 auto module = toModule(error);
126 [stringifiedCondition](auto& stream) -> auto& {
128 } << "["
129 << kind.name << "]");
130 auto& h = ErrorHandler::get();
131 h.onReportViolation(ErrorDescriptor(location, code, module));
132}
133
134template <class Kind, class Error, class Message>
135inline void
136// NOLINTNEXTLINE(readability-function-size) Not used directly but via a macro which hides the number of parameter away
138 Kind kind,
139 const Error& error,
140 const char* stringifiedCondition,
141 Message&& msg) {
142 auto code = toCode(error);
143 auto module = toModule(error);
145 [stringifiedCondition](auto& stream) -> auto& {
147 } << "["
148 << kind.name << "] " << std::forward<Message>(msg));
149 auto& h = ErrorHandler::get();
150 h.onReportViolation(ErrorDescriptor(location, code, module));
151}
152} // namespace detail
153
154template <class Error>
161
162template <class Error>
169
170template <class Error, class Message>
171// NOLINTNEXTLINE(readability-function-size) Not used directly but via a macro which hides the number of parameter away
172inline void report(const bb::detail::SourceLocation& location,
174 const Error& error,
175 const char* stringifiedCondition,
176 Message&& msg) {
177 detail::report(location, kind, error, stringifiedCondition, std::forward<Message>(msg));
178}
179
180template <class Error, class Message>
181// NOLINTNEXTLINE(readability-function-size) Not used directly but via a macro which hides the number of parameter away
182inline void report(const bb::detail::SourceLocation& location,
184 const Error& error,
185 const char* stringifiedCondition,
186 Message&& msg) {
187 detail::report(location, kind, error, stringifiedCondition, std::forward<Message>(msg));
188}
189
190} // namespace er
191} // namespace legacy
192} // namespace iox2
193
194#endif // IOX2_BB_REPORTING_ERROR_REPORTING_CUSTOM_DEFAULT_ERROR_REPORTING_IMPL_HPP
static Interface & get() noexcept
obtain the current singleton instance
This class provides the public interface to the logger and is used with the 'IOX2_LOG' macro....
#define IOX2_ERROR_INTERNAL_LOG(location, msg_stream)
Log the location of an error.
#define IOX2_ERROR_INTERNAL_LOG_FATAL(location, msg_stream)
Log the location of a fatal error.
#define IOX2_ERROR_INTERNAL_LOG_PANIC(location, msg_stream)
Log a panic invocation.
void report(const bb::detail::SourceLocation &location, Kind kind, const Error &error, const char *stringifiedCondition)
log::LogStream & logStringifiedCondition(log::LogStream &stream, const char *stringifiedCondition)
ErrorCode toCode(const Error &error)
Definition types.hpp:83
const char * toErrorName(const Error &error)
Definition types.hpp:103
void report(const bb::detail::SourceLocation &location, Kind, const Error &error, const char *stringifiedCondition)
const char * toModuleName(const Error &error)
Definition types.hpp:98
constexpr bool always_false_v
Helper value to bind a static_assert to a type.
helper struct to create an expected which is signalling an error more easily
Contains all required information about the error. Can be extended as needed without breaking the int...