1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
5 \\ / A nd | Copyright (C) 1991-2009 OpenCFD Ltd.
7 -------------------------------------------------------------------------------
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
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 \*---------------------------------------------------------------------------*/
28 #include "OStringStream.H"
30 #include "dictionary.H"
33 #include "OSspecific.H"
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 Foam::error::error(const string& title)
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())
51 << "error::error(const string& title) : cannot open error stream"
58 Foam::error::error(const dictionary& errDict)
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())
72 << "error::error(const dictionary& errDict) : "
73 "cannot open error stream"
80 Foam::error::error(const error& err)
84 functionName_(err.functionName_),
85 sourceFileName_(err.sourceFileName_),
86 sourceFileLineNumber_(err.sourceFileLineNumber_),
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
126 functionName.c_str(),
133 Foam::error::operator OSstream&()
135 if (!messageStreamPtr_->good())
138 << "error::operator OSstream&() : error stream has failed"
144 return *messageStreamPtr_;
148 Foam::error::operator dictionary() const
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());
165 Foam::string Foam::error::message() const
167 return messageStreamPtr_->str();
171 void Foam::error::exit(const int errNo)
173 if (!throwExceptions_ && JobInfo::constructed)
175 jobInfo.add("FatalError", operator dictionary());
182 Perr<< endl << *this << endl
183 << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
187 if (Pstream::parRun())
189 Perr<< endl << *this << endl
190 << "\nFOAM parallel run exiting\n" << endl;
191 Pstream::exit(errNo);
195 if (throwExceptions_)
201 Perr<< endl << *this << endl
202 << "\nFOAM exiting\n" << endl;
209 void Foam::error::abort()
211 if (!throwExceptions_ && JobInfo::constructed)
213 jobInfo.add("FatalError", operator dictionary());
220 Perr<< endl << *this << endl
221 << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
225 if (Pstream::parRun())
228 Perr<< endl << *this << endl
229 << "\nFOAM parallel run aborting\n" << endl;
234 if (throwExceptions_)
241 Perr<< endl << *this << endl
242 << "\nFOAM aborting\n" << endl;
249 Foam::Ostream& Foam::operator<<(Ostream& os, const error& fErr)
251 os << endl << fErr.message().c_str();
253 if (error::level >= 2 && fErr.sourceFileLineNumber())
256 << " From function " << fErr.functionName().c_str() << endl
257 << " in file " << fErr.sourceFileName().c_str()
258 << " at line " << fErr.sourceFileLineNumber() << '.';
265 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
266 // Global error definitions
268 Foam::error Foam::FatalError("--> FOAM FATAL ERROR : ");
270 // ************************************************************************* //