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 \*---------------------------------------------------------------------------*/
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 
38 Foam::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 
64 Foam::OSstream& Foam::IOerror::operator()
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 {
74  OSstream& os = error::operator()
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 
89 Foam::OSstream& Foam::IOerror::operator()
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 
109 Foam::OSstream& Foam::IOerror::operator()
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,
122  dict.relativeName(),
125  );
126 }
127 
128 
129 Foam::OSstream& Foam::IOerror::operator()
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 
147 Foam::OSstream& Foam::IOerror::operator()
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
158  dict.relativeName(),
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 
200 Foam::IOerror::operator Foam::dictionary() const
201 {
202  dictionary errDict(error::operator dictionary());
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 
215 void 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());
234  JobInfo::shutdown(isAbort || error::useAbort());
235  }
236 
237  simpleExit(errNo, isAbort);
238 }
239 
240 
241 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
242 
243 void Foam::IOerror::exit(const int)
244 {
245  exiting(1, false);
246 }
247 
248 
250 {
251  exiting(1, true);
252 }
253 
254 
255 void 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 
268  if (foamVersion::patched())
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 
327 Foam::IOerror Foam::FatalIOError("--> FOAM FATAL IO ERROR: ");
328 
329 
330 // ************************************************************************* //
Foam::IOerror::abort
void abort()
Abort : used to stop code for fatal errors.
Definition: IOerror.C:249
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:65
Foam::foamVersion::patched
bool patched()
Foam::IOerror::IOerror
IOerror(const string &title)
Construct from title string.
Definition: IOerror.C:38
Foam::messageStream::level
static int level
The output level (verbosity) of messages.
Definition: messageStream.H:112
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:79
Foam::IOstream::lineNumber
label lineNumber() const noexcept
Const access to the current stream line number.
Definition: IOstream.H:318
StringStream.H
Input/output from string buffers.
Foam::FatalIOError
IOerror FatalIOError
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:76
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &p)
Write boundaryPatch as dictionary entries (without surrounding braces)
Definition: boundaryPatch.C:83
Foam::IOerror::SafeFatalIOError
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
Foam::foamVersion::api
const int api
error.H
Foam::dictionary::relativeName
fileName relativeName(const bool caseTag=false) const
The dictionary name relative to the case.
Definition: dictionary.C:186
Foam::dictionary::startLineNumber
label startLineNumber() const
Return line number of first token in dictionary.
Definition: dictionary.C:205
Foam::dictionary::endLineNumber
label endLineNumber() const
Return line number of last token in dictionary.
Definition: dictionary.C:216
fileName.H
Foam::OSstream
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:54
Foam::IOerror::~IOerror
virtual ~IOerror() noexcept
Destructor.
Definition: IOerror.C:58
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::JobInfo::shutdown
static void shutdown()
Simple shutdown (finalize) of JobInfo.
Definition: JobInfo.C:98
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
Foam::IOstream::relativeName
fileName relativeName() const
Return the name of the stream relative to the current case.
Definition: IOstream.C:52
Pstream.H
Foam::IOstream::bad
bool bad() const noexcept
True if stream is corrupted.
Definition: IOstream.H:251
Foam::error::useAbort
static bool useAbort()
True if FOAM_ABORT is on.
Definition: error.C:98
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::jobInfo
JobInfo jobInfo
Definition: JobInfo.C:49
Foam::IOerror::exit
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:243
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::foamVersion::patch
const std::string patch
OpenFOAM patch number as a std::string.
Foam::JobInfo::constructed
static bool constructed
Global value for constructed job info.
Definition: JobInfo.H:101
Foam::IOerror::write
virtual void write(Ostream &os, const bool withTitle=true) const
Print error message.
Definition: IOerror.C:255
dictionary.H
JobInfo.H
Foam::PtrListOps::get
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
Foam::IOerror
Report an I/O error.
Definition: error.H:279
Foam::dictionary::add
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:640
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:56
Foam::error
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Definition: error.H:73