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 "StringStream.H"
34 #include "fileName.H"
35 #include "dictionary.H"
36 #include "JobInfo.H"
37 #include "Pstream.H"
38 
39 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
40 
41 Foam::IOerror::IOerror(const string& 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 string& msg
175 )
176 {
178  {
180  (
181  functionName,
182  sourceFileName,
183  sourceFileLineNumber,
184  ioStream
185  ) << msg << Foam::exit(FatalIOError);
186  }
187  else
188  {
189  std::cerr
190  << nl
191  << "--> FOAM FATAL IO ERROR:" << nl
192  << msg << nl
193  << "file: " << ioStream.relativeName()
194  << " at line " << ioStream.lineNumber() << '.' << nl << nl
195  << " From " << functionName << nl
196  << " in file " << sourceFileName
197  << " at line " << sourceFileLineNumber << '.' << std::endl;
198  std::exit(1);
199  }
200 }
201 
202 
203 Foam::IOerror::operator Foam::dictionary() const
204 {
205  dictionary errDict(error::operator dictionary());
206 
207  errDict.add("type", word("Foam::IOerror"), true); // overwrite
208  errDict.add("ioFileName", ioFileName());
209  errDict.add("ioStartLineNumber", ioStartLineNumber());
210  errDict.add("ioEndLineNumber", ioEndLineNumber());
211 
212  return errDict;
213 }
214 
215 
216 // * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
217 
218 void Foam::IOerror::exiting(const int errNo, const bool isAbort)
219 {
220  if (throwing_)
221  {
222  if (!isAbort)
223  {
224  // Make a copy of the error to throw
225  IOerror errorException(*this);
226 
227  // Reset the message buffer for the next error message
228  messageStreamPtr_->reset();
229 
230  throw errorException;
231  return;
232  }
233  }
234  else if (JobInfo::constructed)
235  {
236  jobInfo.add("FatalIOError", operator dictionary());
237  JobInfo::shutdown(isAbort || error::useAbort());
238  }
240  simpleExit(errNo, isAbort);
241 }
242 
243 
244 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
246 void Foam::IOerror::exit(const int)
247 {
248  exiting(1, false);
249 }
250 
253 {
254  exiting(1, true);
255 }
256 
257 
258 void Foam::IOerror::write(Ostream& os, const bool withTitle) const
259 {
260  if (os.bad())
261  {
262  return;
263  }
264 
265  os << nl;
266  if (withTitle && !title().empty())
267  {
268  os << title().c_str()
269  << "(openfoam-" << foamVersion::api;
270 
271  if (foamVersion::patched())
272  {
273  // Patch-level, when defined
274  os << " patch=" << foamVersion::patch.c_str();
275  }
276  os << ')' << nl;
277  }
278  os << message().c_str();
279 
280 
281  if (!ioFileName().empty())
282  {
283  os << nl << nl
284  << "file: " << ioFileName().c_str();
285 
286  if (ioStartLineNumber() >= 0)
287  {
288  os << " at line " << ioStartLineNumber();
289  if (ioStartLineNumber() < ioEndLineNumber())
290  {
291  os << " to " << ioEndLineNumber();
292  }
293  os << '.';
294  }
295  }
296 
297 
298  const label lineNo = sourceFileLineNumber();
299 
300  if (IOerror::level >= 2 && lineNo && !functionName().empty())
301  {
302  os << nl << nl
303  << " From " << functionName().c_str() << nl;
304 
305  if (!sourceFileName().empty())
306  {
307  os << " in file " << sourceFileName().c_str();
308 
309  if (lineNo > 0)
310  {
311  os << " at line " << lineNo << '.';
312  }
313  }
314  }
315 }
316 
317 
318 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
319 
320 Foam::Ostream& Foam::operator<<(Ostream& os, const IOerror& err)
321 {
322  err.write(os);
323  return os;
324 }
325 
326 
327 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
328 // Global error definitions
329 
330 Foam::IOerror Foam::FatalIOError("--> FOAM FATAL IO ERROR: ");
331 
332 
333 // ************************************************************************* //
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
List of values generated by applying the access operation to each list item.
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
IOerror(const string &title)
Construct from title string.
Definition: IOerror.C:34
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:245
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:370
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:239
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:251
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
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:162
A class for handling character strings derived from std::string.
Definition: string.H:72
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 ...