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-2023 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 
34 namespace 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
154  Suppress_error_message = false;
155 
156  // Store exception stream
157  Exception_stream_pt = &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 ";
175  exception_header += exception_type;
176 
177  // Add the function in which it occurs
178  exception_header += "\n\n at ";
179  exception_header += location;
180  exception_header += "\n\n in ";
181  exception_header += function_name;
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  // Copy message to stream in terminate helper in case the message
214  // doesn't get caught and/or doesn/t make it to the destructor
215  (*TerminateHelper::Exception_stringstream_pt)
216  << (*Exception_stringstream_pt).str();
217  }
218 
219  //========================================================================
220  /// Default output stream for OomphLibErorrs (cerr)
221  //========================================================================
222  std::ostream* OomphLibError::Stream_pt = &std::cerr;
223 
224  //=======================================================================
225  /// Default output width for OomphLibErrors (70)
226  //=======================================================================
227  unsigned OomphLibError::Output_width = 70;
228 
229  //=======================================================================
230  /// Default output stream for OomphLibWarnings(cerr)
231  //=======================================================================
232  std::ostream* OomphLibWarning::Stream_pt = &std::cerr;
233 
234  //=======================================================================
235  /// Default output width for OomphLibWarnings (70)
236  //=======================================================================
237  unsigned OomphLibWarning::Output_width = 70;
238 
239 
240  /// /////////////////////////////////////////////////////////////////////
241  /// /////////////////////////////////////////////////////////////////////
242  /// /////////////////////////////////////////////////////////////////////
243 
244 
245  //=======================================================================
246  /// Namespace containing an output stream that can be used for
247  /// debugging. Use at your own risk -- global data is evil!
248  //=======================================================================
249  namespace Global_output_stream
250  {
251  /// Output stream
252  std::ofstream* Outfile = 0;
253 
254  } // namespace Global_output_stream
255 
256 
257  /// /////////////////////////////////////////////////////////////////////
258  /// /////////////////////////////////////////////////////////////////////
259  /// /////////////////////////////////////////////////////////////////////
260 
261 
262  //=======================================================================
263  /// Namespace containing a number that can be used to annotate things for
264  /// debugging. Use at your own risk -- global data is evil!
265  //=======================================================================
266  namespace Global_unsigned
267  {
268  /// The unsigned
269  unsigned Number = 0;
270 
271  } // namespace Global_unsigned
272  /// /////////////////////////////////////////////////////////////////////
273  /// /////////////////////////////////////////////////////////////////////
274  /// /////////////////////////////////////////////////////////////////////
275 
276 
277  //=======================================================================
278  /// Namespace containing a vector of strings that can be used to
279  /// to store global output modifiers. This is global data
280  /// and you use it at your own risk!
281  //=======================================================================
282  namespace Global_string_for_annotation
283  {
284  /// Return the i-th string or "" if the relevant string hasn't
285  /// been defined
286  std::string string(const unsigned& i)
287  {
288  if (i < String.size())
289  {
290  return String[i];
291  }
292  else
293  {
294  return "";
295  }
296  }
297 
298  /// Storage for strings that may be used for global annotations.
299  /// This is global data and you use it at your own risk!
300  std::vector<std::string> String;
301 
302  } // namespace Global_string_for_annotation
303 
304 
305  /// /////////////////////////////////////////////////////////////////////////
306  /// /////////////////////////////////////////////////////////////////////////
307  /// /////////////////////////////////////////////////////////////////////////
308 
309 
310  //========================================================================
311  /// Single (global) instantiation of the Nullstream
312  //========================================================================
314 
315  //========================================================================
316  /// Single (global) instantiation of the OomphInfo object -- this
317  /// is used throughout the library as a "replacement" for std::cout
318  //========================================================================
320 
321 
322  //========================================================================
323  /// Single global instatiation of the default output modifier.
324  //========================================================================
326 
327 } // namespace oomph
cstr elem_len * i
Definition: cfortran.h:603
///////////////////////////////////////////////////////////////////// ///////////////////////////////...
///////////////////////////////////////////////////////////////////// ///////////////////////////////...
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.
///////////////////////////////////////////////////////////////////// ///////////////////////////////...
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.
//////////////////////////////////////////////////////////////////// ////////////////////////////////...
Nullstream oomph_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