oomph_definitions.cc
Go to the documentation of this file.
1// LIC// ====================================================================
2// LIC// This file forms part of oomph-lib, the object-oriented,
3// LIC// multi-physics finite-element library, available
4// LIC// at http://www.oomph-lib.org.
5// LIC//
6// LIC// Copyright (C) 2006-2025 Matthias Heil and Andrew Hazel
7// LIC//
8// LIC// This library is free software; you can redistribute it and/or
9// LIC// modify it under the terms of the GNU Lesser General Public
10// LIC// License as published by the Free Software Foundation; either
11// LIC// version 2.1 of the License, or (at your option) any later version.
12// LIC//
13// LIC// This library is distributed in the hope that it will be useful,
14// LIC// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// LIC// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16// LIC// Lesser General Public License for more details.
17// LIC//
18// LIC// You should have received a copy of the GNU Lesser General Public
19// LIC// License along with this library; if not, write to the Free Software
20// LIC// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21// LIC// 02110-1301 USA.
22// LIC//
23// LIC// The authors may be contacted at oomph-lib@maths.man.ac.uk.
24// LIC//
25// LIC//====================================================================
26// Non-inline and static member functions for the Oomph-lib
27// exception handlers
28
29#ifdef OOMPH_HAS_STACKTRACE
30#include "stacktrace.h"
31#endif
32#include "oomph_definitions.h"
33
34namespace oomph
35{
36 ///////////////////////////////////////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////////
38 ///////////////////////////////////////////////////////////////////////
39
40
41 /// =====================================================================
42 /// Namespace to control level of comprehensive timings
43 //======================================================================
44 namespace Global_timings
45 {
46 /// Global boolean to switch on comprehensive timing -- can
47 /// probably be declared const false when development on hector
48 /// is complete
50 }; // namespace Global_timings
51
52 /////////////////////////////////////////////////////////////////////
53 /////////////////////////////////////////////////////////////////////
54 /////////////////////////////////////////////////////////////////////
55
56 //=======================================================================
57 /// Helper namespace for set_terminate function -- used to spawn
58 /// messages from uncaught errors
59 /// =======================================================================
60 namespace TerminateHelper
61 {
62 /// Setup terminate helper
63 void setup()
64 {
66 Exception_stringstream_pt = new std::stringstream;
67 std::set_terminate(spawn_errors_from_uncaught_errors);
68 }
69
70 /// Flush string stream of error messages (call when error has been
71 /// caught)
73 {
75 Exception_stringstream_pt = new std::stringstream;
76 }
77
78 /// Function to spawn messages from uncaught errors
80 {
81 (*Error_message_stream_pt) << (*Exception_stringstream_pt).str();
82 }
83
84 /// Clean up function that deletes anything dynamically allocated
85 /// in this namespace
87 {
88 // If it's a null pointer
90 {
91 // Delete it
93
94 // Make it a null pointer
96 }
97 } // End of clean_up_memory
98
99 /// Stream to output error messages
100 std::ostream* Error_message_stream_pt = &std::cerr;
101
102 /// String stream that records the error message
103 std::stringstream* Exception_stringstream_pt = 0;
104 } // namespace TerminateHelper
105
106 ///////////////////////////////////////////////////////////////////////
107 ///////////////////////////////////////////////////////////////////////
108 ///////////////////////////////////////////////////////////////////////
109
110
111 /// =====================================================================
112 /// A class for handling oomph-lib run-time exceptions quietly.
113 //======================================================================
115
116
117 ///////////////////////////////////////////////////////////////////////
118 ///////////////////////////////////////////////////////////////////////
119 ///////////////////////////////////////////////////////////////////////
120
121
122 //========================================================================
123 /// The OomphLibException destructor actually spawns the error message
124 /// created in the constructor (unless suppresed)
125 //==========================================================================
127 {
129 {
130 (*Exception_stream_pt) << (*Exception_stringstream_pt).str();
131 }
134 }
135
136 //========================================================================
137 /// The OomphLibException constructor takes the error description,
138 /// function name, a location string provided by the
139 /// OOMPH_EXCEPTION_LOCATION and an exception type "WARNING" or "ERROR"
140 /// and combines them into a standard error message that is written to the
141 /// exception stream. The output_width of the message can also be specified.
142 /// Optionally provide a traceback of the function calls.
143 //==========================================================================
145 const std::string& function_name,
146 const char* location,
147 const std::string& exception_type,
148 std::ostream& exception_stream,
149 const unsigned& output_width,
150 bool list_trace_back)
151 : std::runtime_error("OomphException")
152 {
153 // By default we shout
155
156 // Store exception stream
158
159 // Create storage for error message
160 Exception_stringstream_pt = new std::stringstream;
161
162 // Build an exception header string from the information passed
163 // Start with a couple of new lines to space things out
164 std::string exception_header = "\n\n";
165
166 // Now add a dividing line
167 for (unsigned i = 0; i < output_width; i++)
168 {
169 exception_header += "=";
170 }
171 exception_header += "\n";
172
173 // Write the type of exception
174 exception_header += "Oomph-lib ";
176
177 // Add the function in which it occurs
178 exception_header += "\n\n at ";
180 exception_header += "\n\n in ";
182
183 // Finish with two new lines
184 exception_header += "\n\n";
185
186 // and a closing line
187 for (unsigned i = 0; i < (unsigned)(0.8 * output_width); i++)
188 {
189 exception_header += "-";
190 }
191
192 // Output the error header to the stream
193 (*Exception_stringstream_pt) << exception_header << std::endl;
194
195 // Report the error
196 (*Exception_stringstream_pt) << std::endl << error_description << std::endl;
197
198#ifdef OOMPH_HAS_STACKTRACE
199 // Print the stacktrace
200 if (list_trace_back)
201 {
203 }
204#endif
205
206 // Finish off with another set of double lines
207 for (unsigned i = 0; i < output_width; i++)
208 {
209 (*Exception_stringstream_pt) << "=";
210 }
211 (*Exception_stringstream_pt) << std::endl << std::endl;
212
213 // If a Problem object hasn't been constructed yet then
214 // TerminateHelper::setup() will likely not have been called and the
215 // TerminateHelper::Exception_stringstream_pt will be a null pointer.
216 // If so, we should call TerminateHelper::setup() ourselves.
219
220 // Copy message to stream in terminate helper in case the message
221 // doesn't get caught and/or doesn/t make it to the destructor
222 (*TerminateHelper::Exception_stringstream_pt)
223 << (*Exception_stringstream_pt).str();
224 }
225
226 //========================================================================
227 /// Default output stream for OomphLibErorrs (cerr)
228 //========================================================================
229 std::ostream* OomphLibError::Stream_pt = &std::cerr;
230
231 //=======================================================================
232 /// Default output width for OomphLibErrors (70)
233 //=======================================================================
234 unsigned OomphLibError::Output_width = 70;
235
236 //=======================================================================
237 /// Default output stream for OomphLibWarnings(cerr)
238 //=======================================================================
239 std::ostream* OomphLibWarning::Stream_pt = &std::cerr;
240
241 //=======================================================================
242 /// Default output width for OomphLibWarnings (70)
243 //=======================================================================
244 unsigned OomphLibWarning::Output_width = 70;
245
246
247 ////////////////////////////////////////////////////////////////////////
248 ////////////////////////////////////////////////////////////////////////
249 ////////////////////////////////////////////////////////////////////////
250
251
252 //=======================================================================
253 /// Namespace containing an output stream that can be used for
254 /// debugging. Use at your own risk -- global data is evil!
255 //=======================================================================
256 namespace Global_output_stream
257 {
258 /// Output stream
259 std::ofstream* Outfile = 0;
260
261 } // namespace Global_output_stream
262
263
264 ////////////////////////////////////////////////////////////////////////
265 ////////////////////////////////////////////////////////////////////////
266 ////////////////////////////////////////////////////////////////////////
267
268
269 //=======================================================================
270 /// Namespace containing a number that can be used to annotate things for
271 /// debugging. Use at your own risk -- global data is evil!
272 //=======================================================================
273 namespace Global_unsigned
274 {
275 /// The unsigned
276 unsigned Number = 0;
277
278 } // namespace Global_unsigned
279 ////////////////////////////////////////////////////////////////////////
280 ////////////////////////////////////////////////////////////////////////
281 ////////////////////////////////////////////////////////////////////////
282
283
284 //=======================================================================
285 /// Namespace containing a vector of strings that can be used to
286 /// to store global output modifiers. This is global data
287 /// and you use it at your own risk!
288 //=======================================================================
289 namespace Global_string_for_annotation
290 {
291 /// Return the i-th string or "" if the relevant string hasn't
292 /// been defined
293 std::string string(const unsigned& i)
294 {
295 if (i < String.size())
296 {
297 return String[i];
298 }
299 else
300 {
301 return "";
302 }
303 }
304
305 /// Storage for strings that may be used for global annotations.
306 /// This is global data and you use it at your own risk!
307 std::vector<std::string> String;
308
309 } // namespace Global_string_for_annotation
310
311
312 ////////////////////////////////////////////////////////////////////////////
313 ////////////////////////////////////////////////////////////////////////////
314 ////////////////////////////////////////////////////////////////////////////
315
316
317 //========================================================================
318 /// Single (global) instantiation of the Nullstream
319 //========================================================================
321
322 //========================================================================
323 /// Single (global) instantiation of the OomphInfo object -- this
324 /// is used throughout the library as a "replacement" for std::cout
325 //========================================================================
327
328
329 //========================================================================
330 /// Single global instatiation of the default output modifier.
331 //========================================================================
333
334} // namespace oomph
cstr elem_len * i
Definition cfortran.h:603
A small nullstream class that throws away everything sent to it.
This class is a wrapper to a stream and an output modifier that is used to control the "info" output ...
static unsigned Output_width
Width in characters of the output report.
static std::ostream * Stream_pt
Output stream that is used to write the errors.
~OomphLibException()
The destructor cannot throw an exception (C++ STL standard)
OomphLibException(const std::string &error_description, const std::string &function_name, const char *location, const std::string &exception_type, std::ostream &exception_stream, const unsigned &output_width, bool list_trace_back)
Constructor takes the error description, function name and a location string provided by the OOMPH_EX...
std::stringstream * Exception_stringstream_pt
String stream that records the error message.
std::ostream * Exception_stream_pt
Exception stream to which we write message in destructor.
bool Suppress_error_message
Boolean to suppress issuing of the error message in destructor (useful if error is caught successfull...
static unsigned Output_width
Width of output.
static std::ostream * Stream_pt
Output stream that is used to write the errors.
A base class that contains a single virtual member function: The () operator that may be used to modi...
TAdvectionDiffusionReactionElement<NREAGENT,DIM,NNODE_1D> elements are isoparametric triangular DIM-d...
std::ofstream * Outfile
Output stream.
std::vector< std::string > String
Storage for strings that may be used for global annotations. This is global data and you use it at yo...
std::string string(const unsigned &i)
Return the i-th string or "" if the relevant string hasn't been defined.
bool Doc_comprehensive_timings
Global boolean to switch on comprehensive timing – can probably be declared const false when developm...
unsigned Number
The unsigned.
void clean_up_memory()
Clean up function that deletes anything dynamically allocated in this namespace.
void suppress_exception_error_messages()
Flush string stream of error messages (call when error has been caught)
void setup()
Setup terminate helper.
void spawn_errors_from_uncaught_errors()
Function to spawn messages from uncaught errors.
std::stringstream * Exception_stringstream_pt
String stream that records the error message.
std::ostream * Error_message_stream_pt
Stream to output error messages.
DRAIG: Change all instances of (SPATIAL_DIM) to (DIM-1).
Nullstream oomph_nullstream
Single (global) instantiation of the Nullstream.
OutputModifier default_output_modifier
Single global instatiation of the default output modifier.
OomphInfo oomph_info
Single (global) instantiation of the OomphInfo object – this is used throughout the library as a "rep...
static void print_stacktrace(std::ostream &exception_stream, unsigned int max_frames=63)
Definition stacktrace.h:47