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