initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / db / error / IOerror.C
blob584481236906e342ec86d1b23a61d412cd3ea911
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2009 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "error.H"
28 #include "OStringStream.H"
29 #include "fileName.H"
30 #include "dictionary.H"
31 #include "JobInfo.H"
32 #include "Pstream.H"
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 Foam::IOerror::IOerror(const string& title)
38     error(title),
39     ioFileName_("unknown"),
40     ioStartLineNumber_(-1),
41     ioEndLineNumber_(-1)
45 Foam::IOerror::IOerror(const dictionary& errDict)
47     error(errDict),
48     ioFileName_(errDict.lookup("ioFileName")),
49     ioStartLineNumber_(readLabel(errDict.lookup("ioStartLineNumber"))),
50     ioEndLineNumber_(readLabel(errDict.lookup("ioEndLineNumber")))
54 Foam::IOerror::~IOerror() throw()
58 Foam::OSstream& Foam::IOerror::operator()
60     const char* functionName,
61     const char* sourceFileName,
62     const int sourceFileLineNumber,
63     const string& ioFileName,
64     const label ioStartLineNumber,
65     const label ioEndLineNumber
68     error::operator()(functionName, sourceFileName, sourceFileLineNumber);
69     ioFileName_ = ioFileName;
70     ioStartLineNumber_ = ioStartLineNumber;
71     ioEndLineNumber_ = ioEndLineNumber;
73     return operator OSstream&();
77 Foam::OSstream& Foam::IOerror::operator()
79     const char* functionName,
80     const char* sourceFileName,
81     const int sourceFileLineNumber,
82     const IOstream& ioStream
85     return operator()
86     (
87         functionName,
88         sourceFileName,
89         sourceFileLineNumber,
90         ioStream.name(),
91         ioStream.lineNumber(),
92         -1
93     );
97 Foam::OSstream& Foam::IOerror::operator()
99     const char* functionName,
100     const char* sourceFileName,
101     const int sourceFileLineNumber,
102     const dictionary& dict
105     return operator()
106     (
107         functionName,
108         sourceFileName,
109         sourceFileLineNumber,
110         dict.name(),
111         dict.startLineNumber(),
112         dict.endLineNumber()
113     );
117 Foam::IOerror::operator Foam::dictionary() const
119     dictionary errDict(error::operator dictionary());
121     errDict.remove("type");
122     errDict.add("type", word("Foam::IOerror"));
124     errDict.add("ioFileName", ioFileName());
125     errDict.add("ioStartLineNumber", ioStartLineNumber());
126     errDict.add("ioEndLineNumber", ioEndLineNumber());
128     return errDict;
132 void Foam::IOerror::exit(const int)
134     if (!throwExceptions_ && JobInfo::constructed)
135     {
136         jobInfo.add("FatalIOError", operator dictionary());
137         jobInfo.exit();
138     }
140     if (abort_)
141     {
142         Perr<< endl << *this << endl
143             << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
144         printStack(Perr);
145         ::abort();
146     }
148     if (Pstream::parRun())
149     {
150         Perr<< endl << *this << endl
151             << "\nFOAM parallel run exiting\n" << endl;
152         Pstream::exit(1);
153     }
154     else
155     {
156         if (throwExceptions_)
157         {
158             throw *this;
159         }
160         else
161         {
162             Perr<< endl << *this << endl
163                 << "\nFOAM exiting\n" << endl;
164             ::exit(1);
165         }
166     }
170 void Foam::IOerror::abort()
172     if (!throwExceptions_ && JobInfo::constructed)
173     {
174         jobInfo.add("FatalIOError", operator dictionary());
175         jobInfo.abort();
176     }
178     if (abort_)
179     {
180         Perr<< endl << *this << endl
181             << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
182         printStack(Perr);
183         ::abort();
184     }
186     if (Pstream::parRun())
187     {
188         Perr<< endl << *this << endl
189             << "\nFOAM parallel run aborting\n" << endl;
190         printStack(Perr);
191         Pstream::abort();
192     }
193     else
194     {
195         if (throwExceptions_)
196         {
197             throw *this;
198         }
199         else
200         {
201             Perr<< endl << *this << endl
202                 << "\nFOAM aborting\n" << endl;
203             printStack(Perr);
204             ::abort();
205         }
206     }
210 Foam::Ostream& Foam::operator<<(Ostream& os, const IOerror& ioErr)
212     os  << endl << ioErr.message().c_str() << endl << endl;
214     os  << "file: " << ioErr.ioFileName().c_str();
216     if (ioErr.ioStartLineNumber() >= 0 && ioErr.ioEndLineNumber() >= 0)
217     {
218         os  << " from line " << ioErr.ioStartLineNumber()
219             << " to line " << ioErr.ioEndLineNumber() << '.';
220     }
221     else if (ioErr.ioStartLineNumber() >= 0)
222     {
223         os  << " at line " << ioErr.ioStartLineNumber() << '.';
224     }
226     if (IOerror::level >= 2 && ioErr.sourceFileLineNumber())
227     {
228         os  << endl << endl
229             << "    From function " << ioErr.functionName().c_str() << endl
230             << "    in file " << ioErr.sourceFileName().c_str()
231             << " at line " << ioErr.sourceFileLineNumber() << '.';
232     }
234     return os;
238 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
239 // Global error definitions
241 Foam::IOerror Foam::FatalIOError("--> FOAM FATAL IO ERROR : ");
243 // ************************************************************************* //