OpenFOAM logo
Open Source CFD Toolkit

error.H

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2005 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software; you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by the
00013     Free Software Foundation; either version 2 of the License, or (at your
00014     option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM; if not, write to the Free Software Foundation,
00023     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
00024 
00025 Class
00026     error
00027 
00028 Description
00029     Class to handle errors and exceptions in a simple, consistent stream-based
00030     manner.  The error class is globaly instantiated with a title string.
00031     Errors, messages and other data are piped to the messageStream class in the
00032     standard manner.   Manipulators are supplied for exit and abort which may
00033     terminate the program or throw an exception depending of if the exception
00034      handling has beed switched on (off by default).
00035 
00036 Usage
00037     error << "message1" << "message2" << FoamDataType << exit(errNo);
00038     error << "message1" << "message2" << FoamDataType << abort();
00039 
00040 SourceFiles
00041     error.C
00042 
00043 \*---------------------------------------------------------------------------*/
00044 
00045 #ifndef error_H
00046 #define error_H
00047 
00048 #include "messageStream.H"
00049 
00050 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00051 
00052 namespace Foam
00053 {
00054 
00055 /*---------------------------------------------------------------------------*\
00056                            Class error Declaration
00057 \*---------------------------------------------------------------------------*/
00058 
00059 class error
00060 :
00061     public messageStream
00062 {
00063 
00064 protected:
00065 
00066     // Protected data
00067 
00068         string functionName_;
00069         string sourceFileName_;
00070         label sourceFileLineNumber_;
00071 
00072         bool abort_;
00073 
00074         bool throwExceptions_;
00075         OStringStream* messageStreamPtr_;
00076 
00077 public:
00078 
00079     // Constructors
00080 
00081         //- Construct from title string
00082         error(const string& title);
00083 
00084         //- Construct from dictionary
00085         error(const dictionary& errDict);
00086 
00087 
00088     // Member functions
00089 
00090         string message() const;
00091 
00092         const string& functionName() const
00093         {
00094             return functionName_;
00095         }
00096 
00097         const string& sourceFileName() const
00098         {
00099             return sourceFileName_;
00100         }
00101 
00102         label sourceFileLineNumber() const
00103         {
00104             return sourceFileLineNumber_;
00105         }
00106 
00107         void throwExceptions()
00108         {
00109             throwExceptions_ = true;
00110         }
00111 
00112         void dontThrowExceptions()
00113         {
00114             throwExceptions_ = false;
00115         }
00116 
00117         //- Convert to Ostream
00118         //  Prints basic message and then returns Ostream for further info.
00119         OSstream& operator()
00120         (
00121             const char* functionName,
00122             const char* sourceFileName,
00123             const int sourceFileLineNumber = 0
00124         );
00125 
00126         OSstream& operator()
00127         (
00128             const string& functionName,
00129             const char* sourceFileName,
00130             const int sourceFileLineNumber = 0
00131         );
00132 
00133         //- Convert to Ostream
00134         //  Prints basic message and then returns Ostream for further info.
00135         operator OSstream&();
00136 
00137         //- Create and return a dictionary
00138         operator dictionary() const;
00139 
00140 
00141         //- Helper function to print a stack
00142         static void printStack(Ostream& os);
00143 
00144         //- Exit : can be called for any error to exit program. Prints stack
00145         //  before exiting.
00146         void exit(const int errNo = 1);
00147 
00148         //- Abort : used to stop code for fatal errors. Prints stack before
00149         //  exiting.
00150         void abort();
00151 
00152 
00153     // Ostream operator
00154 
00155         friend Ostream& operator<<(Ostream&, const error&);
00156 };
00157 
00158 
00159 /*---------------------------------------------------------------------------*\
00160                            Class IOerror Declaration
00161 \*---------------------------------------------------------------------------*/
00162 
00163 class IOerror
00164 :
00165     public error
00166 {
00167     // Private data
00168 
00169         string ioFileName_;
00170         label ioStartLineNumber_;
00171         label ioEndLineNumber_;
00172 
00173 
00174 public:
00175 
00176     // Constructors
00177 
00178         //- Construct from title string
00179         IOerror(const string& title);
00180 
00181         //- Construct from dictionary
00182         IOerror(const dictionary& errDict);
00183 
00184 
00185     // Member functions
00186 
00187         const string& ioFileName() const
00188         {
00189             return ioFileName_;
00190         }
00191 
00192         label ioStartLineNumber() const
00193         {
00194             return ioStartLineNumber_;
00195         }
00196 
00197         label ioEndLineNumber() const
00198         {
00199             return ioEndLineNumber_;
00200         }
00201 
00202         //- Convert to Ostream
00203         //  Prints basic message and then returns Ostream for further info.
00204         OSstream& operator()
00205         (
00206             const char* functionName,
00207             const char* sourceFileName,
00208             const int sourceFileLineNumber,
00209             const string& ioFileName,
00210             const label ioStartLineNumber = -1,
00211             const label ioEndLineNumber = -1
00212         );
00213 
00214         //- Convert to Ostream
00215         //  Prints basic message and then returns Ostream for further info.
00216         OSstream& operator()
00217         (
00218             const char* functionName,
00219             const char* sourceFileName,
00220             const int sourceFileLineNumber,
00221             const IOstream&
00222         );
00223 
00224         //- Convert to Ostream
00225         //  Prints basic message and then returns Ostream for further info.
00226         OSstream& operator()
00227         (
00228             const char* functionName,
00229             const char* sourceFileName,
00230             const int sourceFileLineNumber,
00231             const dictionary&
00232         );
00233 
00234         //- Create and return a dictionary
00235         operator dictionary() const;
00236 
00237 
00238         //- Exit : can be called for any error to exit program
00239         void exit(const int errNo = 1);
00240 
00241         //- Abort : used to stop code for fatal errors
00242         void abort();
00243 
00244 
00245     // Ostream operator
00246 
00247         friend Ostream& operator<<(Ostream&, const IOerror&);
00248 };
00249 
00250 
00251 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00252 // Global error declarations: defined in error.C
00253 
00254 extern error         FatalError;
00255 extern IOerror       FatalIOError;
00256 
00257 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00258 // Convienient macros to add the file name and line number to the function name
00259 
00260 #define FatalErrorIn(fn) FatalError(fn, __FILE__, __LINE__)
00261 #define FatalIOErrorIn(fn, ios) FatalIOError(fn, __FILE__, __LINE__, ios)
00262 
00263 // Call for functions which are not currently implemented.
00264 // The functionName is printed and then abort is called.
00265 #define notImplemented(fn) \
00266     FatalErrorIn(fn) << "Not implemented" << Foam::abort(FatalError);
00267 
00268 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00269 
00270 } // End namespace Foam
00271 
00272 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00273 
00274 #include "errorManip.H"
00275 
00276 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00277 
00278 #endif
00279 
00280 // ************************************************************************* //
For further information go to www.openfoam.org