FreeFOAM The Cross-Platform CFD Toolkit
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) 1991-2010 OpenCFD Ltd.
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 <OpenFOAM/OStringStream.H>
28 #include <OpenFOAM/fileName.H>
29 #include <OpenFOAM/dictionary.H>
30 #include <OpenFOAM/JobInfo.H>
31 #include <OpenFOAM/Pstream.H>
32 
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 
35 Foam::IOerror::IOerror(const string& title)
36 :
37  error(title),
38  ioFileName_("unknown"),
39  ioStartLineNumber_(-1),
40  ioEndLineNumber_(-1)
41 {}
42 
43 
45 :
46  error(errDict),
47  ioFileName_(errDict.lookup("ioFileName")),
48  ioStartLineNumber_(readLabel(errDict.lookup("ioStartLineNumber"))),
49  ioEndLineNumber_(readLabel(errDict.lookup("ioEndLineNumber")))
50 {}
51 
52 
54 {}
55 
56 
57 Foam::OSstream& Foam::IOerror::operator()
58 (
59  const char* functionName,
60  const char* sourceFileName,
61  const int sourceFileLineNumber,
62  const string& ioFileName,
63  const label ioStartLineNumber,
64  const label ioEndLineNumber
65 )
66 {
67  error::operator()(functionName, sourceFileName, sourceFileLineNumber);
68  ioFileName_ = ioFileName;
69  ioStartLineNumber_ = ioStartLineNumber;
70  ioEndLineNumber_ = ioEndLineNumber;
71 
72  return operator OSstream&();
73 }
74 
75 
76 Foam::OSstream& Foam::IOerror::operator()
77 (
78  const char* functionName,
79  const char* sourceFileName,
80  const int sourceFileLineNumber,
81  const IOstream& ioStream
82 )
83 {
84  return operator()
85  (
86  functionName,
87  sourceFileName,
88  sourceFileLineNumber,
89  ioStream.name(),
90  ioStream.lineNumber(),
91  -1
92  );
93 }
94 
95 
96 Foam::OSstream& Foam::IOerror::operator()
97 (
98  const char* functionName,
99  const char* sourceFileName,
100  const int sourceFileLineNumber,
101  const dictionary& dict
102 )
103 {
104  return operator()
105  (
106  functionName,
107  sourceFileName,
108  sourceFileLineNumber,
109  dict.name(),
110  dict.startLineNumber(),
111  dict.endLineNumber()
112  );
113 }
114 
115 
116 Foam::IOerror::operator Foam::dictionary() const
117 {
118  dictionary errDict(error::operator dictionary());
119 
120  errDict.remove("type");
121  errDict.add("type", word("Foam::IOerror"));
122 
123  errDict.add("ioFileName", ioFileName());
124  errDict.add("ioStartLineNumber", ioStartLineNumber());
125  errDict.add("ioEndLineNumber", ioEndLineNumber());
126 
127  return errDict;
128 }
129 
130 
131 void Foam::IOerror::exit(const int)
132 {
133  if (!throwExceptions_ && JobInfo::constructed)
134  {
135  jobInfo.add("FatalIOError", operator dictionary());
136  jobInfo.exit();
137  }
138 
139  if (abort_)
140  {
141  abort();
142  }
143 
144  if (Pstream::parRun())
145  {
146  Perr<< endl << *this << endl
147  << "\nFOAM parallel run exiting\n" << endl;
148  Pstream::exit(1);
149  }
150  else
151  {
152  if (throwExceptions_)
153  {
154  // Make a copy of the error to throw
155  IOerror errorException(*this);
156 
157  // Rewind the message buffer for the next error message
158  messageStreamPtr_->rewind();
159 
160  throw errorException;
161  }
162  else
163  {
164  Perr<< endl << *this << endl
165  << "\nFOAM exiting\n" << endl;
166  ::exit(1);
167  }
168  }
169 }
170 
171 
173 {
174  if (!throwExceptions_ && JobInfo::constructed)
175  {
176  jobInfo.add("FatalIOError", operator dictionary());
177  jobInfo.abort();
178  }
179 
180  if (abort_)
181  {
182  Perr<< endl << *this << endl
183  << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
184  printStack(Perr);
185  ::abort();
186  }
187 
188  if (Pstream::parRun())
189  {
190  Perr<< endl << *this << endl
191  << "\nFOAM parallel run aborting\n" << endl;
192  printStack(Perr);
193  Pstream::abort();
194  }
195  else
196  {
197  if (throwExceptions_)
198  {
199  // Make a copy of the error to throw
200  IOerror errorException(*this);
201 
202  // Rewind the message buffer for the next error message
203  messageStreamPtr_->rewind();
204 
205  throw errorException;
206  }
207  else
208  {
209  Perr<< endl << *this << endl
210  << "\nFOAM aborting\n" << endl;
211  printStack(Perr);
212  ::abort();
213  }
214  }
215 }
216 
217 
219 {
220  os << endl
221  << ioErr.title().c_str() << endl
222  << ioErr.message().c_str() << endl << endl;
223 
224  os << "file: " << ioErr.ioFileName().c_str();
225 
226  if (ioErr.ioStartLineNumber() >= 0 && ioErr.ioEndLineNumber() >= 0)
227  {
228  os << " from line " << ioErr.ioStartLineNumber()
229  << " to line " << ioErr.ioEndLineNumber() << '.';
230  }
231  else if (ioErr.ioStartLineNumber() >= 0)
232  {
233  os << " at line " << ioErr.ioStartLineNumber() << '.';
234  }
235 
236  if (IOerror::level >= 2 && ioErr.sourceFileLineNumber())
237  {
238  os << endl << endl
239  << " From function " << ioErr.functionName().c_str() << endl
240  << " in file " << ioErr.sourceFileName().c_str()
241  << " at line " << ioErr.sourceFileLineNumber() << '.';
242  }
243 
244  return os;
245 }
246 
247 
248 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
249 // Global error definitions
250 
251 Foam::IOerror Foam::FatalIOError("--> FOAM FATAL IO ERROR: ");
252 
253 // ************************ vim: set sw=4 sts=4 et: ************************ //