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-2023 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 Foam_error_H
57 #define Foam_error_H
58 
59 #include "messageStream.H"
60 #include <memory>
61 
62 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
63 
64 namespace Foam
65 {
66 
67 // Forward Declarations
68 class OStringStream;
69 template<class EnumType> class Enum;
70 
71 /*---------------------------------------------------------------------------*\
72  Class error Declaration
73 \*---------------------------------------------------------------------------*/
74 
75 class error
76 :
77  public std::exception,
78  public messageStream
79 {
80  // Private Member Functions
81 
82  //- Exit or abort code, with exception and jobinfo handling
83  void exiting(const int errNo, const bool isAbort);
84 
85 
86 protected:
87 
88  // Protected Data
89 
90  string functionName_;
93  bool throwing_;
94  std::unique_ptr<OStringStream> messageStreamPtr_;
95 
96 
97  // Protected Member Functions
98 
99  //- Exit or abort, without throwing or job control handling
100  void simpleExit(const int errNo, const bool isAbort);
101 
102 
103 public:
104 
105  // Data Types
106 
107  //- Handling of errors. The exact handling depends on the local context.
108  enum class handlerTypes : char
109  {
110  DEFAULT = 0,
111  IGNORE,
112  WARN,
113  STRICT
114  };
115 
116  //- Names of the error handler types
117  static const Enum<handlerTypes> handlerNames;
118 
119 
120  // Constructors
121 
122  //- Construct from title string
123  explicit error(const string& title);
124 
125  //- Construct from dictionary
126  explicit error(const dictionary& errDict);
127 
128  //- Copy construct
129  error(const error& err);
130 
131 
132  //- Destructor
133  virtual ~error() noexcept;
134 
135 
136  // Static Functions
137 
138  //- Like Pstream::master but with a Pstream::parRun guard in case
139  //- Pstream has not yet been initialised.
140  //
141  // \param communicator is the numbered MPI communicator.
142  // By default it uses UPstream::worldComm
143  static bool master(const label communicator = -1);
144 
145  //- Test if an age warning should be emitted.
146  // \param version is the old version (YYMM) for determining the
147  // age in months compared to the current OpenFOAM version
148  // as conveyed by the \c foamVersion::api value.
149  static bool warnAboutAge(const int version) noexcept;
150 
151  //- Emit warning on stderr about something being old.
152  // \param what description for the warning
153  // \param version is the old version (YYMM) for determining the
154  // age in months compared to the current OpenFOAM version
155  // as conveyed by the \c foamVersion::api value.
156  static bool warnAboutAge(const char* what, const int version);
157 
158 
159  // Member Functions
160 
161  //- The accumulated error message
162  string message() const;
163 
164  //- Clear any messages
165  void clear() const;
166 
167  //- The currently defined function name for output messages
168  const string& functionName() const noexcept
169  {
170  return functionName_;
171  }
172 
173  //- The currently defined source-file name for output messages
174  const string& sourceFileName() const noexcept
175  {
176  return sourceFileName_;
177  }
178 
179  //- The currently defined source-file line number for output messages
180  label sourceFileLineNumber() const noexcept
181  {
182  return sourceFileLineNumber_;
183  }
184 
185  //- Return the current exception throwing state (on or off)
186  bool throwing() const noexcept
187  {
188  return throwing_;
189  }
190 
191  //- Specify exception throwing state (on or off)
192  // \return the previous throwing state
193  bool throwing(const bool on) noexcept
194  {
195  bool old(throwing_);
196  throwing_ = on;
197  return old;
198  }
199 
200 
201  // Output
203  //- Return OSstream for output operations
204  OSstream& stream();
205 
206  //- Implicit cast to OSstream for << operations
207  operator OSstream&()
208  {
209  return this->stream();
210  }
211 
212  //- Explicit convert to OSstream for << operations
214  {
215  return this->stream();
216  }
217 
218  //- Define basic print message
219  // \return OSstream for further operations
220  OSstream& operator()
221  (
222  const string& functionName
223  );
224 
225  //- Define basic print message
226  // \return OSstream for further operations
227  OSstream& operator()
228  (
229  const char* functionName,
230  const char* sourceFileName,
231  const int sourceFileLineNumber = 0
232  );
233 
234  //- Define basic print message
235  // \return OSstream for further operations
236  OSstream& operator()
237  (
238  const string& functionName,
239  const char* sourceFileName,
240  const int sourceFileLineNumber = 0
241  );
242 
243 
244  // Other
245 
246  //- Extract a dictionary representation of the error information
247  operator dictionary() const;
248 
249 
250  //- Helper function to print a stack, with optional upper limit.
251  //- Used when OpenFOAM IO not yet initialised.
252  static void safePrintStack(std::ostream& os, int size = -1);
253 
254  //- Helper function to print a stack, with optional upper limit.
255  static void printStack(Ostream& os, int size = -1);
256 
257  //- True if FOAM_ABORT is on.
258  static bool useAbort();
259 
260 
261  //- Exit : can be called for any error to exit program.
262  // Redirects to abort() when FOAM_ABORT is on.
263  void exit(const int errNo = 1);
264 
265  //- Abort : used to stop code for fatal errors.
266  // Prints stack before exiting.
267  void abort();
268 
269  //- Print error message
270  virtual void write(Ostream& os, const bool withTitle = true) const;
271 
272 
273  // Housekeeping
274 
275  //- Specify exception throwing state (default is \b on)
276  // \return the previous throwing state
277  bool throwExceptions(const bool on = true) noexcept
278  {
279  return throwing(on);
280  }
281 
282  //- Deactivate exception throwing
283  // \return the previous throwing state
285  {
286  return throwExceptions(false);
287  }
288 };
289 
290 
291 /*---------------------------------------------------------------------------*\
292  Class IOerror Declaration
293 \*---------------------------------------------------------------------------*/
294 
295 //- Report an I/O error
296 class IOerror
297 :
298  public error
299 {
300  // Private Data
301 
302  string ioFileName_;
303  label ioStartLineNumber_;
304  label ioEndLineNumber_;
305 
306 
307  // Private Member Functions
308 
309  //- Exit or abort code, with exception and jobinfo handling
310  void exiting(const int errNo, const bool isAbort);
311 
312 
313 public:
314 
315  // Constructors
316 
317  //- Construct from title string
318  explicit IOerror(const string& title);
319 
320  //- Construct from dictionary
321  explicit IOerror(const dictionary& errDict);
322 
323 
324  //- Destructor
325  virtual ~IOerror() noexcept;
326 
327 
328  // Member Functions
329 
330  //- The currently defined IO name for output messages
331  const string& ioFileName() const noexcept
332  {
333  return ioFileName_;
334  }
335 
336  //- The currently defined IO start-line number for output messages
337  label ioStartLineNumber() const noexcept
338  {
339  return ioStartLineNumber_;
340  }
341 
342  //- The currently defined IO end-line number
343  label ioEndLineNumber() const noexcept
344  {
345  return ioEndLineNumber_;
346  }
347 
348 
349  // Output
350 
351  //- Define basic print message
352  // \return OSstream for further operations
353  OSstream& operator()
354  (
355  const char* functionName,
356  const char* sourceFileName,
357  const int sourceFileLineNumber,
358  const string& ioFileName,
359  const label ioStartLineNumber = -1,
360  const label ioEndLineNumber = -1
361  );
362 
363  //- Define basic print message for IO stream
364  // \return OSstream for further operations
365  OSstream& operator()
366  (
367  const char* functionName,
368  const char* sourceFileName,
369  const int sourceFileLineNumber,
370  const IOstream& ioStream
371  );
372 
373  //- Define basic print message for dictionary entries.
374  //- Uses the dictionary::relativeName() on output.
375  // \return OSstream for further operations
376  OSstream& operator()
377  (
378  const char* functionName,
379  const char* sourceFileName,
380  const int sourceFileLineNumber,
381  const dictionary& dict
382  );
383 
384 
385  //- Define basic print message for IO stream,
386  //- independent of a source-file reference.
387  //- Uses the dictionary::relativeName() on output.
388  // \return OSstream for further operations
389  OSstream& operator()
390  (
391  const std::string& where,
392  const IOstream& ioStream
393  );
394 
395  //- Define basic print message for dictionary entries,
396  //- independent of a source-file reference.
397  //- Uses the dictionary::relativeName() on output.
398  // \return OSstream for further operations
399  OSstream& operator()
400  (
401  const std::string& where,
402  const dictionary& dict
403  );
404 
405 
406  //- Print basic message and exit.
407  // Uses cerr if streams not yet constructed (at startup).
408  // Use in startup parsing instead of FatalError.
409  static void SafeFatalIOError
410  (
411  const char* functionName,
412  const char* sourceFileName,
413  const int sourceFileLineNumber,
414  const IOstream& ioStream,
415  const string& msg
416  );
417 
418 
419  // Other
420 
421  //- Extract a dictionary representation of the error information
422  operator dictionary() const;
424  //- Exit : can be called for any error to exit program
425  void exit(const int errNo = 1);
426 
427  //- Abort : used to stop code for fatal errors
428  void abort();
429 
430  //- Print error message
431  virtual void write(Ostream& os, const bool withTitle = true) const;
432 };
433 
434 
435 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
436 
437 //- Ostream operator
438 Ostream& operator<<(Ostream& os, const error& err);
439 
440 //- Ostream operator
441 Ostream& operator<<(Ostream& os, const IOerror& err);
442 
443 
444 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
445 // Global error declarations: defined in error.C
446 
447 //- Error stream (stdout output on all processes),
448 //- with additional 'FOAM FATAL ERROR' header text and stack trace.
449 extern error FatalError;
450 
451 //- Error stream (stdout output on all processes),
452 //- with additional 'FOAM FATAL IO ERROR' header text and stack trace.
453 extern IOerror FatalIOError;
454 
455 
456 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
457 
458 } // End namespace Foam
459 
460 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
461 // Convenience macros to add the file name and line number to the function name
462 
463 //- Report an error message using Foam::FatalError
464 // for functionName in file __FILE__ at line __LINE__
465 #define FatalErrorIn(functionName) \
466  ::Foam::FatalError((functionName), __FILE__, __LINE__)
467 
468 //- Report an error message using Foam::FatalError
469 // for FUNCTION_NAME in file __FILE__ at line __LINE__
470 #define FatalErrorInFunction FatalErrorIn(FUNCTION_NAME)
471 
472 //- Report an error message using Foam::FatalError
473 // about unknown lookup type in table
474 #define FatalErrorInLookup(lookupTag, lookupName, lookupTable) \
475  ::Foam::FatalError(FUNCTION_NAME, __FILE__, __LINE__) \
476  << "Unknown " << (lookupTag) << " type " << (lookupName) \
477  << "\n\nValid " << (lookupTag) << " types :\n" \
478  << ((lookupTable).sortedToc()) << '\n'
479 
480 
481 //- Report an error message using Foam::FatalIOError
482 // for functionName in file __FILE__ at line __LINE__
483 // for a particular IOstream
484 #define FatalIOErrorIn(functionName, ios) \
485  ::Foam::FatalIOError((functionName), __FILE__, __LINE__, (ios))
486 
487 //- Report an error message using Foam::FatalIOError
488 // for FUNCTION_NAME in file __FILE__ at line __LINE__
489 // for a particular IOstream
490 #define FatalIOErrorInFunction(ios) FatalIOErrorIn(FUNCTION_NAME, ios)
491 
492 
493 //- Report an error message using Foam::FatalIOError
494 // about unknown lookup type in table
495 #define FatalIOErrorInLookup(ios, lookupTag, lookupName, lookupTable) \
496  ::Foam::FatalIOError(FUNCTION_NAME, __FILE__, __LINE__, (ios)) \
497  << "Unknown " << (lookupTag) << " type " << (lookupName) \
498  << "\n\nValid " << (lookupTag) << " types :\n" \
499  << ((lookupTable).sortedToc()) << '\n'
500 
501 
502 //- Report an error message using Foam::FatalIOError
503 // (or cerr if FatalIOError not yet constructed)
504 // for functionName in file __FILE__ at line __LINE__
505 // for a particular IOstream
506 #define SafeFatalIOErrorIn(functionName, ios, msg) \
507  ::Foam::IOerror::SafeFatalIOError \
508  ((functionName), __FILE__, __LINE__, (ios), (msg))
509 
510 //- Report an error message using Foam::FatalIOError
511 // (or cerr if FatalIOError not yet constructed)
512 // for functionName in file __FILE__ at line __LINE__
513 // for a particular IOstream
514 #define SafeFatalIOErrorInFunction(ios, msg) \
515  SafeFatalIOErrorIn(FUNCTION_NAME, ios, msg)
516 
517 
518 //- Issue a FatalErrorIn for a function not currently implemented.
519 // The functionName is printed and then abort is called.
520 //
521 // This macro can be particularly useful when methods must be defined to
522 // complete the interface of a derived class even if they should never be
523 // called for this derived class.
524 #define notImplemented(functionName) \
525  FatalErrorIn(functionName) \
526  << "Not implemented" << ::Foam::abort(FatalError);
527 
528 //- Issue a FatalErrorIn for a function not currently implemented.
529 // The FUNCTION_NAME is printed and then abort is called.
530 //
531 // This macro can be particularly useful when methods must be defined to
532 // complete the interface of a derived class even if they should never be
533 // called for this derived class.
534 #define NotImplemented notImplemented(FUNCTION_NAME)
535 
536 
537 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
538 
539 #include "errorManip.H"
540 
541 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
542 
543 #endif
544 
545 // ************************************************************************* //
const string & title() const noexcept
The title of this error type.
Warn on errors/problems.
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:50
dictionary dict
IOerror(const string &title)
Construct from title string.
Definition: IOerror.C:34
void clear() const
Clear any messages.
Definition: error.C:337
std::unique_ptr< OStringStream > messageStreamPtr_
Definition: error.H:91
handlerTypes
Handling of errors. The exact handling depends on the local context.
Definition: error.H:109
void abort()
Abort : used to stop code for fatal errors.
Definition: IOerror.C:245
error FatalError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL ERROR&#39; header text and sta...
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
virtual ~IOerror() noexcept
Destructor.
Definition: IOerror.C:54
bool throwing() const noexcept
Return the current exception throwing state (on or off)
Definition: error.H:218
bool dontThrowExceptions() noexcept
Deactivate exception throwing.
Definition: error.H:356
virtual ~error() noexcept
Destructor.
Definition: error.C:157
string sourceFileName_
Definition: error.H:88
Handle output messages in a simple, consistent stream-based manner.
Definition: messageStream.H:70
Default behaviour (local meaning)
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: error.C:343
static bool warnAboutAge(const int version) noexcept
Test if an age warning should be emitted.
Definition: error.C:67
error(const string &title)
Construct from title string.
Definition: error.C:119
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Definition: error.H:70
label sourceFileLineNumber_
Definition: error.H:89
static void safePrintStack(std::ostream &os, int size=-1)
Helper function to print a stack, with optional upper limit. Used when OpenFOAM IO not yet initialise...
static bool useAbort()
True if FOAM_ABORT is on.
Definition: error.C:110
static bool master(const label communicator=-1)
Like Pstream::master but with a Pstream::parRun guard in case Pstream has not yet been initialised...
Definition: error.C:53
const string & functionName() const noexcept
The currently defined function name for output messages.
Definition: error.H:194
string message() const
The accumulated error message.
Definition: error.C:331
void abort()
Abort : used to stop code for fatal errors.
Definition: error.C:349
virtual void write(Ostream &os, const bool withTitle=true) const
Print error message.
Definition: error.C:355
Ignore on errors/problems.
label sourceFileLineNumber() const noexcept
The currently defined source-file line number for output messages.
Definition: error.H:210
bool throwExceptions(const bool on=true) noexcept
Specify exception throwing state (default is on)
Definition: error.H:346
Report an I/O error.
Definition: error.H:370
An Ostream is an abstract base class for all output systems (streams, files, token lists...
Definition: Ostream.H:56
const direction noexcept
Definition: Scalar.H:258
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:239
const string & ioFileName() const noexcept
The currently defined IO name for output messages.
Definition: error.H:415
label ioStartLineNumber() const noexcept
The currently defined IO start-line number for output messages.
Definition: error.H:423
OBJstream os(runTime.globalPath()/outputName)
OSstream & operator()()
Explicit convert to OSstream for << operations.
Definition: error.H:254
string functionName_
Definition: error.H:87
virtual void write(Ostream &os, const bool withTitle=true) const
Print error message.
Definition: IOerror.C:251
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
bool throwing_
Definition: error.H:90
const std::string version
OpenFOAM version (name or stringified number) as a std::string.
An IOstream is an abstract base class for all input/output systems; be they streams, files, token lists etc.
Definition: IOstream.H:82
Enum is a wrapper around a list of names/values that represent particular enumeration (or int) values...
Definition: error.H:64
OSstream & stream()
Return OSstream for output operations.
Definition: error.C:316
const string & sourceFileName() const noexcept
The currently defined source-file name for output messages.
Definition: error.H:202
label ioEndLineNumber() const noexcept
The currently defined IO end-line number.
Definition: error.H:431
static void printStack(Ostream &os, int size=-1)
Helper function to print a stack, with optional upper limit.
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:162
Fatal on errors/problems.
Output to string buffer, using a OSstream. Always UNCOMPRESSED.
Definition: StringStream.H:256
Namespace for OpenFOAM.
static const Enum< handlerTypes > handlerNames
Names of the error handler types.
Definition: error.H:120
void simpleExit(const int errNo, const bool isAbort)
Exit or abort, without throwing or job control handling.
Definition: error.C:265
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...