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 | Copyright (C) 2011-2015 OpenFOAM Foundation
6  \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
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 #include "JobInfo.H"
33 
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 
36 Foam::IOerror::IOerror(const string& title)
37 :
38  error(title),
39  ioFileName_("unknown"),
40  ioStartLineNumber_(-1),
41  ioEndLineNumber_(-1)
42 {}
43 
44 
46 :
47  error(errDict),
48  ioFileName_(errDict.lookup("ioFileName")),
49  ioStartLineNumber_(readLabel(errDict.lookup("ioStartLineNumber"))),
50  ioEndLineNumber_(readLabel(errDict.lookup("ioEndLineNumber")))
51 {}
52 
53 
55 {}
56 
57 
58 Foam::OSstream& Foam::IOerror::operator()
59 (
60  const char* functionName,
61  const char* sourceFileName,
62  const int sourceFileLineNumber,
63  const string& ioFileName,
64  const label ioStartLineNumber,
65  const label ioEndLineNumber
66 )
67 {
68  error::operator()(functionName, sourceFileName, sourceFileLineNumber);
69  ioFileName_ = ioFileName;
70  ioStartLineNumber_ = ioStartLineNumber;
71  ioEndLineNumber_ = ioEndLineNumber;
72 
73  return operator OSstream&();
74 }
75 
76 
77 Foam::OSstream& Foam::IOerror::operator()
78 (
79  const char* functionName,
80  const char* sourceFileName,
81  const int sourceFileLineNumber,
82  const IOstream& ioStream
83 )
84 {
85  return operator()
86  (
87  functionName,
88  sourceFileName,
89  sourceFileLineNumber,
90  ioStream.name(),
91  ioStream.lineNumber(),
92  -1
93  );
94 }
95 
96 
97 Foam::OSstream& Foam::IOerror::operator()
98 (
99  const char* functionName,
100  const char* sourceFileName,
101  const int sourceFileLineNumber,
102  const dictionary& dict
103 )
104 {
105  return operator()
106  (
107  functionName,
108  sourceFileName,
109  sourceFileLineNumber,
110  dict.name(),
113  );
114 }
115 
116 
118 (
119  const char* functionName,
120  const char* sourceFileName,
121  const int sourceFileLineNumber,
122  const IOstream& ioStream,
123  const string& msg
124 )
125 {
127  {
129  (
130  ioStream
131  ) << msg << Foam::exit(FatalIOError);
132  }
133  else
134  {
135  std::cerr
136  << std::endl
137  << "--> FOAM FATAL IO ERROR:" << std::endl
138  << msg
139  << std::endl
140  << "file: " << ioStream.name()
141  << " at line " << ioStream.lineNumber() << '.'
142  << std::endl << std::endl
143  << " From function " << functionName
144  << std::endl
145  << " in file " << sourceFileName
146  << " at line " << sourceFileLineNumber << '.'
147  << std::endl;
148  ::exit(1);
149  }
150 }
151 
152 
153 Foam::IOerror::operator Foam::dictionary() const
154 {
155  dictionary errDict(error::operator dictionary());
156 
157  errDict.remove("type");
158  errDict.add("type", word("Foam::IOerror"));
159 
160  errDict.add("ioFileName", ioFileName());
161  errDict.add("ioStartLineNumber", ioStartLineNumber());
162  errDict.add("ioEndLineNumber", ioEndLineNumber());
163 
164  return errDict;
165 }
166 
167 
168 void Foam::IOerror::exit(const int)
169 {
170  if (!throwExceptions_ && JobInfo::constructed)
171  {
172  jobInfo.add("FatalIOError", operator dictionary());
173  jobInfo.exit();
174  }
175 
176  if (env("FOAM_ABORT"))
177  {
178  abort();
179  }
180 
181  if (Pstream::parRun())
182  {
183  Perr<< endl << *this << endl
184  << "\nFOAM parallel run exiting\n" << endl;
185  Pstream::exit(1);
186  }
187  else
188  {
189  if (throwExceptions_)
190  {
191  // Make a copy of the error to throw
192  IOerror errorException(*this);
193 
194  // Rewind the message buffer for the next error message
195  messageStreamPtr_->rewind();
196 
197  throw errorException;
198  }
199  else
200  {
201  Perr<< endl << *this << endl
202  << "\nFOAM exiting\n" << endl;
203  ::exit(1);
204  }
205  }
206 }
207 
208 
210 {
211  if (!throwExceptions_ && JobInfo::constructed)
212  {
213  jobInfo.add("FatalIOError", operator dictionary());
214  jobInfo.abort();
215  }
216 
217  if (env("FOAM_ABORT"))
218  {
219  Perr<< endl << *this << endl
220  << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
221  printStack(Perr);
222  ::abort();
223  }
224 
225  if (Pstream::parRun())
226  {
227  Perr<< endl << *this << endl
228  << "\nFOAM parallel run aborting\n" << endl;
229  printStack(Perr);
230  Pstream::abort();
231  }
232  else
233  {
234  if (throwExceptions_)
235  {
236  // Make a copy of the error to throw
237  IOerror errorException(*this);
238 
239  // Rewind the message buffer for the next error message
240  messageStreamPtr_->rewind();
241 
242  throw errorException;
243  }
244  else
245  {
246  Perr<< endl << *this << endl
247  << "\nFOAM aborting\n" << endl;
248  printStack(Perr);
249 
250  // Prefer ::exit(1) to avoid unnecessary warnings on Windows
251 #ifdef MSWIN
252  ::exit(1);
253 #else
254  ::abort();
255 #endif
256  }
257  }
258 }
259 
260 
262 {
263  if (!os.bad())
264  {
265  os << endl
266  << ioErr.title().c_str() << endl
267  << ioErr.message().c_str() << endl << endl;
268 
269  os << "file: " << ioErr.ioFileName().c_str();
270 
271  if (ioErr.ioStartLineNumber() >= 0 && ioErr.ioEndLineNumber() >= 0)
272  {
273  os << " from line " << ioErr.ioStartLineNumber()
274  << " to line " << ioErr.ioEndLineNumber() << '.';
275  }
276  else if (ioErr.ioStartLineNumber() >= 0)
277  {
278  os << " at line " << ioErr.ioStartLineNumber() << '.';
279  }
280 
281  if (IOerror::level >= 2 && ioErr.sourceFileLineNumber())
282  {
283  os << endl << endl
284  << " From function " << ioErr.functionName().c_str() << endl
285  << " in file " << ioErr.sourceFileName().c_str()
286  << " at line " << ioErr.sourceFileLineNumber() << '.';
287  }
288  }
289 
290  return os;
291 }
292 
293 
294 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
295 // Global error definitions
296 
297 Foam::IOerror Foam::FatalIOError("--> FOAM FATAL IO ERROR: ");
298 
299 // ************************************************************************* //
Foam::JobInfo::exit
void exit()
Definition: JobInfo.C:164
Foam::IOerror::ioEndLineNumber
label ioEndLineNumber() const
Definition: error.H:233
Foam::error::sourceFileName
const string & sourceFileName() const
Definition: error.H:111
Foam::env
bool env(const word &)
Return true if environment variable of given name is defined.
Definition: POSIX.C:95
Foam::IOerror::abort
void abort()
Abort : used to stop code for fatal errors.
Definition: IOerror.C:209
Foam::word
A class for handling words, derived from string.
Definition: word.H:59
Foam::dictionaryName::name
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:103
Foam::UPstream::exit
static void exit(int errnum=1)
Exit program.
Definition: UPstream.C:46
Foam::IOerror::IOerror
IOerror(const string &title)
Construct from title string.
Definition: IOerror.C:36
Foam::UPstream::parRun
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:377
Foam::Perr
prefixOSstream Perr(cerr, "Perr")
Definition: IOstreams.H:54
Foam::messageStream::level
static int level
Definition: messageStream.H:97
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:71
Foam::IOerror::ioStartLineNumber
label ioStartLineNumber() const
Definition: error.H:228
Foam::error::functionName
const string & functionName() const
Definition: error.H:106
Foam::FatalIOError
IOerror FatalIOError
Foam::UPstream::abort
static void abort()
Abort program.
Definition: UPstream.C:52
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::IOerror::ioFileName
const string & ioFileName() const
Definition: error.H:223
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:59
Foam::IOerror::~IOerror
virtual ~IOerror()
Destructor.
Definition: IOerror.C:54
error.H
Foam::dictionary::startLineNumber
label startLineNumber() const
Return line number of first token in dictionary.
Definition: dictionary.C:244
OStringStream.H
Foam::dictionary::endLineNumber
label endLineNumber() const
Return line number of last token in dictionary.
Definition: dictionary.C:257
fileName.H
Foam::IOerror::SafeFatalIOError
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:118
Foam::OSstream
Generic output stream.
Definition: OSstream.H:51
Foam::operator<<
Ostream & operator<<(Ostream &, const edgeMesh &)
Definition: edgeMeshIO.C:130
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::IOstream::bad
bool bad() const
Return true if stream is corrupted.
Definition: IOstream.H:351
Pstream.H
Foam::error::message
string message() const
Definition: error.C:159
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Foam::error::operator()
OSstream & operator()()
Explicitly convert to OSstream for << operations.
Definition: error.H:154
Foam::jobInfo
JobInfo jobInfo
Definition: JobInfo.C:35
Foam::error::sourceFileLineNumber
label sourceFileLineNumber() const
Definition: error.H:116
Foam::IOerror::exit
void exit(const int errNo=1)
Exit : can be called for any error to exit program.
Definition: IOerror.C:168
Foam::JobInfo::constructed
static bool constructed
Definition: JobInfo.H:71
dictionary.H
Foam::readLabel
label readLabel(Istream &is)
Definition: label.H:64
Foam::messageStream::title
const string & title() const
Return the title of this error type.
Definition: messageStream.H:118
JobInfo.H
Foam::IOstream::lineNumber
label lineNumber() const
Return current stream line number.
Definition: IOstream.H:438
Foam::IOerror
Report an I/O error.
Definition: error.H:195
FatalIOErrorInFunction
#define FatalIOErrorInFunction(ios)
Report an error message using Foam::FatalIOError.
Definition: error.H:330
Foam::JobInfo::abort
void abort()
Definition: JobInfo.C:170
Foam::Ostream
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
Definition: Ostream.H:53
Foam::IOstream::name
virtual const fileName & name() const
Return the name of the stream.
Definition: IOstream.H:297
Foam::error
Class to handle errors and exceptions in a simple, consistent stream-based manner.
Definition: error.H:66
Foam::dictionary::add
bool add(entry *, bool mergeEntry=false)
Add a new entry.
Definition: dictionary.C:729
lookup
stressControl lookup("compactNormalStress") >> compactNormalStress