FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

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-2010 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
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your 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, see <http://www.gnu.org/licenses/>.
00023 
00024 Class
00025     Foam::error
00026 
00027 Description
00028     Class to handle errors and exceptions in a simple, consistent stream-based
00029     manner.
00030 
00031     The error class is globaly instantiated with a title string. Errors,
00032     messages and other data are piped to the messageStream class in the
00033     standard manner.  Manipulators are supplied for exit and abort which may
00034     terminate the program or throw an exception depending of if the exception
00035     handling has beed switched on (off by default).
00036 
00037 Usage
00038     @code
00039         error << "message1" << "message2" << FoamDataType << exit(errNo);
00040         error << "message1" << "message2" << FoamDataType << abort();
00041     @endcode
00042 
00043 SourceFiles
00044     error.C
00045 
00046 \*---------------------------------------------------------------------------*/
00047 
00048 #ifndef error_H
00049 #define error_H
00050 
00051 #include "messageStream.H"
00052 
00053 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00054 
00055 namespace Foam
00056 {
00057 
00058 // Forward declaration of friend functions and operators
00059 class error;
00060 Ostream& operator<<(Ostream&, const error&);
00061 
00062 
00063 /*---------------------------------------------------------------------------*\
00064                            Class error Declaration
00065 \*---------------------------------------------------------------------------*/
00066 
00067 class error
00068 :
00069     public std::exception,
00070     public messageStream
00071 {
00072 
00073 protected:
00074 
00075     // Protected data
00076 
00077         string functionName_;
00078         string sourceFileName_;
00079         label sourceFileLineNumber_;
00080 
00081         bool abort_;
00082 
00083         bool throwExceptions_;
00084         OStringStream* messageStreamPtr_;
00085 
00086 public:
00087 
00088     // Constructors
00089 
00090         //- Construct from title string
00091         error(const string& title);
00092 
00093         //- Construct from dictionary
00094         error(const dictionary& errDict);
00095 
00096         //- Construct as copy
00097         error(const error& err);
00098 
00099 
00100     // Destructor
00101 
00102         virtual ~error() throw();
00103 
00104 
00105     // Member functions
00106 
00107         string message() const;
00108 
00109         const string& functionName() const
00110         {
00111             return functionName_;
00112         }
00113 
00114         const string& sourceFileName() const
00115         {
00116             return sourceFileName_;
00117         }
00118 
00119         label sourceFileLineNumber() const
00120         {
00121             return sourceFileLineNumber_;
00122         }
00123 
00124         void throwExceptions()
00125         {
00126             throwExceptions_ = true;
00127         }
00128 
00129         void dontThrowExceptions()
00130         {
00131             throwExceptions_ = false;
00132         }
00133 
00134         //- Convert to Ostream
00135         //  Prints basic message and then returns Ostream for further info.
00136         OSstream& operator()
00137         (
00138             const char* functionName,
00139             const char* sourceFileName,
00140             const int sourceFileLineNumber = 0
00141         );
00142 
00143         OSstream& operator()
00144         (
00145             const string& functionName,
00146             const char* sourceFileName,
00147             const int sourceFileLineNumber = 0
00148         );
00149 
00150         //- Convert to Ostream
00151         //  Prints basic message and then returns Ostream for further info.
00152         operator OSstream&();
00153 
00154         //- Explicitly convert to Ostream for << operations
00155         OSstream& operator()()
00156         {
00157             return operator OSstream&();
00158         }
00159 
00160         //- Create and return a dictionary
00161         operator dictionary() const;
00162 
00163 
00164         //- Helper function to print a stack
00165         static void printStack(Ostream& os);
00166 
00167         //- Exit : can be called for any error to exit program. Prints stack
00168         //  before exiting.
00169         void exit(const int errNo = 1);
00170 
00171         //- Abort : used to stop code for fatal errors. Prints stack before
00172         //  exiting.
00173         void abort();
00174 
00175 
00176     // Ostream operator
00177 
00178         friend Ostream& operator<<(Ostream&, const error&);
00179 };
00180 
00181 
00182 // Forward declaration of friend functions and operators
00183 
00184 class IOerror;
00185 
00186 Ostream& operator<<(Ostream&, const IOerror&);
00187 
00188 
00189 /*---------------------------------------------------------------------------*\
00190                            Class IOerror Declaration
00191 \*---------------------------------------------------------------------------*/
00192 
00193 //- Report an I/O error
00194 class IOerror
00195 :
00196     public error
00197 {
00198     // Private data
00199 
00200         string ioFileName_;
00201         label ioStartLineNumber_;
00202         label ioEndLineNumber_;
00203 
00204 
00205 public:
00206 
00207     // Constructors
00208 
00209         //- Construct from title string
00210         IOerror(const string& title);
00211 
00212         //- Construct from dictionary
00213         IOerror(const dictionary& errDict);
00214 
00215 
00216     // Destructor
00217 
00218         virtual ~IOerror() throw();
00219 
00220 
00221     // Member functions
00222 
00223         const string& ioFileName() const
00224         {
00225             return ioFileName_;
00226         }
00227 
00228         label ioStartLineNumber() const
00229         {
00230             return ioStartLineNumber_;
00231         }
00232 
00233         label ioEndLineNumber() const
00234         {
00235             return ioEndLineNumber_;
00236         }
00237 
00238         //- Convert to Ostream
00239         //  Prints basic message and then returns Ostream for further info.
00240         OSstream& operator()
00241         (
00242             const char* functionName,
00243             const char* sourceFileName,
00244             const int sourceFileLineNumber,
00245             const string& ioFileName,
00246             const label ioStartLineNumber = -1,
00247             const label ioEndLineNumber = -1
00248         );
00249 
00250         //- Convert to Ostream
00251         //  Prints basic message and then returns Ostream for further info.
00252         OSstream& operator()
00253         (
00254             const char* functionName,
00255             const char* sourceFileName,
00256             const int sourceFileLineNumber,
00257             const IOstream&
00258         );
00259 
00260         //- Convert to Ostream
00261         //  Prints basic message and then returns Ostream for further info.
00262         OSstream& operator()
00263         (
00264             const char* functionName,
00265             const char* sourceFileName,
00266             const int sourceFileLineNumber,
00267             const dictionary&
00268         );
00269 
00270         //- Create and return a dictionary
00271         operator dictionary() const;
00272 
00273 
00274         //- Exit : can be called for any error to exit program
00275         void exit(const int errNo = 1);
00276 
00277         //- Abort : used to stop code for fatal errors
00278         void abort();
00279 
00280 
00281     // Ostream operator
00282 
00283         friend Ostream& operator<<(Ostream&, const IOerror&);
00284 };
00285 
00286 
00287 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00288 // Global error declarations: defined in error.C
00289 
00290 extern error FatalError;
00291 extern IOerror FatalIOError;
00292 
00293 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00294 // Convienient macros to add the file name and line number to the function name
00295 
00296 #define FatalErrorIn(fn) FatalError(fn, __FILE__, __LINE__)
00297 #define FatalIOErrorIn(fn, ios) FatalIOError(fn, __FILE__, __LINE__, ios)
00298 
00299 // Call for functions which are not currently implemented.
00300 // The functionName is printed and then abort is called.
00301 #define notImplemented(fn) \
00302     FatalErrorIn(fn) << "Not implemented" << Foam::abort(FatalError);
00303 
00304 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00305 
00306 } // End namespace Foam
00307 
00308 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00309 
00310 #include "errorManip.H"
00311 
00312 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00313 
00314 #endif
00315 
00316 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines