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 -------------------------------------------------------------------------------
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 Note
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 
41 Foam::IOerror::IOerror(const char* title)
42 :
43  error(title),
44  ioFileName_("unknown"),
45  ioStartLineNumber_(-1),
46  ioEndLineNumber_(-1)
47 {}
48 
49 
50 Foam::IOerror::IOerror(const dictionary& errDict)
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 * * * * * * * * * * * * * * * //
60 
62 {}
63 
64 
65 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
66 
67 Foam::OSstream& Foam::IOerror::operator()
68 (
69  const char* functionName,
70  const char* sourceFileName,
71  const int sourceFileLineNumber,
72  const 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_ = ioFileName;
85  ioStartLineNumber_ = ioStartLineNumber;
86  ioEndLineNumber_ = ioEndLineNumber;
87 
88  return os;
89 }
90 
91 
92 Foam::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 
112 Foam::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(),
128  );
129 }
130 
131 
132 Foam::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 
150 Foam::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(),
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 
204 Foam::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 
219 void 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());
238  JobInfo::shutdown(isAbort || error::useAbort());
239  }
241  simpleExit(errNo, isAbort);
242 }
243 
244 
245 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
247 void Foam::IOerror::exit(const int)
248 {
249  exiting(1, false);
250 }
251 
254 {
255  exiting(1, true);
256 }
257 
258 
259 void 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 
272  if (foamVersion::patched())
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 label 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  }
314  }
315  }
316 }
317 
318 
319 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
320 
321 Foam::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 
331 Foam::IOerror Foam::FatalIOError("--> FOAM FATAL IO ERROR: ");
332 
333 
334 // ************************************************************************* //
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
List of values generated by applying the access operation to each list item.
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
fileName relativeName() const
Return the name of the stream relative to the current case.
Definition: IOstream.C:39
void clear() const
Clear any accumulated error messages.
Definition: error.C:350
label endLineNumber() const
Return line number of last token in dictionary.
Definition: dictionary.C:209
bool bad() const noexcept
True if stream is corrupted.
Definition: IOstream.H:305
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:125
void abort()
Abort : used to stop code for fatal errors.
Definition: IOerror.C:246
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:129
Input/output from string buffers.
virtual ~IOerror() noexcept
Destructor.
Definition: IOerror.C:54
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
static void shutdown()
Simple shutdown (finalize) of JobInfo.
Definition: JobInfo.C:94
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
bool patched()
Test if the patch string appears to be in use, which is when it is defined (non-zero).
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:625
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Definition: error.H:70
static bool useAbort()
True if FOAM_ABORT is on.
Definition: error.C:110
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
Report an I/O error.
Definition: error.H:375
const int api
OpenFOAM api number (integer) corresponding to the value of OPENFOAM at the time of compilation...
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
OBJstream os(runTime.globalPath()/outputName)
fileName relativeName(const bool caseTag=false) const
The dictionary name relative to the case.
Definition: dictionary.C:179
virtual void write(Ostream &os, const bool withTitle=true) const
Print error message.
Definition: IOerror.C:252
label lineNumber() const noexcept
Const access to the current stream line number.
Definition: IOstream.H:390
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:77
An IOstream is an abstract base class for all input/output systems; be they streams, files, token lists etc.
Definition: IOstream.H:82
const std::string patch
OpenFOAM patch number as a std::string.
JobInfo jobInfo
Definition: JobInfo.C:45
A class for handling character strings derived from std::string.
Definition: string.H:72
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
static int level
The output level (verbosity) of messages.
label startLineNumber() const
Return line number of first token in dictionary.
Definition: dictionary.C:198
static bool constructed
Global value for constructed job info.
Definition: JobInfo.H:115
IOerror FatalIOError
Error stream (stdout output on all processes), with additional &#39;FOAM FATAL IO ERROR&#39; header text and ...