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