A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Documentation ▼
Installation
Manual
Models
Contributing
Wiki
Development ▼
API Docs
Issue Tracker
Merge Requests
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
* SPDX-License-Identifier: GPL-2.0-only
5
*
6
* Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7
* Quincy Tse <quincy.tse@nicta.com.au>
8
*/
9
#ifndef NS3_FATAL_ERROR_H
10
#define NS3_FATAL_ERROR_H
11
12
#include "
fatal-impl.h
"
13
#include "
log.h
"
// LogGetTimePrinter, LogGetNodePrinter, LogLineFlushPartial
14
15
#include <cstdlib>
16
#include <exception>
17
#include <iostream>
18
#include <string_view>
19
20
#ifdef STACKTRACE_LIBRARY_IS_LINKED
21
/**
22
* @brief Macro prints the current stack trace to standard error.
23
*
24
* This macro is defined only if the stacktrace library is linked.
25
*
26
* @internal Delegates to ns3::FatalImpl::PrintStackTrace(), which is defined out-of-line
27
* (in fatal-impl.cc) so that the heavy \c <stacktrace> header -- and the
28
* \c <format> / \c <charconv> machinery it transitively drags in on some
29
* standard libraries -- stays out of this ubiquitously included header. The
30
* call is a no-op at runtime if the stacktrace library is not linked.
31
*/
32
#define PRINT_STACKTRACE ::ns3::FatalImpl::PrintStackTrace()
33
#else
34
/**
35
* @brief Macro does nothing if stacktrace library is not available.
36
*/
37
#define PRINT_STACKTRACE
38
#endif
39
40
/**
41
* @file
42
* @ingroup fatal
43
* @brief \c NS_FATAL_x macro definitions.
44
*/
45
46
/**
47
* @ingroup core
48
* @defgroup fatal Fatal Error Handlers
49
*
50
* @brief Functions to help clean up when a fatal error
51
* is encountered.
52
*
53
* The functions in this group are used to perform
54
* limited clean up, like flushing active streams, when
55
* fatal errors are encountered (through assertion fail,
56
* calls to NS_ABORT_* or calls to NS_FATAL_ERROR).
57
*
58
* Currently, other than flushing active ostreams, these
59
* functions does not interfere with outside memory. There
60
* is still a residual risk that invalid ostream
61
* pointers may be present, and may corrupt the memory
62
* on the attempt to execute the flush() function.
63
*/
64
65
namespace
ns3
66
{
67
68
/**
69
* @ingroup fatal
70
*
71
* @brief Output string marking imminent invocation of std::terminate.
72
*
73
* This is useful to know when capturing output and you want to strip
74
* system and compiler-dependent messages generated by std::terminate.
75
*/
76
constexpr
std::string_view
NS_FATAL_MSG
{
"NS_FATAL, terminating"
};
77
78
}
// namespace ns3
79
80
/**
81
* @ingroup fatal
82
*
83
* @brief Fatal error reporting with no message, implementation.
84
*
85
* When this macro is hit at runtime the error details will
86
* printed to \c stderr, including the file name and line number.
87
* Optionally, if \c fatal is true, the macro
88
* will invoke \c std::terminate(). If \c fatal is false,
89
* the invoking function should return an error code to its caller,
90
* which is expected to call NS_FATAL_ERROR to cause termination.
91
*
92
* @param [in] fatal Call \c std::terminate() if true.
93
*
94
* This macro is enabled unconditionally in all builds,
95
* including debug and optimized builds.
96
*
97
* The time and node prefixes are streamed directly to std::clog rather
98
* than through the buffered log line of NS_LOG (see ns3::LogLineBegin()),
99
* because the buffer may allocate memory, which we want to avoid on a
100
* fatal path. Keep the prefix format in sync with
101
* NS_LOG_APPEND_TIME_PREFIX and NS_LOG_APPEND_NODE_PREFIX in
102
* log-macros-enabled.h, so fatal messages and log lines look alike.
103
*/
104
#define NS_FATAL_ERROR_IMPL_NO_MSG(fatal) \
105
do \
106
{ \
107
::ns3::LogLineFlushPartial(); \
108
if (ns3::TimePrinter printer = ns3::LogGetTimePrinter(); printer != nullptr) \
109
{ \
110
(*printer)(std::clog); \
111
std::clog << " "; \
112
} \
113
if (ns3::NodePrinter printer = ns3::LogGetNodePrinter(); printer != nullptr) \
114
{ \
115
(*printer)(std::clog); \
116
std::clog << " "; \
117
} \
118
std::cerr << "file=" << __FILE__ << ", line=" << __LINE__ << std::endl; \
119
::ns3::FatalImpl::FlushStreams(); \
120
if (fatal) \
121
{ \
122
std::cerr << ns3::NS_FATAL_MSG << std::endl; \
123
PRINT_STACKTRACE; \
124
std::terminate(); \
125
} \
126
} while (false)
127
128
/**
129
* @ingroup fatal
130
*
131
* @brief Fatal error reporting with a message, implementation.
132
*
133
* When this macro is hit at runtime the error details will
134
* printed to \c stderr, including the message, file name and line number.
135
* Optionally, if \c fatal is true, the macro
136
* will invoke \c std::terminate(). If \c fatal is false,
137
* the invoking function should return an error code to its caller,
138
* which is expected to call NS_FATAL_ERROR to cause termination.
139
*
140
* @param [in] msg The error message to print, if not empty.
141
* @param [in] fatal Call \c std::terminate() if true.
142
*
143
* This macro is enabled unconditionally in all builds,
144
* including debug and optimized builds.
145
*/
146
#define NS_FATAL_ERROR_IMPL(msg, fatal) \
147
do \
148
{ \
149
::ns3::LogLineFlushPartial(); \
150
std::cerr << "msg=\"" << msg << "\", "; \
151
NS_FATAL_ERROR_IMPL_NO_MSG(fatal); \
152
} while (false)
153
154
/**
155
* @ingroup fatal
156
*
157
* @brief Report a fatal error and terminate.
158
*
159
* When this macro is hit at runtime, details of filename
160
* and line number are printed to \c stderr, and the program
161
* is halted by calling \c std::terminate(). This will
162
* trigger any clean up code registered by
163
* \c std::set_terminate (NS3 default is a stream-flushing
164
* code), but may be overridden.
165
*
166
* This macro is enabled unconditionally in all builds,
167
* including debug and optimized builds.
168
*/
169
#define NS_FATAL_ERROR_NO_MSG() NS_FATAL_ERROR_IMPL_NO_MSG(true)
170
171
/**
172
* @ingroup fatal
173
*
174
* @brief Report a fatal error, deferring termination.
175
*
176
* When this macro is hit at runtime, details of filename
177
* and line number are printed to \c stderr, however the program
178
* is _not_ halted. It is expected that the function using this
179
* macro will return an error code, and its caller will
180
* invoke NS_FATAL_ERROR(msg) triggering std::terminate().
181
*
182
* This macro is enabled unconditionally in all builds,
183
* including debug and optimized builds.
184
*/
185
#define NS_FATAL_ERROR_NO_MSG_CONT() NS_FATAL_ERROR_IMPL_NO_MSG(false)
186
187
/**
188
* @ingroup fatal
189
*
190
* @brief Report a fatal error with a message and terminate.
191
*
192
* @param [in] msg message to output when this macro is hit.
193
*
194
* When this macro is hit at runtime, the user-specified
195
* error message are printed to \c stderr, followed by a call
196
* to the NS_FATAL_ERROR_NO_MSG() macro which prints the
197
* details of filename and line number to \c stderr. The
198
* program will be halted by calling \c std::terminate(),
199
* triggering any clean up code registered by
200
* \c std::set_terminate (NS3 default is a stream-flushing
201
* code, but may be overridden).
202
*
203
* This macro is enabled unconditionally in all builds,
204
* including debug and optimized builds.
205
*/
206
#define NS_FATAL_ERROR(msg) NS_FATAL_ERROR_IMPL(msg, true)
207
208
/**
209
* @ingroup fatal
210
*
211
* @brief Report a fatal error with a message, deferring termination.
212
*
213
* When this macro is hit at runtime, details of filename
214
* and line number are printed to \c stderr, however the program
215
* is _not_ halted. It is expected that the function using this
216
* macro will return an error code, and its caller will
217
* invoke NS_FATAL_ERROR(msg) triggering \c std::terminate().
218
*
219
* This macro is enabled unconditionally in all builds,
220
* including debug and optimized builds.
221
*/
222
#define NS_FATAL_ERROR_CONT(msg) NS_FATAL_ERROR_IMPL(msg, false)
223
224
#endif
/* FATAL_ERROR_H */
fatal-impl.h
ns3::FatalImpl::RegisterStream(), ns3::FatalImpl::UnregisterStream(), and ns3::FatalImpl::FlushStream...
ns3::NS_FATAL_MSG
constexpr std::string_view NS_FATAL_MSG
Output string marking imminent invocation of std::terminate.
Definition
fatal-error.h:76
log.h
Debug message logging.
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
src
core
model
fatal-error.h
Generated on
for ns-3 by
1.16.1