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