initial commit for version 1.6.x patch release
[OpenFOAM-1.6.x.git] / src / OpenFOAM / db / error / error.C
blob7dc6b2e4e21d37190c964e670b43158552331015
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"
33 #include "OSspecific.H"
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 Foam::error::error(const string& title)
39     std::exception(),
40     messageStream(title, messageStream::FATAL),
41     functionName_("unknown"),
42     sourceFileName_("unknown"),
43     sourceFileLineNumber_(0),
44     abort_(env("FOAM_ABORT")),
45     throwExceptions_(false),
46     messageStreamPtr_(new OStringStream())
48     if (!messageStreamPtr_->good())
49     {
50         Perr<< endl
51             << "error::error(const string& title) : cannot open error stream"
52             << endl;
53         exit(1);
54     }
58 Foam::error::error(const dictionary& errDict)
60     std::exception(),
61     messageStream(errDict),
62     functionName_(errDict.lookup("functionName")),
63     sourceFileName_(errDict.lookup("sourceFileName")),
64     sourceFileLineNumber_(readLabel(errDict.lookup("sourceFileLineNumber"))),
65     abort_(env("FOAM_ABORT")),
66     throwExceptions_(false),
67     messageStreamPtr_(new OStringStream())
69     if (!messageStreamPtr_->good())
70     {
71         Perr<< endl
72             << "error::error(const dictionary& errDict) : "
73                "cannot open error stream"
74             << endl;
75         exit(1);
76     }
80 Foam::error::error(const error& err)
82     std::exception(),
83     messageStream(err),
84     functionName_(err.functionName_),
85     sourceFileName_(err.sourceFileName_),
86     sourceFileLineNumber_(err.sourceFileLineNumber_),
87     abort_(err.abort_),
88     throwExceptions_(err.throwExceptions_),
89     messageStreamPtr_(new OStringStream(*err.messageStreamPtr_))
91     //*messageStreamPtr_ << err.message();
95 Foam::error::~error() throw()
97     delete messageStreamPtr_;
101 Foam::OSstream& Foam::error::operator()
103     const char* functionName,
104     const char* sourceFileName,
105     const int sourceFileLineNumber
108     messageStreamPtr_->rewind();
109     functionName_ = functionName;
110     sourceFileName_ = sourceFileName;
111     sourceFileLineNumber_ = sourceFileLineNumber;
113     return operator OSstream&();
117 Foam::OSstream& Foam::error::operator()
119     const string& functionName,
120     const char* sourceFileName,
121     const int sourceFileLineNumber
124     return operator()
125     (
126         functionName.c_str(),
127         sourceFileName,
128         sourceFileLineNumber
129     );
133 Foam::error::operator OSstream&()
135     if (!messageStreamPtr_->good())
136     {
137         Perr<< endl
138             << "error::operator OSstream&() : error stream has failed"
139             << endl;
140         printStack(Perr);
141         abort();
142     }
144     return *messageStreamPtr_;
148 Foam::error::operator dictionary() const
150     dictionary errDict;
152     string oneLineMessage(message());
153     oneLineMessage.replaceAll('\n', ' ');
155     errDict.add("type", word("Foam::error"));
156     errDict.add("message", oneLineMessage);
157     errDict.add("function", functionName());
158     errDict.add("sourceFile", sourceFileName());
159     errDict.add("sourceFileLineNumber", sourceFileLineNumber());
161     return errDict;
165 Foam::string Foam::error::message() const
167     return messageStreamPtr_->str();
171 void Foam::error::exit(const int errNo)
173     if (!throwExceptions_ && JobInfo::constructed)
174     {
175         jobInfo.add("FatalError", operator dictionary());
176         jobInfo.exit();
177     }
179     if (abort_)
180     {
181         printStack(*this);
182         Perr<< endl << *this << endl
183             << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
184         abort();
185     }
187     if (Pstream::parRun())
188     {
189         Perr<< endl << *this << endl
190             << "\nFOAM parallel run exiting\n" << endl;
191         Pstream::exit(errNo);
192     }
193     else
194     {
195         if (throwExceptions_)
196         {
197             throw *this;
198         }
199         else
200         {
201             Perr<< endl << *this << endl
202                 << "\nFOAM exiting\n" << endl;
203             ::exit(1);
204         }
205     }
209 void Foam::error::abort()
211     if (!throwExceptions_ && JobInfo::constructed)
212     {
213         jobInfo.add("FatalError", operator dictionary());
214         jobInfo.abort();
215     }
217     if (abort_)
218     {
219         printStack(*this);
220         Perr<< endl << *this << endl
221             << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
222         ::abort();
223     }
225     if (Pstream::parRun())
226     {
227         printStack(*this);
228         Perr<< endl << *this << endl
229             << "\nFOAM parallel run aborting\n" << endl;
230         Pstream::abort();
231     }
232     else
233     {
234         if (throwExceptions_)
235         {
236             throw *this;
237         }
238         else
239         {
240             printStack(*this);
241             Perr<< endl << *this << endl
242                 << "\nFOAM aborting\n" << endl;
243             ::abort();
244         }
245     }
249 Foam::Ostream& Foam::operator<<(Ostream& os, const error& fErr)
251     os  << endl << fErr.message().c_str();
253     if (error::level >= 2 && fErr.sourceFileLineNumber())
254     {
255         os  << endl << endl
256             << "    From function " << fErr.functionName().c_str() << endl
257             << "    in file " << fErr.sourceFileName().c_str()
258             << " at line " << fErr.sourceFileLineNumber() << '.';
259     }
261     return os;
265 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
266 // Global error definitions
268 Foam::error Foam::FatalError("--> FOAM FATAL ERROR : ");
270 // ************************************************************************* //