IOerror.C
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-2016 OpenFOAM Foundation
9 Copyright (C) 2015-2021 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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\*---------------------------------------------------------------------------*/
28
29#include "error.H"
30#include "StringStream.H"
31#include "fileName.H"
32#include "dictionary.H"
33#include "JobInfo.H"
34#include "Pstream.H"
35
36// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
37
38Foam::IOerror::IOerror(const string& title)
39:
40 error(title),
41 ioFileName_("unknown"),
42 ioStartLineNumber_(-1),
43 ioEndLineNumber_(-1)
44{}
45
46
48:
49 error(errDict),
50 ioFileName_(errDict.get<string>("ioFileName")),
51 ioStartLineNumber_(errDict.get<label>("ioStartLineNumber")),
52 ioEndLineNumber_(errDict.get<label>("ioEndLineNumber"))
53{}
54
55
56// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * //
57
59{}
60
61
62// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
63
65(
66 const char* functionName,
67 const char* sourceFileName,
68 const int sourceFileLineNumber,
69 const string& ioFileName,
70 const label ioStartLineNumber,
71 const label ioEndLineNumber
72)
73{
75 (
76 functionName,
77 sourceFileName,
78 sourceFileLineNumber
79 );
80
81 ioFileName_ = ioFileName;
82 ioStartLineNumber_ = ioStartLineNumber;
83 ioEndLineNumber_ = ioEndLineNumber;
84
85 return os;
86}
87
88
90(
91 const char* functionName,
92 const char* sourceFileName,
93 const int sourceFileLineNumber,
94 const IOstream& ioStream
95)
96{
97 return operator()
98 (
99 functionName,
100 sourceFileName,
101 sourceFileLineNumber,
102 ioStream.relativeName(),
103 ioStream.lineNumber(),
104 -1 // No known endLineNumber
105 );
106}
107
108
110(
111 const char* functionName,
112 const char* sourceFileName,
113 const int sourceFileLineNumber,
114 const dictionary& dict
115)
116{
117 return operator()
118 (
119 functionName,
120 sourceFileName,
121 sourceFileLineNumber,
125 );
126}
127
128
130(
131 const std::string& where,
132 const IOstream& ioStream
133)
134{
135 return operator()
136 (
137 where.c_str(),
138 "", // No source file
139 -1, // Non-zero to ensure 'where' is reported
140 ioStream.relativeName(),
141 ioStream.lineNumber(),
142 -1 // No known endLineNumber
143 );
144}
145
146
148(
149 const std::string& where,
150 const dictionary& dict
151)
152{
153 return operator()
154 (
155 where.c_str(),
156 "", // No source file
157 -1, // Non-zero to ensure 'where' is reported
161 );
162}
163
164
166(
167 const char* functionName,
168 const char* sourceFileName,
169 const int sourceFileLineNumber,
170 const IOstream& ioStream,
171 const string& msg
172)
173{
175 {
177 (
178 functionName,
179 sourceFileName,
180 sourceFileLineNumber,
181 ioStream
182 ) << msg << Foam::exit(FatalIOError);
183 }
184 else
185 {
186 std::cerr
187 << nl
188 << "--> FOAM FATAL IO ERROR:" << nl
189 << msg << nl
190 << "file: " << ioStream.relativeName()
191 << " at line " << ioStream.lineNumber() << '.' << nl << nl
192 << " From " << functionName << nl
193 << " in file " << sourceFileName
194 << " at line " << sourceFileLineNumber << '.' << std::endl;
195 std::exit(1);
196 }
197}
198
199
201{
203
204 errDict.add("type", word("Foam::IOerror"), true); // overwrite
205 errDict.add("ioFileName", ioFileName());
206 errDict.add("ioStartLineNumber", ioStartLineNumber());
207 errDict.add("ioEndLineNumber", ioEndLineNumber());
208
209 return errDict;
210}
211
212
213// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
214
215void Foam::IOerror::exiting(const int errNo, const bool isAbort)
216{
217 if (throwing_)
218 {
219 if (!isAbort)
220 {
221 // Make a copy of the error to throw
222 IOerror errorException(*this);
223
224 // Reset the message buffer for the next error message
225 messageStreamPtr_->reset();
226
227 throw errorException;
228 return;
229 }
230 }
231 else if (JobInfo::constructed)
232 {
233 jobInfo.add("FatalIOError", operator dictionary());
235 }
236
237 simpleExit(errNo, isAbort);
238}
239
240
241// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
242
243void Foam::IOerror::exit(const int)
244{
245 exiting(1, false);
246}
247
248
250{
251 exiting(1, true);
252}
253
254
255void Foam::IOerror::write(Ostream& os, const bool withTitle) const
256{
257 if (os.bad())
258 {
259 return;
260 }
261
262 os << nl;
263 if (withTitle && !title().empty())
264 {
265 os << title().c_str()
266 << "(openfoam-" << foamVersion::api;
267
269 {
270 // Patch-level, when defined
271 os << " patch=" << foamVersion::patch.c_str();
272 }
273 os << ')' << nl;
274 }
275 os << message().c_str();
276
277
278 if (!ioFileName().empty())
279 {
280 os << nl << nl
281 << "file: " << ioFileName().c_str();
282
283 if (ioStartLineNumber() >= 0)
284 {
285 os << " at line " << ioStartLineNumber();
286 if (ioStartLineNumber() < ioEndLineNumber())
287 {
288 os << " to " << ioEndLineNumber();
289 }
290 os << '.';
291 }
292 }
293
294
295 const label lineNo = sourceFileLineNumber();
296
297 if (IOerror::level >= 2 && lineNo && !functionName().empty())
298 {
299 os << nl << nl
300 << " From " << functionName().c_str() << nl;
301
302 if (!sourceFileName().empty())
303 {
304 os << " in file " << sourceFileName().c_str();
305
306 if (lineNo > 0)
307 {
308 os << " at line " << lineNo << '.';
309 }
310 }
311 }
312}
313
314
315// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
316
318{
319 err.write(os);
320 return os;
321}
322
323
324// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
325// Global error definitions
326
327Foam::IOerror Foam::FatalIOError("--> FOAM FATAL IO ERROR: ");
328
329
330// ************************************************************************* //
Input/output from string buffers.
Report an I/O error.
Definition: error.H:282
virtual ~IOerror() noexcept
Destructor.
Definition: IOerror.C:58
virtual void write(Ostream &os, const bool withTitle=true) const
Print error message.
Definition: IOerror.C:255
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:166
void abort()
Abort : used to stop code for fatal errors.
Definition: IOerror.C:249
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:82
label lineNumber() const noexcept
Const access to the current stream line number.
Definition: IOstream.H:318
bool bad() const noexcept
True if stream is corrupted.
Definition: IOstream.H:251
fileName relativeName() const
Return the name of the stream relative to the current case.
Definition: IOstream.C:52
void exit()
Job end with "exit" termination.
Definition: JobInfo.C:234
static void shutdown()
Simple shutdown (finalize) of JobInfo.
Definition: JobInfo.C:98
static bool constructed
Global value for constructed job info.
Definition: JobInfo.H:101
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:57
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:62
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
fileName relativeName(const bool caseTag=false) const
The dictionary name relative to the case.
Definition: dictionary.C:186
label endLineNumber() const
Return line number of last token in dictionary.
Definition: dictionary.C:216
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:640
label startLineNumber() const
Return line number of first token in dictionary.
Definition: dictionary.C:205
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Definition: error.H:77
static bool useAbort()
True if FOAM_ABORT is on.
Definition: error.C:98
friend Ostream & operator(Ostream &, const faMatrix< Type > &)
virtual bool write()
Write the output fields.
static int level
The output level (verbosity) of messages.
A class for handling character strings derived from std::string.
Definition: string.H:79
A class for handling words, derived from Foam::string.
Definition: word.H:68
OBJstream os(runTime.globalPath()/outputName)
const std::string patch
OpenFOAM patch number as a std::string.
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
IOerror FatalIOError
const direction noexcept
Definition: Scalar.H:223
JobInfo jobInfo
Definition: JobInfo.C:49
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict