A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
fatal-error.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2006 INRIA, 2010 NICTA
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18 * Quincy Tse <quincy.tse@nicta.com.au>
19 */
20#ifndef NS3_FATAL_ERROR_H
21#define NS3_FATAL_ERROR_H
22
23#include "fatal-impl.h"
24#include "log.h" // NS_LOG_APPEND...
25
26#include <cstdlib>
27#include <exception>
28#include <iostream>
29#include <string_view>
30
31/**
32 * \file
33 * \ingroup fatal
34 * \brief \c NS_FATAL_x macro definitions.
35 */
36
37/**
38 * \ingroup core
39 * \defgroup fatal Fatal Error Handlers
40 *
41 * \brief Functions to help clean up when a fatal error
42 * is encountered.
43 *
44 * The functions in this group are used to perform
45 * limited clean up, like flushing active streams, when
46 * fatal errors are encountered (through assertion fail,
47 * calls to NS_ABORT_* or calls to NS_FATAL_ERROR).
48 *
49 * Currently, other than flushing active ostreams, these
50 * functions does not interfere with outside memory. There
51 * is still a residual risk that invalid ostream
52 * pointers may be present, and may corrupt the memory
53 * on the attempt to execute the flush() function.
54 */
55
56namespace ns3
57{
58
59/**
60 * \ingroup fatal
61 *
62 * \brief Output string marking imminent invocation of std::terminate.
63 *
64 * This is useful to know when capturing output and you want to strip
65 * system and compiler-dependent messages generated by std::terminate.
66 */
67constexpr std::string_view NS_FATAL_MSG{"NS_FATAL, terminating"};
68
69} // namespace ns3
70
71/**
72 * \ingroup fatal
73 *
74 * \brief Fatal error reporting with no message, implementation.
75 *
76 * When this macro is hit at runtime the error details will
77 * printed to \c stderr, including the file name and line number.
78 * Optionally, if \c fatal is true, the macro
79 * will invoke \c std::terminate(). If \c fatal is false,
80 * the invoking function should return an error code to its caller,
81 * which is expected to call NS_FATAL_ERROR to cause termination.
82 *
83 * \param [in] fatal Call \c std::terminate() if true.
84 *
85 * This macro is enabled unconditionally in all builds,
86 * including debug and optimized builds.
87 */
88#define NS_FATAL_ERROR_IMPL_NO_MSG(fatal) \
89 do \
90 { \
91 NS_LOG_APPEND_TIME_PREFIX_IMPL; \
92 NS_LOG_APPEND_NODE_PREFIX_IMPL; \
93 std::cerr << "file=" << __FILE__ << ", line=" << __LINE__ << std::endl; \
94 ::ns3::FatalImpl::FlushStreams(); \
95 if (fatal) \
96 { \
97 std::cerr << ns3::NS_FATAL_MSG << std::endl; \
98 std::terminate(); \
99 } \
100 } while (false)
101
102/**
103 * \ingroup fatal
104 *
105 * \brief Fatal error reporting with a message, implementation.
106 *
107 * When this macro is hit at runtime the error details will
108 * printed to \c stderr, including the message, file name and line number.
109 * Optionally, if \c fatal is true, the macro
110 * will invoke \c std::terminate(). If \c fatal is false,
111 * the invoking function should return an error code to its caller,
112 * which is expected to call NS_FATAL_ERROR to cause termination.
113 *
114 * \param [in] msg The error message to print, if not empty.
115 * \param [in] fatal Call \c std::terminate() if true.
116 *
117 * This macro is enabled unconditionally in all builds,
118 * including debug and optimized builds.
119 */
120#define NS_FATAL_ERROR_IMPL(msg, fatal) \
121 do \
122 { \
123 std::cerr << "msg=\"" << msg << "\", "; \
124 NS_FATAL_ERROR_IMPL_NO_MSG(fatal); \
125 } while (false)
126
127/**
128 * \ingroup fatal
129 *
130 * \brief Report a fatal error and terminate.
131 *
132 * When this macro is hit at runtime, details of filename
133 * and line number are printed to \c stderr, and the program
134 * is halted by calling \c std::terminate(). This will
135 * trigger any clean up code registered by
136 * \c std::set_terminate (NS3 default is a stream-flushing
137 * code), but may be overridden.
138 *
139 * This macro is enabled unconditionally in all builds,
140 * including debug and optimized builds.
141 */
142#define NS_FATAL_ERROR_NO_MSG() NS_FATAL_ERROR_IMPL_NO_MSG(true)
143
144/**
145 * \ingroup fatal
146 *
147 * \brief Report a fatal error, deferring termination.
148 *
149 * When this macro is hit at runtime, details of filename
150 * and line number are printed to \c stderr, however the program
151 * is _not_ halted. It is expected that the function using this
152 * macro will return an error code, and its caller will
153 * invoke NS_FATAL_ERROR(msg) triggering std::terminate().
154 *
155 * This macro is enabled unconditionally in all builds,
156 * including debug and optimized builds.
157 */
158#define NS_FATAL_ERROR_NO_MSG_CONT() NS_FATAL_ERROR_IMPL_NO_MSG(false)
159
160/**
161 * \ingroup fatal
162 *
163 * \brief Report a fatal error with a message and terminate.
164 *
165 * \param [in] msg message to output when this macro is hit.
166 *
167 * When this macro is hit at runtime, the user-specified
168 * error message are printed to \c stderr, followed by a call
169 * to the NS_FATAL_ERROR_NO_MSG() macro which prints the
170 * details of filename and line number to \c stderr. The
171 * program will be halted by calling \c std::terminate(),
172 * triggering any clean up code registered by
173 * \c std::set_terminate (NS3 default is a stream-flushing
174 * code, but may be overridden).
175 *
176 * This macro is enabled unconditionally in all builds,
177 * including debug and optimized builds.
178 */
179#define NS_FATAL_ERROR(msg) NS_FATAL_ERROR_IMPL(msg, true)
180
181/**
182 * \ingroup fatal
183 *
184 * \brief Report a fatal error with a message, deferring termination.
185 *
186 * When this macro is hit at runtime, details of filename
187 * and line number are printed to \c stderr, however the program
188 * is _not_ halted. It is expected that the function using this
189 * macro will return an error code, and its caller will
190 * invoke NS_FATAL_ERROR(msg) triggering \c std::terminate().
191 *
192 * This macro is enabled unconditionally in all builds,
193 * including debug and optimized builds.
194 */
195#define NS_FATAL_ERROR_CONT(msg) NS_FATAL_ERROR_IMPL(msg, false)
196
197#endif /* FATAL_ERROR_H */
ns3::FatalImpl::RegisterStream(), ns3::FatalImpl::UnregisterStream(), and ns3::FatalImpl::FlushStream...
constexpr std::string_view NS_FATAL_MSG
Output string marking imminent invocation of std::terminate.
Definition: fatal-error.h:67
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.