error.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2015 OpenFOAM Foundation
9  Copyright (C) 2015-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::error
29 
30 Description
31  Class to handle errors and exceptions in a simple, consistent stream-based
32  manner.
33 
34  The error class is globally instantiated with a title string. Errors,
35  messages and other data are piped to the messageStream class in the
36  standard manner. Manipulators are supplied for exit and abort that may
37  terminate the program or throw an exception depending on whether the
38  exception handling has been switched on (off by default).
39 
40 Usage
41  \code
42  error << "message1" << "message2" << FoamDataType << exit(errNo);
43  error << "message1" << "message2" << FoamDataType << abort();
44  \endcode
45 
46 SourceFiles
47  error.C
48  IOerror.C
49 
50 SeeAlso
51  Foam::errorManip
52  Foam::errorManipArg
53 
54 \*---------------------------------------------------------------------------*/
55 
56 #ifndef error_H
57 #define error_H
58 
59 #include "messageStream.H"
60 #include <memory>
61 
62 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
63 
64 namespace Foam
65 {
66 
67 /*---------------------------------------------------------------------------*\
68  Class error Declaration
69 \*---------------------------------------------------------------------------*/
70 
71 class error
72 :
73  public std::exception,
74  public messageStream
75 {
76  // Private Member Functions
77 
78  //- Common code for exit or abort
79  void exitOrAbort(const int errNo, const bool isAbort);
80 
81 
82 protected:
83 
84  // Protected Data
85 
86  string functionName_;
89  bool throwing_;
90  std::unique_ptr<OStringStream> messageStreamPtr_;
91 
92 
93 public:
94 
95  // Constructors
96 
97  //- Construct from title string
98  explicit error(const string& title);
99 
100  //- Construct from dictionary
101  explicit error(const dictionary& errDict);
102 
103  //- Copy construct
104  error(const error& err);
105 
106 
107  //- Destructor
108  virtual ~error() noexcept;
109 
110 
111  // Static Functions
112 
113  //- Emit warning on stderr about something being old.
114  // \param what description for the warning
115  // \param version is the old version (YYMM) for determining the
116  // age in months compared to the current OpenFOAM version
117  // as conveyed by the \c foamVersion::api value.
118  static void warnAboutAge(const char* what, const int version);
119 
120 
121  // Member Functions
122 
123  //- The accumulated error message
124  string message() const;
125 
126  //- Clear any messages
127  void clear() const;
128 
129  const string& functionName() const
130  {
131  return functionName_;
132  }
133 
134  const string& sourceFileName() const
135  {
136  return sourceFileName_;
137  }
138 
139  label sourceFileLineNumber() const
140  {
141  return sourceFileLineNumber_;
142  }
143 
144  //- Return the current exception throwing (on or off)
145  bool throwing() const
146  {
147  return throwing_;
148  }
149 
150  //- Activate/deactivate exception throwing
151  // \return the previous throwing state
152  inline bool throwExceptions(bool doThrow)
153  {
154  const bool prev = throwing_;
155  throwing_ = doThrow;
156  return prev;
157  }
158 
159  //- Activate exception throwing
160  // \return the previous throwing state
161  inline bool throwExceptions()
162  {
163  return throwExceptions(true);
164  }
165 
166  //- Deactivate exception throwing
167  // \return the previous throwing state
168  inline bool dontThrowExceptions()
169  {
170  return throwExceptions(false);
171  }
172 
173  //- Define basic print message
174  // \return OSstream for further info.
175  OSstream& operator()
176  (
177  const string& functionName
178  );
179 
180  //- Define basic print message
181  // \return OSstream for further info.
182  OSstream& operator()
183  (
184  const char* functionName,
185  const char* sourceFileName,
186  const int sourceFileLineNumber = 0
187  );
188 
189  //- Define basic print message
190  // \return OSstream for further info.
191  OSstream& operator()
192  (
193  const string& functionName,
194  const char* sourceFileName,
195  const int sourceFileLineNumber = 0
196  );
197 
198  //- Explicit convert to OSstream for << operations
200  {
201  return operator OSstream&();
202  }
203 
204  //- Convert to OSstream
205  operator OSstream&();
206 
207  //- Create and return a dictionary representation of the error
208  operator dictionary() const;
209 
210 
211  //- Helper function to print a stack,
212  //- used when OpenFOAM IO not yet initialised.
213  static void safePrintStack(std::ostream& os);
214 
215  //- Helper function to print a stack
216  static void printStack(Ostream& os);
217 
218  //- True if FOAM_ABORT is on.
219  static bool useAbort();
220 
221  //- Exit : can be called for any error to exit program.
222  // Redirects to abort() when FOAM_ABORT is on.
223  void exit(const int errNo = 1);
224 
225  //- Abort : used to stop code for fatal errors.
226  // Prints stack before exiting.
227  void abort();
228 
229  //- Print error message
230  void write(Ostream& os, const bool includeTitle = true) const;
231 };
232 
233 
234 /*---------------------------------------------------------------------------*\
235  Class IOerror Declaration
236 \*---------------------------------------------------------------------------*/
237 
238 //- Report an I/O error
239 class IOerror
240 :
241  public error
242 {
243  // Private Data
244 
245  string ioFileName_;
246  label ioStartLineNumber_;
247  label ioEndLineNumber_;
248 
249 
250  // Private Member Functions
251 
252  //- Common code for exit or abort
253  void exitOrAbort(const int errNo, const bool isAbort);
254 
255 
256 public:
257 
258  // Constructors
259 
260  //- Construct from title string
261  explicit IOerror(const string& title);
262 
263  //- Construct from dictionary
264  explicit IOerror(const dictionary& errDict);
265 
266 
267  //- Destructor
268  virtual ~IOerror() noexcept;
269 
270 
271  // Member Functions
272 
273  const string& ioFileName() const
274  {
275  return ioFileName_;
276  }
277 
278  label ioStartLineNumber() const
279  {
280  return ioStartLineNumber_;
281  }
282 
283  label ioEndLineNumber() const
284  {
285  return ioEndLineNumber_;
286  }
287 
288  //- Convert to OSstream
289  // Prints basic message and returns OSstream for further info.
290  OSstream& operator()
291  (
292  const char* functionName,
293  const char* sourceFileName,
294  const int sourceFileLineNumber,
295  const string& ioFileName,
296  const label ioStartLineNumber = -1,
297  const label ioEndLineNumber = -1
298  );
299 
300  //- Convert to OSstream
301  // Prints basic message and returns OSstream for further info.
302  OSstream& operator()
303  (
304  const char* functionName,
305  const char* sourceFileName,
306  const int sourceFileLineNumber,
307  const IOstream& ioStream
308  );
309 
310  //- Convert to OSstream
311  // Prints basic message and returns OSstream for further info.
312  OSstream& operator()
313  (
314  const char* functionName,
315  const char* sourceFileName,
316  const int sourceFileLineNumber,
317  const dictionary& dict
318  );
319 
320  //- Print basic message and exit.
321  // Uses cerr if streams not yet constructed (at startup).
322  // Use in startup parsing instead of FatalError.
323  static void SafeFatalIOError
324  (
325  const char* functionName,
326  const char* sourceFileName,
327  const int sourceFileLineNumber,
328  const IOstream& ioStream,
329  const string& msg
330  );
331 
332  //- Create and return a dictionary representation of the error
333  operator dictionary() const;
334 
335 
336  //- Exit : can be called for any error to exit program
337  void exit(const int errNo = 1);
338 
339  //- Abort : used to stop code for fatal errors
340  void abort();
341 
342  //- Print error message
343  void write(Ostream& os, const bool includeTitle = true) const;
344 };
345 
346 
347 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
348 
349 //- Ostream operator
350 Ostream& operator<<(Ostream& os, const error& err);
351 
352 //- Ostream operator
353 Ostream& operator<<(Ostream& os, const IOerror& err);
354 
355 
356 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
357 // Global error declarations: defined in error.C
358 
359 //- Error stream (uses stdout - output on all processes),
360 //- with additional 'FOAM FATAL ERROR' header text and stack trace.
361 extern error FatalError;
362 
363 //- Error stream (uses stdout - output on all processes),
364 //- with additional 'FOAM FATAL IO ERROR' header text and stack trace.
365 extern IOerror FatalIOError;
366 
367 
368 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
369 
370 } // End namespace Foam
371 
372 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
373 // Convenience macros to add the file name and line number to the function name
374 
375 //- Report an error message using Foam::FatalError
376 // for functionName in file __FILE__ at line __LINE__
377 #define FatalErrorIn(functionName) \
378  ::Foam::FatalError((functionName), __FILE__, __LINE__)
379 
380 //- Report an error message using Foam::FatalError
381 // for FUNCTION_NAME in file __FILE__ at line __LINE__
382 #define FatalErrorInFunction FatalErrorIn(FUNCTION_NAME)
383 
384 //- Report an error message using Foam::FatalError
385 // about unknown lookup type in table
386 #define FatalErrorInLookup(lookupTag, lookupName, lookupTable) \
387  ::Foam::FatalError(FUNCTION_NAME, __FILE__, __LINE__) \
388  << "Unknown " << (lookupTag) << " type " << (lookupName) \
389  << "\n\nValid " << (lookupTag) << " types :\n" \
390  << ((lookupTable).sortedToc()) << '\n'
391 
392 
393 //- Report an error message using Foam::FatalIOError
394 // for functionName in file __FILE__ at line __LINE__
395 // for a particular IOstream
396 #define FatalIOErrorIn(functionName, ios) \
397  ::Foam::FatalIOError((functionName), __FILE__, __LINE__, (ios))
398 
399 //- Report an error message using Foam::FatalIOError
400 // for FUNCTION_NAME in file __FILE__ at line __LINE__
401 // for a particular IOstream
402 #define FatalIOErrorInFunction(ios) FatalIOErrorIn(FUNCTION_NAME, ios)
403 
404 
405 //- Report an error message using Foam::FatalIOError
406 // about unknown lookup type in table
407 #define FatalIOErrorInLookup(ios, lookupTag, lookupName, lookupTable) \
408  ::Foam::FatalIOError(FUNCTION_NAME, __FILE__, __LINE__, (ios)) \
409  << "Unknown " << (lookupTag) << " type " << (lookupName) \
410  << "\n\nValid " << (lookupTag) << " types :\n" \
411  << ((lookupTable).sortedToc()) << '\n'
412 
413 
414 //- Report an error message using Foam::FatalIOError
415 // (or cerr if FatalIOError not yet constructed)
416 // for functionName in file __FILE__ at line __LINE__
417 // for a particular IOstream
418 #define SafeFatalIOErrorIn(functionName, ios, msg) \
419  ::Foam::IOerror::SafeFatalIOError \
420  ((functionName), __FILE__, __LINE__, (ios), (msg))
421 
422 //- Report an error message using Foam::FatalIOError
423 // (or cerr if FatalIOError not yet constructed)
424 // for functionName in file __FILE__ at line __LINE__
425 // for a particular IOstream
426 #define SafeFatalIOErrorInFunction(ios, msg) \
427  SafeFatalIOErrorIn(FUNCTION_NAME, ios, msg)
428 
429 
430 //- Issue a FatalErrorIn for a function not currently implemented.
431 // The functionName is printed and then abort is called.
432 //
433 // This macro can be particularly useful when methods must be defined to
434 // complete the interface of a derived class even if they should never be
435 // called for this derived class.
436 #define notImplemented(functionName) \
437  FatalErrorIn(functionName) \
438  << "Not implemented" << ::Foam::abort(FatalError);
439 
440 //- Issue a FatalErrorIn for a function not currently implemented.
441 // The FUNCTION_NAME is printed and then abort is called.
442 //
443 // This macro can be particularly useful when methods must be defined to
444 // complete the interface of a derived class even if they should never be
445 // called for this derived class.
446 #define NotImplemented notImplemented(FUNCTION_NAME)
447 
448 
449 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
450 
451 #include "errorManip.H"
452 
453 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
454 
455 #endif
456 
457 // ************************************************************************* //
Foam::IOerror::ioEndLineNumber
label ioEndLineNumber() const
Definition: error.H:282
Foam::error::sourceFileName
const string & sourceFileName() const
Definition: error.H:133
Foam::error::printStack
static void printStack(Ostream &os)
Helper function to print a stack.
Definition: dummyPrintStack.C:36
Foam::error::messageStreamPtr_
std::unique_ptr< OStringStream > messageStreamPtr_
Definition: error.H:89
Foam::error::dontThrowExceptions
bool dontThrowExceptions()
Deactivate exception throwing.
Definition: error.H:167
Foam::IOerror::abort
void abort()
Abort : used to stop code for fatal errors.
Definition: IOerror.C:255
Foam::error::throwExceptions
bool throwExceptions()
Activate exception throwing.
Definition: error.H:160
Foam::messageStream
Class to handle messaging in a simple, consistent stream-based manner.
Definition: messageStream.H:71
Foam::error::sourceFileName_
string sourceFileName_
Definition: error.H:86
Foam::IOerror::IOerror
IOerror(const string &title)
Construct from title string.
Definition: IOerror.C:38
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:75
Foam::IOerror::ioStartLineNumber
label ioStartLineNumber() const
Definition: error.H:277
Foam::error::functionName
const string & functionName() const
Definition: error.H:128
SafeFatalIOErrorIn
#define SafeFatalIOErrorIn(functionName, ios, msg)
Report an error message using Foam::FatalIOError.
Definition: error.H:417
Foam::FatalIOError
IOerror FatalIOError
Foam::error::throwing
bool throwing() const
Return the current exception throwing (on or off)
Definition: error.H:144
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
NotImplemented
#define NotImplemented
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:445
Foam::IOerror::SafeFatalIOError
static void SafeFatalIOError(const char *functionName, const char *sourceFileName, const int sourceFileLineNumber, const IOstream &ioStream, const string &msg)
Print basic message and exit.
Definition: IOerror.C:124
Foam::IOerror::ioFileName
const string & ioFileName() const
Definition: error.H:272
FatalIOErrorInLookup
#define FatalIOErrorInLookup(ios, lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalIOError.
Definition: error.H:406
Foam::error::error
error(const string &title)
Construct from title string.
Definition: error.C:83
notImplemented
#define notImplemented(functionName)
Issue a FatalErrorIn for a function not currently implemented.
Definition: error.H:435
FatalIOErrorIn
#define FatalIOErrorIn(functionName, ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:395
Foam::OSstream
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:54
Foam::IOerror::~IOerror
virtual ~IOerror() noexcept
Destructor.
Definition: IOerror.C:58
dict
dictionary dict
Definition: searchingEngine.H:14
SafeFatalIOErrorInFunction
#define SafeFatalIOErrorInFunction(ios, msg)
Report an error message using Foam::FatalIOError.
Definition: error.H:425
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
FatalErrorInLookup
#define FatalErrorInLookup(lookupTag, lookupName, lookupTable)
Report an error message using Foam::FatalError.
Definition: error.H:385
Foam::error::message
string message() const
The accumulated error message.
Definition: error.C:277
Foam::error::sourceFileLineNumber_
label sourceFileLineNumber_
Definition: error.H:87
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::error::useAbort
static bool useAbort()
True if FOAM_ABORT is on.
Definition: error.C:74
Foam::error::exit
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: error.C:289
Foam::foamVersion::version
const std::string version
OpenFOAM version (name or stringified number) as a std::string.
messageStream.H
Foam::error::operator()
OSstream & operator()()
Explicit convert to OSstream for << operations.
Definition: error.H:198
Foam::error::~error
virtual ~error() noexcept
Destructor.
Definition: error.C:121
Foam::error::functionName_
string functionName_
Definition: error.H:85
Foam::error::sourceFileLineNumber
label sourceFileLineNumber() const
Definition: error.H:138
Foam::IOerror::exit
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:249
Foam::error::warnAboutAge
static void warnAboutAge(const char *what, const int version)
Emit warning on stderr about something being old.
Definition: error.C:41
Foam::error::abort
void abort()
Abort : used to stop code for fatal errors.
Definition: error.C:295
Foam::IOerror::write
void write(Ostream &os, const bool includeTitle=true) const
Print error message.
Definition: IOerror.C:261
Foam::error::clear
void clear() const
Clear any messages.
Definition: error.C:283
FUNCTION_NAME
#define FUNCTION_NAME
Definition: messageStream.H:270
Foam::messageStream::title
const string & title() const
The title of this error type.
Definition: messageStream.H:132
errorManip.H
Foam::IOerror
Report an I/O error.
Definition: error.H:238
FatalErrorIn
#define FatalErrorIn(functionName)
Report an error message using Foam::FatalError.
Definition: error.H:376
Foam::error::write
void write(Ostream &os, const bool includeTitle=true) const
Print error message.
Definition: error.C:301
Foam::error::throwing_
bool throwing_
Definition: error.H:88
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::error
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Definition: error.H:70
Foam::error::safePrintStack
static void safePrintStack(std::ostream &os)
Definition: dummyPrintStack.C:32