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

messageStream.C

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 \*---------------------------------------------------------------------------*/
00025 
00026 #include "error.H"
00027 #include <OpenFOAM/dictionary.H>
00028 #include <OpenFOAM/Pstream.H>
00029 
00030 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
00031 
00032 int Foam::messageStream::level(Foam::debug::debugSwitch("level", 2));
00033 
00034 
00035 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00036 
00037 Foam::messageStream::messageStream
00038 (
00039     const string& title,
00040     errorSeverity sev,
00041     const int maxErrors
00042 )
00043 :
00044     title_(title),
00045     severity_(sev),
00046     maxErrors_(maxErrors),
00047     errorCount_(0)
00048 {}
00049 
00050 
00051 Foam::messageStream::messageStream(const dictionary& dict)
00052 :
00053     title_(dict.lookup("title")),
00054     severity_(FATAL),
00055     maxErrors_(0),
00056     errorCount_(0)
00057 {}
00058 
00059 
00060 Foam::OSstream& Foam::messageStream::operator()
00061 (
00062     const char* functionName,
00063     const char* sourceFileName,
00064     const int sourceFileLineNumber
00065 )
00066 {
00067     OSstream& os = operator OSstream&();
00068 
00069     os  << endl
00070         << "    From function " << functionName << endl
00071         << "    in file " << sourceFileName
00072         << " at line " << sourceFileLineNumber << endl
00073         << "    ";
00074 
00075     return os;
00076 }
00077 
00078 
00079 Foam::OSstream& Foam::messageStream::operator()
00080 (
00081     const string& functionName,
00082     const char* sourceFileName,
00083     const int sourceFileLineNumber
00084 )
00085 {
00086     return operator()
00087     (
00088         functionName.c_str(),
00089         sourceFileName,
00090         sourceFileLineNumber
00091     );
00092 }
00093 
00094 
00095 Foam::OSstream& Foam::messageStream::operator()
00096 (
00097     const char* functionName,
00098     const char* sourceFileName,
00099     const int sourceFileLineNumber,
00100     const string& ioFileName,
00101     const label ioStartLineNumber,
00102     const label ioEndLineNumber
00103 )
00104 {
00105     OSstream& os = operator OSstream&();
00106 
00107     os  << endl
00108         << "    From function " << functionName << endl
00109         << "    in file " << sourceFileName
00110         << " at line " << sourceFileLineNumber << endl
00111         << "    Reading " << ioFileName;
00112 
00113     if (ioStartLineNumber >= 0 && ioEndLineNumber >= 0)
00114     {
00115         os  << " from line " << ioStartLineNumber
00116             << " to line " << ioEndLineNumber;
00117     }
00118     else if (ioStartLineNumber >= 0)
00119     {
00120         os  << " at line " << ioStartLineNumber;
00121     }
00122 
00123     os << endl  << "    ";
00124 
00125     return os;
00126 }
00127 
00128 
00129 Foam::OSstream& Foam::messageStream::operator()
00130 (
00131     const char* functionName,
00132     const char* sourceFileName,
00133     const int sourceFileLineNumber,
00134     const IOstream& ioStream
00135 )
00136 {
00137     return operator()
00138     (
00139         functionName,
00140         sourceFileName,
00141         sourceFileLineNumber,
00142         ioStream.name(),
00143         ioStream.lineNumber(),
00144         -1
00145     );
00146 }
00147 
00148 
00149 Foam::OSstream& Foam::messageStream::operator()
00150 (
00151     const char* functionName,
00152     const char* sourceFileName,
00153     const int sourceFileLineNumber,
00154     const dictionary& dict
00155 )
00156 {
00157     return operator()
00158     (
00159         functionName,
00160         sourceFileName,
00161         sourceFileLineNumber,
00162         dict.name(),
00163         dict.startLineNumber(),
00164         dict.endLineNumber()
00165     );
00166 }
00167 
00168 
00169 Foam::messageStream::operator Foam::OSstream&()
00170 {
00171     if (level)
00172     {
00173         bool collect = (severity_ == INFO || severity_ == WARNING);
00174 
00175         // Report the error
00176         if (!Pstream::master() && collect)
00177         {
00178             return Snull;
00179         }
00180         else
00181         {
00182             if (title().size())
00183             {
00184                 if (Pstream::parRun() && !collect)
00185                 {
00186                     Pout<< title().c_str();
00187                 }
00188                 else
00189                 {
00190                     Sout<< title().c_str();
00191                 }
00192             }
00193 
00194             if (maxErrors_)
00195             {
00196                 errorCount_++;
00197 
00198                 if (errorCount_ >= maxErrors_)
00199                 {
00200                     FatalErrorIn("messageStream::operator OSstream&()")
00201                         << "Too many errors"
00202                         << abort(FatalError);
00203                 }
00204             }
00205 
00206             if (Pstream::parRun() && !collect)
00207             {
00208                 return Pout;
00209             }
00210             else
00211             {
00212                 return Sout;
00213             }
00214         }
00215     }
00216 
00217     return Snull;
00218 }
00219 
00220 
00221 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00222 // Global messageStream definitions
00223 
00224 Foam::messageStream Foam::SeriousError
00225 (
00226     "--> FOAM Serious Error : ",
00227     messageStream::SERIOUS,
00228     100
00229 );
00230 
00231 Foam::messageStream Foam::Warning
00232 (
00233     "--> FOAM Warning : ",
00234     messageStream::WARNING
00235 );
00236 
00237 Foam::messageStream Foam::Info("", messageStream::INFO);
00238 
00239 
00240 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines