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-2019 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  error::operator()(functionName, sourceFileName, sourceFileLineNumber);
75  ioFileName_ = ioFileName;
76  ioStartLineNumber_ = ioStartLineNumber;
77  ioEndLineNumber_ = ioEndLineNumber;
78 
79  return operator OSstream&();
80 }
81 
82 
83 Foam::OSstream& Foam::IOerror::operator()
84 (
85  const char* functionName,
86  const char* sourceFileName,
87  const int sourceFileLineNumber,
88  const IOstream& ioStream
89 )
90 {
91  return operator()
92  (
93  functionName,
94  sourceFileName,
95  sourceFileLineNumber,
96  ioStream.name(),
97  ioStream.lineNumber(),
98  -1
99  );
100 }
101 
102 
103 Foam::OSstream& Foam::IOerror::operator()
104 (
105  const char* functionName,
106  const char* sourceFileName,
107  const int sourceFileLineNumber,
108  const dictionary& dict
109 )
110 {
111  return operator()
112  (
113  functionName,
114  sourceFileName,
115  sourceFileLineNumber,
116  dict.name(),
119  );
120 }
121 
122 
124 (
125  const char* functionName,
126  const char* sourceFileName,
127  const int sourceFileLineNumber,
128  const IOstream& ioStream,
129  const string& msg
130 )
131 {
133  {
135  (
136  functionName,
137  sourceFileName,
138  sourceFileLineNumber,
139  ioStream
140  ) << msg << Foam::exit(FatalIOError);
141  }
142  else
143  {
144  std::cerr
145  << nl
146  << "--> FOAM FATAL IO ERROR:" << nl
147  << msg << nl
148  << "file: " << ioStream.name()
149  << " at line " << ioStream.lineNumber() << '.' << nl << nl
150  << " From function " << functionName << nl
151  << " in file " << sourceFileName
152  << " at line " << sourceFileLineNumber << '.' << std::endl;
153  std::exit(1);
154  }
155 }
156 
157 
158 Foam::IOerror::operator Foam::dictionary() const
159 {
160  dictionary errDict(error::operator dictionary());
161 
162  errDict.remove("type");
163  errDict.add("type", word("Foam::IOerror"));
164 
165  errDict.add("ioFileName", ioFileName());
166  errDict.add("ioStartLineNumber", ioStartLineNumber());
167  errDict.add("ioEndLineNumber", ioEndLineNumber());
168 
169  return errDict;
170 }
171 
172 
173 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
174 
175 void Foam::IOerror::exit(const int)
176 {
177  if (!throwExceptions_ && JobInfo::constructed)
178  {
179  jobInfo.add("FatalIOError", operator dictionary());
180  jobInfo.exit();
181  }
182 
183  if (env("FOAM_ABORT"))
184  {
185  abort();
186  }
187  else if (throwExceptions_)
188  {
189  // Make a copy of the error to throw
190  IOerror errorException(*this);
191 
192  // Reset the message buffer for the next error message
193  messageStreamPtr_->reset();
194 
195  throw errorException;
196  }
197  else if (Pstream::parRun())
198  {
199  Perr<< nl << *this << nl
200  << "\nFOAM parallel run exiting\n" << endl;
201  Pstream::exit(1);
202  }
203  else
204  {
205  Perr<< nl << *this << nl
206  << "\nFOAM exiting\n" << endl;
207  std::exit(1);
208  }
209 }
210 
211 
213 {
214  if (!throwExceptions_ && JobInfo::constructed)
215  {
216  jobInfo.add("FatalIOError", operator dictionary());
217  jobInfo.abort();
218  }
219 
220  if (env("FOAM_ABORT"))
221  {
222  Perr<< nl << *this << nl
223  << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
224  printStack(Perr);
225  std::abort();
226  }
227  else if (throwExceptions_)
228  {
229  // Make a copy of the error to throw
230  IOerror errorException(*this);
231 
232  // Reset the message buffer for the next error message
233  messageStreamPtr_->reset();
234 
235  throw errorException;
236  }
237  else if (Pstream::parRun())
238  {
239  Perr<< nl << *this << nl
240  << "\nFOAM parallel run aborting\n" << endl;
241  printStack(Perr);
242  Pstream::abort();
243  }
244  else
245  {
246  Perr<< nl << *this << nl
247  << "\nFOAM aborting\n" << endl;
248  printStack(Perr);
249 
250  #ifdef _WIN32
251  std::exit(1); // Prefer exit() to avoid unnecessary warnings
252  #else
253  std::abort();
254  #endif
255  }
256 }
257 
258 
259 void Foam::IOerror::write(Ostream& os, const bool includeTitle) const
260 {
261  if (!os.bad())
262  {
263  os << nl;
264  if (includeTitle && !title().empty())
265  {
266  os << title().c_str() << nl;
267  }
268 
269  os << message().c_str() << nl << nl;
270 
271  const bool hasFile = !ioFileName().empty();
272 
273  if (hasFile)
274  {
275  os << "file: " << ioFileName().c_str();
276 
277  if (ioStartLineNumber() >= 0)
278  {
279  if (ioStartLineNumber() < ioEndLineNumber())
280  {
281  os << " from line " << ioStartLineNumber()
282  << " to line " << ioEndLineNumber() << '.';
283  }
284  else
285  {
286  os << " at line " << ioStartLineNumber() << '.';
287  }
288  }
289  }
290 
291  if (IOerror::level >= 2 && sourceFileLineNumber())
292  {
293  if (hasFile)
294  {
295  os << nl << nl;
296  }
297 
298  os << " From function " << functionName().c_str() << nl
299  << " in file " << sourceFileName().c_str()
300  << " at line " << sourceFileLineNumber() << '.';
301  }
302  }
303 }
304 
305 
306 // * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
307 
309 {
310  err.write(os);
311 
312  return os;
313 }
314 
315 
316 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
317 // Global error definitions
318 
319 Foam::IOerror Foam::FatalIOError("--> FOAM FATAL IO ERROR: ");
320 
321 
322 // ************************************************************************* //
Foam::JobInfo::exit
void exit()
End with "termination=exit".
Definition: JobInfo.C:172
Foam::IOerror::abort
void abort()
Abort : used to stop code for fatal errors.
Definition: IOerror.C:212
Foam::word
A class for handling words, derived from Foam::string.
Definition: word.H:62
Foam::UPstream::exit
static void exit(int errnum=1)
Exit program.
Definition: UPstream.C:59
Foam::IOerror::IOerror
IOerror(const string &title)
Construct from title string.
Definition: IOerror.C:38
Foam::UPstream::parRun
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:414
Foam::messageStream::level
static int level
Controls the output verbosity of messageStream.
Definition: messageStream.H:108
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:75
StringStream.H
Input/output from string buffers.
Foam::FatalIOError
IOerror FatalIOError
Foam::UPstream::abort
static void abort()
Abort program.
Definition: UPstream.C:66
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:337
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:73
Foam::env
bool env(const std::string &envName)
True if environment variable of given name is defined.
Definition: MSwindows.C:363
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:124
Foam::dictionary::name
const fileName & name() const
The dictionary name.
Definition: dictionary.H:446
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
Foam::IOerror::~IOerror
virtual ~IOerror()
Destructor.
Definition: IOerror.C:58
error.H
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::IOstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.C:39
Foam::OSstream
Generic output stream.
Definition: OSstream.H:54
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam::IOstream::bad
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:234
Pstream.H
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:137
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:130
Foam::error::operator()
OSstream & operator()()
Explicitly convert to OSstream for << operations.
Definition: error.H:184
Foam::jobInfo
JobInfo jobInfo
Definition: JobInfo.C:49
Foam::Perr
prefixOSstream Perr
An Ostream wrapper for parallel output to std::cerr.
Foam::IOerror::exit
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:175
Foam::nl
constexpr char nl
Definition: Ostream.H:372
Foam::JobInfo::constructed
static bool constructed
Global value for constructed job info.
Definition: JobInfo.H:80
Foam::IOerror::write
void write(Ostream &os, const bool includeTitle=true) const
Print error message.
Definition: IOerror.C:259
dictionary.H
JobInfo.H
Foam::IOstream::lineNumber
label lineNumber() const
Const access to the current stream line number.
Definition: IOstream.H:301
Foam::IOerror
Report an I/O error.
Definition: error.H:218
Foam::dictionary::add
entry * add(entry *entryPtr, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:703
Foam::JobInfo::abort
void abort()
End with "termination=abort".
Definition: JobInfo.C:178
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:64
Foam::operator<<
Ostream & operator<<(Ostream &, const boundaryPatch &)
Definition: boundaryPatch.C:102