error.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2021 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::error
26 
27 Description
28  Class to handle errors and exceptions in a simple, consistent stream-based
29  manner.
30 
31  The error class is globally instantiated with a title string. Errors,
32  messages and other data are piped to the messageStream class in the
33  standard manner. Manipulators are supplied for exit and abort which may
34  terminate the program or throw an exception depending on whether the
35  exception handling has been switched on (off by default).
36 
37 Usage
38  \code
39  error << "message1" << "message2" << FoamDataType << exit(errNo);
40  error << "message1" << "message2" << FoamDataType << abort();
41  \endcode
42 
43 SourceFiles
44  error.C
45 
46 \*---------------------------------------------------------------------------*/
47 
48 #ifndef error_H
49 #define error_H
50 
51 #include "OStringStream.H"
52 #include "messageStream.H"
53 
54 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
55 
56 namespace Foam
57 {
58 
59 // Forward declaration of friend functions and operators
60 class error;
61 Ostream& operator<<(Ostream&, const error&);
62 
63 
64 /*---------------------------------------------------------------------------*\
65  Class error Declaration
66 \*---------------------------------------------------------------------------*/
67 
68 class error
69 :
70  public std::exception,
71  public messageStream
72 {
73 
74 protected:
75 
76  // Protected data
77 
78  string functionName_;
81 
82  bool abort_;
83 
84  bool throwExceptions_;
85 
87 
88 
89 public:
90 
91  // Constructors
92 
93  //- Construct from title string
94  error(const string& title);
95 
96 
97  // Member Functions
98 
99  string message() const;
100 
101  const string& functionName() const
102  {
103  return functionName_;
104  }
105 
106  const string& sourceFileName() const
107  {
108  return sourceFileName_;
109  }
110 
112  {
113  return sourceFileLineNumber_;
114  }
115 
116  void throwExceptions()
117  {
118  throwExceptions_ = true;
119  }
120 
121  void dontThrowExceptions()
122  {
123  throwExceptions_ = false;
124  }
125 
126  //- Convert to OSstream
127  // Prints basic message and returns OSstream for further info.
128  OSstream& operator()
129  (
130  const char* functionName,
131  const char* sourceFileName,
132  const int sourceFileLineNumber = 0
133  );
134 
135  //- Convert to OSstream
136  // Prints basic message and returns OSstream for further info.
137  OSstream& operator()
138  (
139  const string& functionName,
140  const char* sourceFileName,
141  const int sourceFileLineNumber = 0
142  );
143 
144  //- Explicitly convert to OSstream for << operations
145  OSstream& operator()();
146 
147  //- Convert to OSstream for << operations
148  operator OSstream&()
149  {
150  return this->operator()();
151  }
152 
153  //- Create and return a dictionary
154  operator dictionary() const;
155 
156  //- Helper function to print a stack (if OpenFOAM IO not yet
157  // initialised)
158  static void safePrintStack(std::ostream&);
159 
160  //- Helper function to print a stack
161  static void printStack(Ostream&);
162 
163  //- Exit : can be called for any error to exit program.
164  // Prints stack before exiting.
165  void exit(const int errNo = 1);
166 
167  //- Abort : used to stop code for fatal errors.
168  // Prints stack before exiting.
169  void abort();
170 
171 
172  // Ostream operator
173 
174  friend Ostream& operator<<(Ostream&, const error&);
175 };
176 
177 
178 // Forward declaration of friend functions and operators
179 class IOerror;
180 Ostream& operator<<(Ostream&, const IOerror&);
181 
182 
183 /*---------------------------------------------------------------------------*\
184  Class IOerror Declaration
185 \*---------------------------------------------------------------------------*/
186 
187 //- Report an I/O error
188 class IOerror
189 :
190  public error
191 {
192  // Private Data
193 
194  string ioFileName_;
195  label ioStartLineNumber_;
196  label ioEndLineNumber_;
197 
198 
199 public:
200 
201  // Constructors
202 
203  //- Construct from title string
204  IOerror(const string& title);
205 
206 
207  // Member Functions
208 
209  const string& ioFileName() const
210  {
211  return ioFileName_;
212  }
213 
214  label ioStartLineNumber() const
215  {
216  return ioStartLineNumber_;
217  }
218 
219  label ioEndLineNumber() const
220  {
221  return ioEndLineNumber_;
222  }
223 
224  //- Convert to OSstream
225  // Prints basic message and returns OSstream for further info.
226  OSstream& operator()
227  (
228  const char* functionName,
229  const char* sourceFileName,
230  const int sourceFileLineNumber,
231  const string& ioFileName,
232  const label ioStartLineNumber = -1,
233  const label ioEndLineNumber = -1
234  );
235 
236  //- Convert to OSstream
237  // Prints basic message and returns OSstream for further info.
238  OSstream& operator()
239  (
240  const char* functionName,
241  const char* sourceFileName,
242  const int sourceFileLineNumber,
243  const IOstream&
244  );
245 
246  //- Convert to OSstream
247  // Prints basic message and returns OSstream for further info.
248  OSstream& operator()
249  (
250  const char* functionName,
251  const char* sourceFileName,
252  const int sourceFileLineNumber,
253  const dictionary&
254  );
255 
256  //- Print basic message and exit. Uses cerr if streams not constructed
257  // yet (at startup). Use in startup parsing instead of FatalError.
258  static void SafeFatalIOError
259  (
260  const char* functionName,
261  const char* sourceFileName,
262  const int sourceFileLineNumber,
263  const IOstream&,
264  const string& msg
265  );
266 
267  //- Create and return a dictionary
268  operator dictionary() const;
269 
270  //- Exit : can be called for any error to exit program
271  void exit(const int errNo = 1);
272 
273  //- Abort : used to stop code for fatal errors
274  void abort();
275 
276 
277  // Ostream operator
278 
279  friend Ostream& operator<<(Ostream&, const IOerror&);
280 };
281 
282 
283 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
284 // Global error declarations: defined in error.C
285 
286 extern error FatalError;
287 extern IOerror FatalIOError;
288 
289 // Template argument dependent "false" for static_assert in templated functions
290 template <class... T>
291 constexpr bool False = false;
292 
293 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
294 
295 } // End namespace Foam
296 
297 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
298 // Convenience macros to add the file name and line number to the function name
299 
300 //- Report an error message using Foam::FatalError
301 // for functionName in file __FILE__ at line __LINE__
302 #define FatalErrorIn(functionName) \
303  ::Foam::FatalError((functionName), __FILE__, __LINE__)
304 
305 //- Report an error message using Foam::FatalError
306 // for FUNCTION_NAME in file __FILE__ at line __LINE__
307 #define FatalErrorInFunction FatalErrorIn(FUNCTION_NAME)
308 
309 
310 //- Report an error message using Foam::FatalIOError
311 // for functionName in file __FILE__ at line __LINE__
312 // for a particular IOstream
313 #define FatalIOErrorIn(functionName, ios) \
314  ::Foam::FatalIOError((functionName), __FILE__, __LINE__, (ios))
315 
316 //- Report an error message using Foam::FatalIOError
317 // for FUNCTION_NAME in file __FILE__ at line __LINE__
318 // for a particular IOstream
319 #define FatalIOErrorInFunction(ios) FatalIOErrorIn(FUNCTION_NAME, ios)
320 
321 
322 //- Report an error message using Foam::FatalIOError
323 // (or cerr if FatalIOError not yet constructed)
324 // for functionName in file __FILE__ at line __LINE__
325 // for a particular IOstream
326 #define SafeFatalIOErrorIn(functionName, ios, msg) \
327  ::Foam::IOerror::SafeFatalIOError \
328  ((functionName), __FILE__, __LINE__, (ios), (msg))
329 
330 //- Report an error message using Foam::FatalIOError
331 // (or cerr if FatalIOError not yet constructed)
332 // for functionName in file __FILE__ at line __LINE__
333 // for a particular IOstream
334 #define SafeFatalIOErrorInFunction(ios, msg) \
335  SafeFatalIOErrorIn(FUNCTION_NAME, ios, msg)
336 
337 
338 //- Issue a FatalErrorIn for a function not currently implemented.
339 // The functionName is printed and then abort is called.
340 //
341 // This macro can be particularly useful when methods must be defined to
342 // complete the interface of a derived class even if they should never be
343 // called for this derived class.
344 #define notImplemented(functionName) \
345  FatalErrorIn(functionName) \
346  << "Not implemented" << ::Foam::abort(::Foam::FatalError);
347 
348 //- Issue a FatalErrorIn for a function not currently implemented.
349 // The FUNCTION_NAME is printed and then abort is called.
350 //
351 // This macro can be particularly useful when methods must be defined to
352 // complete the interface of a derived class even if they should never be
353 // called for this derived class.
354 #define NotImplemented notImplemented(FUNCTION_NAME)
355 
356 
357 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
358 
359 #include "errorManip.H"
360 
361 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
362 
363 #endif
364 
365 // ************************************************************************* //
Report an I/O error.
Definition: error.H:190
const string & ioFileName() const
Definition: error.H:208
static void SafeFatalIOError(const char *functionName, const char *sourceFileName, const int sourceFileLineNumber, const IOstream &, const string &msg)
Print basic message and exit. Uses cerr if streams not constructed.
Definition: IOerror.C:105
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:158
label ioEndLineNumber() const
Definition: error.H:218
IOerror(const string &title)
Construct from title string.
Definition: IOerror.C:35
label ioStartLineNumber() const
Definition: error.H:213
void abort()
Abort : used to stop code for fatal errors.
Definition: IOerror.C:199
friend Ostream & operator<<(Ostream &, const IOerror &)
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:72
Generic output stream.
Definition: OSstream.H:54
Output to memory buffer stream.
Definition: OStringStream.H:52
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:160
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Definition: error.H:71
string functionName_
Definition: error.H:77
bool abort_
Definition: error.H:81
const string & sourceFileName() const
Definition: error.H:105
label sourceFileLineNumber_
Definition: error.H:79
friend Ostream & operator<<(Ostream &, const error &)
OStringStream messageStream_
Definition: error.H:85
const string & functionName() const
Definition: error.H:100
string message() const
Definition: error.C:119
static void printStack(Ostream &)
Helper function to print a stack.
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: error.C:125
void throwExceptions()
Definition: error.H:115
error(const string &title)
Construct from title string.
Definition: error.C:36
OSstream & operator()()
Explicitly convert to OSstream for << operations.
Definition: error.C:88
static void safePrintStack(std::ostream &)
Helper function to print a stack (if OpenFOAM IO not yet.
Definition: printStack.C:192
string sourceFileName_
Definition: error.H:78
void abort()
Abort : used to stop code for fatal errors.
Definition: error.C:166
bool throwExceptions_
Definition: error.H:83
void dontThrowExceptions()
Definition: error.H:120
label sourceFileLineNumber() const
Definition: error.H:110
A functionName is a word starting with '#'.
Definition: functionName.H:60
Class to handle messaging in a simple, consistent stream-based manner.
Definition: messageStream.H:69
const string & title() const
Return the title of this error type.
Namespace for OpenFOAM.
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:59
constexpr bool False
Definition: error.H:290
IOerror FatalIOError
error FatalError
Ostream & operator<<(Ostream &, const ensightPart &)
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)