OpenFOAM logo
The Open Source CFD Toolbox
  Programmer’s C++ Doxygen OpenCFD Solutions Contact OpenFOAM

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-2007 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     // Destructor
00089 
00090         ~error();
00091 
00092 
00093     // Member functions
00094 
00095         string message() const;
00096 
00097         const string& functionName() const
00098         {
00099             return functionName_;
00100         }
00101 
00102         const string& sourceFileName() const
00103         {
00104             return sourceFileName_;
00105         }
00106 
00107         label sourceFileLineNumber() const
00108         {
00109             return sourceFileLineNumber_;
00110         }
00111 
00112         void throwExceptions()
00113         {
00114             throwExceptions_ = true;
00115         }
00116 
00117         void dontThrowExceptions()
00118         {
00119             throwExceptions_ = false;
00120         }
00121 
00122         //- Convert to Ostream
00123         //  Prints basic message and then returns Ostream for further info.
00124         OSstream& operator()
00125         (
00126             const char* functionName,
00127             const char* sourceFileName,
00128             const int sourceFileLineNumber = 0
00129         );
00130 
00131         OSstream& operator()
00132         (
00133             const string& functionName,
00134             const char* sourceFileName,
00135             const int sourceFileLineNumber = 0
00136         );
00137 
00138         //- Convert to Ostream
00139         //  Prints basic message and then returns Ostream for further info.
00140         operator OSstream&();
00141 
00142         //- Create and return a dictionary
00143         operator dictionary() const;
00144 
00145 
00146         //- Helper function to print a stack
00147         static void printStack(Ostream& os);
00148 
00149         //- Exit : can be called for any error to exit program. Prints stack
00150         //  before exiting.
00151         void exit(const int errNo = 1);
00152 
00153         //- Abort : used to stop code for fatal errors. Prints stack before
00154         //  exiting.
00155         void abort();
00156 
00157 
00158     // Ostream operator
00159 
00160         friend Ostream& operator<<(Ostream&, const error&);
00161 };
00162 
00163 
00164 /*---------------------------------------------------------------------------*\
00165                            Class IOerror Declaration
00166 \*---------------------------------------------------------------------------*/
00167 
00168 class IOerror
00169 :
00170     public error
00171 {
00172     // Private data
00173 
00174         string ioFileName_;
00175         label ioStartLineNumber_;
00176         label ioEndLineNumber_;
00177 
00178 
00179 public:
00180 
00181     // Constructors
00182 
00183         //- Construct from title string
00184         IOerror(const string& title);
00185 
00186         //- Construct from dictionary
00187         IOerror(const dictionary& errDict);
00188 
00189 
00190     // Member functions
00191 
00192         const string& ioFileName() const
00193         {
00194             return ioFileName_;
00195         }
00196 
00197         label ioStartLineNumber() const
00198         {
00199             return ioStartLineNumber_;
00200         }
00201 
00202         label ioEndLineNumber() const
00203         {
00204             return ioEndLineNumber_;
00205         }
00206 
00207         //- Convert to Ostream
00208         //  Prints basic message and then returns Ostream for further info.
00209         OSstream& operator()
00210         (
00211             const char* functionName,
00212             const char* sourceFileName,
00213             const int sourceFileLineNumber,
00214             const string& ioFileName,
00215             const label ioStartLineNumber = -1,
00216             const label ioEndLineNumber = -1
00217         );
00218 
00219         //- Convert to Ostream
00220         //  Prints basic message and then returns Ostream for further info.
00221         OSstream& operator()
00222         (
00223             const char* functionName,
00224             const char* sourceFileName,
00225             const int sourceFileLineNumber,
00226             const IOstream&
00227         );
00228 
00229         //- Convert to Ostream
00230         //  Prints basic message and then returns Ostream for further info.
00231         OSstream& operator()
00232         (
00233             const char* functionName,
00234             const char* sourceFileName,
00235             const int sourceFileLineNumber,
00236             const dictionary&
00237         );
00238 
00239         //- Create and return a dictionary
00240         operator dictionary() const;
00241 
00242 
00243         //- Exit : can be called for any error to exit program
00244         void exit(const int errNo = 1);
00245 
00246         //- Abort : used to stop code for fatal errors
00247         void abort();
00248 
00249 
00250     // Ostream operator
00251 
00252         friend Ostream& operator<<(Ostream&, const IOerror&);
00253 };
00254 
00255 
00256 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00257 // Global error declarations: defined in error.C
00258 
00259 extern error         FatalError;
00260 extern IOerror       FatalIOError;
00261 
00262 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00263 // Convienient macros to add the file name and line number to the function name
00264 
00265 #define FatalErrorIn(fn) FatalError(fn, __FILE__, __LINE__)
00266 #define FatalIOErrorIn(fn, ios) FatalIOError(fn, __FILE__, __LINE__, ios)
00267 
00268 // Call for functions which are not currently implemented.
00269 // The functionName is printed and then abort is called.
00270 #define notImplemented(fn) \
00271     FatalErrorIn(fn) << "Not implemented" << Foam::abort(FatalError);
00272 
00273 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00274 
00275 } // End namespace Foam
00276 
00277 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00278 
00279 #include "errorManip.H"
00280 
00281 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00282 
00283 #endif
00284 
00285 // ************************************************************************* //
Copyright © 2000-2007 OpenCFD Ltd