FreeFOAM The Cross-Platform CFD Toolkit
messageStream.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
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
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
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
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "error.H"
27 #include <OpenFOAM/dictionary.H>
28 #include <OpenFOAM/Pstream.H>
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
33 
34 
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
36 
38 (
39  const string& title,
40  errorSeverity sev,
41  const int maxErrors
42 )
43 :
44  title_(title),
45  severity_(sev),
46  maxErrors_(maxErrors),
47  errorCount_(0)
48 {}
49 
50 
52 :
53  title_(dict.lookup("title")),
54  severity_(FATAL),
55  maxErrors_(0),
56  errorCount_(0)
57 {}
58 
59 
60 Foam::OSstream& Foam::messageStream::operator()
61 (
62  const char* functionName,
63  const char* sourceFileName,
64  const int sourceFileLineNumber
65 )
66 {
67  OSstream& os = operator OSstream&();
68 
69  os << endl
70  << " From function " << functionName << endl
71  << " in file " << sourceFileName
72  << " at line " << sourceFileLineNumber << endl
73  << " ";
74 
75  return os;
76 }
77 
78 
79 Foam::OSstream& Foam::messageStream::operator()
80 (
81  const string& functionName,
82  const char* sourceFileName,
83  const int sourceFileLineNumber
84 )
85 {
86  return operator()
87  (
88  functionName.c_str(),
89  sourceFileName,
90  sourceFileLineNumber
91  );
92 }
93 
94 
95 Foam::OSstream& Foam::messageStream::operator()
96 (
97  const char* functionName,
98  const char* sourceFileName,
99  const int sourceFileLineNumber,
100  const string& ioFileName,
101  const label ioStartLineNumber,
102  const label ioEndLineNumber
103 )
104 {
105  OSstream& os = operator OSstream&();
106 
107  os << endl
108  << " From function " << functionName << endl
109  << " in file " << sourceFileName
110  << " at line " << sourceFileLineNumber << endl
111  << " Reading " << ioFileName;
112 
113  if (ioStartLineNumber >= 0 && ioEndLineNumber >= 0)
114  {
115  os << " from line " << ioStartLineNumber
116  << " to line " << ioEndLineNumber;
117  }
118  else if (ioStartLineNumber >= 0)
119  {
120  os << " at line " << ioStartLineNumber;
121  }
122 
123  os << endl << " ";
124 
125  return os;
126 }
127 
128 
129 Foam::OSstream& Foam::messageStream::operator()
130 (
131  const char* functionName,
132  const char* sourceFileName,
133  const int sourceFileLineNumber,
134  const IOstream& ioStream
135 )
136 {
137  return operator()
138  (
139  functionName,
140  sourceFileName,
141  sourceFileLineNumber,
142  ioStream.name(),
143  ioStream.lineNumber(),
144  -1
145  );
146 }
147 
148 
149 Foam::OSstream& Foam::messageStream::operator()
150 (
151  const char* functionName,
152  const char* sourceFileName,
153  const int sourceFileLineNumber,
154  const dictionary& dict
155 )
156 {
157  return operator()
158  (
159  functionName,
160  sourceFileName,
161  sourceFileLineNumber,
162  dict.name(),
163  dict.startLineNumber(),
164  dict.endLineNumber()
165  );
166 }
167 
168 
169 Foam::messageStream::operator Foam::OSstream&()
170 {
171  if (level)
172  {
173  bool collect = (severity_ == INFO || severity_ == WARNING);
174 
175  // Report the error
176  if (!Pstream::master() && collect)
177  {
178  return Snull;
179  }
180  else
181  {
182  if (title().size())
183  {
184  if (Pstream::parRun() && !collect)
185  {
186  Pout<< title().c_str();
187  }
188  else
189  {
190  Sout<< title().c_str();
191  }
192  }
193 
194  if (maxErrors_)
195  {
196  errorCount_++;
197 
198  if (errorCount_ >= maxErrors_)
199  {
200  FatalErrorIn("messageStream::operator OSstream&()")
201  << "Too many errors"
202  << abort(FatalError);
203  }
204  }
205 
206  if (Pstream::parRun() && !collect)
207  {
208  return Pout;
209  }
210  else
211  {
212  return Sout;
213  }
214  }
215  }
216 
217  return Snull;
218 }
219 
220 
221 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
222 // Global messageStream definitions
223 
225 (
226  "--> FOAM Serious Error : ",
228  100
229 );
230 
232 (
233  "--> FOAM Warning : ",
235 );
236 
238 
239 
240 // ************************ vim: set sw=4 sts=4 et: ************************ //