IOerror.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2025 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "error.H"
27 #include "OStringStream.H"
28 #include "fileName.H"
29 #include "dictionary.H"
30 #include "jobInfo.H"
31 #include "Pstream.H"
32 
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 
36 :
37  ioFileName_("unknown"),
38  ioStartLineNumber_(-1),
39  ioEndLineNumber_(-1),
40  ioGlobal_(false)
41 {}
42 
43 
45 (
46  const string& ioFileName,
47  const label ioStartLineNumber,
48  const label ioEndLineNumber,
49  const bool ioGlobal
50 )
51 :
52  ioFileName_(ioFileName),
53  ioStartLineNumber_(ioStartLineNumber),
54  ioEndLineNumber_(ioEndLineNumber),
55  ioGlobal_(ioGlobal)
56 {}
57 
58 
60 :
61  ioFileName_(ios.name()),
62  ioStartLineNumber_(ios.lineNumber()),
63  ioEndLineNumber_(-1),
64  ioGlobal_(ios.global())
65 {}
66 
67 
69 :
70  ioFileName_(dict.name()),
71  ioStartLineNumber_(dict.startLineNumber()),
72  ioEndLineNumber_(dict.endLineNumber()),
73  ioGlobal_(dict.global())
74 {}
75 
76 
77 Foam::IOerror::IOerror(const string& title)
78 :
79  error(title),
81 {}
82 
83 
84 Foam::OSstream& Foam::IOerror::operator()
85 (
86  const char* functionName,
87  const char* sourceFileName,
88  const int sourceFileLineNumber,
89  const IOerrorLocation& location
90 )
91 {
92  error::operator()(functionName, sourceFileName, sourceFileLineNumber);
93 
94  IOerrorLocation::operator=(location);
95 
96  return operator OSstream&();
97 }
98 
99 
101 (
102  const char* functionName,
103  const char* sourceFileName,
104  const int sourceFileLineNumber,
105  const IOstream& ioStream,
106  const string& msg
107 )
108 {
110  {
112  (
113  functionName,
114  sourceFileName,
115  sourceFileLineNumber,
116  ioStream
117  ) << msg << Foam::exit(FatalIOError);
118  }
119  else
120  {
121  std::cerr
122  << std::endl
123  << "--> FOAM FATAL IO ERROR:" << std::endl
124  << msg
125  << std::endl
126  << "file: " << ioStream.name()
127  << " at line " << ioStream.lineNumber() << '.'
128  << std::endl << std::endl
129  << " From function " << functionName
130  << std::endl
131  << " in file " << sourceFileName
132  << " at line " << sourceFileLineNumber << '.'
133  << std::endl;
134  ::exit(1);
135  }
136 }
137 
138 
139 Foam::IOerror::operator Foam::dictionary() const
140 {
141  dictionary errDict(error::operator dictionary());
142 
143  errDict.remove("type");
144  errDict.add("type", word("Foam::IOerror"));
145 
146  errDict.add("ioFileName", ioFileName());
147  errDict.add("ioStartLineNumber", ioStartLineNumber());
148 
149  if (ioEndLineNumber() != -1)
150  {
151  errDict.add("ioEndLineNumber", ioEndLineNumber());
152  }
153 
154  return errDict;
155 }
156 
157 
158 void Foam::IOerror::exit(const int errNo)
159 {
160  if (IOerror::level <= 0)
161  {
162  if (Pstream::parRun())
163  {
164  Pstream::exit(errNo);
165  }
166  else
167  {
168  ::exit(errNo);
169  }
170  }
171 
172  if (!throwExceptions_ && jobInfo::constructed)
173  {
174  jobInfo_.add("FatalIOError", operator dictionary());
175  jobInfo_.exit();
176  }
177 
178  if (abort_)
179  {
180  abort();
181  }
182 
183  if (Pstream::parRun())
184  {
185  if (ioGlobal())
186  {
187  if (Pstream::master())
188  {
189  Serr<< endl << *this << endl
190  << "\nFOAM parallel run exiting\n" << endl;
191  }
192  }
193  else
194  {
195  Perr<< endl << *this << endl
196  << "\nFOAM parallel run exiting\n" << endl;
197  }
198 
199  Pstream::exit(errNo);
200  }
201  else
202  {
203  if (throwExceptions_)
204  {
205  // Make a copy of the error to throw
206  IOerror errorException(*this);
207 
208  // Rewind the message buffer for the next error message
209  messageStream_.rewind();
210 
211  throw errorException;
212  }
213  else
214  {
215  Serr<< endl << *this << endl
216  << "\nFOAM exiting\n" << endl;
217  ::exit(errNo);
218  }
219  }
220 }
221 
222 
224 {
225  if (!throwExceptions_ && jobInfo::constructed)
226  {
227  jobInfo_.add("FatalIOError", operator dictionary());
228  jobInfo_.abort();
229  }
230 
231  if (abort_)
232  {
233  Perr<< endl << *this << endl
234  << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
235  printStack(Perr);
236  ::abort();
237  }
238 
239  if (Pstream::parRun())
240  {
241  if (ioGlobal())
242  {
243  if (Pstream::master())
244  {
245  Serr<< endl << *this << endl
246  << "\nFOAM parallel run aborting\n" << endl;
247  printStack(Perr);
248  }
249  }
250  else
251  {
252  Perr<< endl << *this << endl
253  << "\nFOAM parallel run aborting\n" << endl;
254  printStack(Perr);
255  }
256 
257  Pstream::abort();
258  }
259  else
260  {
261  if (throwExceptions_)
262  {
263  // Make a copy of the error to throw
264  IOerror errorException(*this);
265 
266  // Rewind the message buffer for the next error message
267  messageStream_.rewind();
268 
269  throw errorException;
270  }
271  else
272  {
273  Serr<< endl << *this << endl
274  << "\nFOAM aborting\n" << endl;
275  printStack(Serr);
276  ::abort();
277  }
278  }
279 }
280 
281 
283 {
284  if (!os.bad())
285  {
286  os << endl
287  << ioErr.title().c_str() << endl
288  << ioErr.message().c_str() << endl << endl;
289 
290  os << "file: " << ioErr.ioFileName().c_str();
291 
292  if (ioErr.ioStartLineNumber() >= 0)
293  {
294  if (ioErr.ioEndLineNumber() > ioErr.ioStartLineNumber())
295  {
296  os << " from line " << ioErr.ioStartLineNumber()
297  << " to " << ioErr.ioEndLineNumber() << '.';
298  }
299  else
300  {
301  os << " at line " << ioErr.ioStartLineNumber() << '.';
302  }
303  }
304 
305  if (IOerror::level >= 2 && ioErr.sourceFileLineNumber())
306  {
307  os << endl << endl
308  << " From function " << ioErr.functionName().c_str() << endl
309  << " in file " << ioErr.sourceFileName().c_str()
310  << " at line " << ioErr.sourceFileLineNumber() << '.';
311  }
312  }
313 
314  return os;
315 }
316 
317 
318 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
319 // Global error definitions
320 
321 Foam::IOerror Foam::FatalIOError("--> FOAM FATAL IO ERROR: ");
322 
323 // ************************************************************************* //
const string & ioFileName() const
Access the file name.
Definition: error.H:229
IOerrorLocation()
Construct null.
Definition: IOerror.C:35
label ioEndLineNumber() const
Access the end line number.
Definition: error.H:241
label ioStartLineNumber() const
Access the start line number.
Definition: error.H:235
static void SafeFatalIOError(const char *functionName, const char *sourceFileName, const int sourceFileLineNumber, const IOstream &, const string &msg)
Print basic message and exit. Uses cerr if streams not constructed.
Definition: IOerror.C:101
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:158
IOerror(const string &title)
Construct from title string.
Definition: IOerror.C:77
void abort()
Abort : used to stop code for fatal errors.
Definition: IOerror.C:223
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:72
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:450
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:297
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:351
Generic output stream.
Definition: OSstream.H:54
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:57
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:423
static void abort()
Abort program.
Definition: UPstream.C:52
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:399
static void exit(int errnum=1)
Exit program.
Definition: UPstream.C:46
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:1020
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Definition: error.H:71
const string & sourceFileName() const
Definition: error.H:105
const string & functionName() const
Definition: error.H:100
string message() const
Definition: error.C:119
OSstream & operator()()
Explicitly convert to OSstream for << operations.
Definition: error.C:88
label sourceFileLineNumber() const
Definition: error.H:110
A functionName is a word starting with '#'.
Definition: functionName.H:60
void exit()
Definition: jobInfo.C:198
static bool constructed
Definition: jobInfo.H:72
void abort()
Definition: jobInfo.C:204
const string & title() const
Return the title of this error type.
A class for handling words, derived from string.
Definition: word.H:62
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
prefixOSstream Perr(cerr, "Perr")
Definition: IOstreams.H:54
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:59
jobInfo jobInfo_
Definition: jobInfo.C:44
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:258
errorManip< error > abort(error &err)
Definition: errorManip.H:131
IOerror FatalIOError
word name(const LagrangianState state)
Return a string representation of a Lagrangian state enumeration.
OSstream Serr(cerr, "Serr")
Definition: IOstreams.H:52
Ostream & operator<<(Ostream &os, const fvConstraints &constraints)
dictionary dict