messageStream.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2016 OpenFOAM Foundation
9  Copyright (C) 2016-2020 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 Class
28  Foam::messageStream
29 
30 Description
31  Class to handle messaging in a simple, consistent stream-based
32  manner.
33 
34  The messageStream class is globally instantiated with a title string and
35  a severity (which controls the program termination) and a number of
36  errors before termination. Errors, messages and other data are sent to
37  the messageStream class in the standard manner.
38 
39 Usage
40  \code
41  messageStream
42  << "message1" << "message2" << FoamDataType << endl;
43  \endcode
44 
45 SourceFiles
46  messageStream.C
47 
48 \*---------------------------------------------------------------------------*/
49 
50 #ifndef messageStream_H
51 #define messageStream_H
52 
53 #include "label.H"
54 #include "string.H"
55 
56 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
57 
58 namespace Foam
59 {
60 
61 // Forward Declarations
62 class IOstream;
63 class Ostream;
64 class OSstream;
65 class OStringStream;
66 class dictionary;
67 
68 /*---------------------------------------------------------------------------*\
69  Class messageStream Declaration
70 \*---------------------------------------------------------------------------*/
71 
72 class messageStream
73 {
74 public:
75 
76  //- Message type, or error severity flags
77  enum errorSeverity
78  {
79  INFO = 1,
83  INFO_STDERR = INFO | 0x10,
84  };
85 
86 
87 protected:
88 
89  // Protected Data
90 
91  string title_;
94  int errorCount_;
95 
96 
97 public:
98 
99  // Static Data
100 
101  //- Control the output verbosity of messageStream
102  //
103  // - level == 0 : suppress all output
104  // - level == 1 : normal output
105  // - level >= 2 : report source file name and line number if available
106  //
107  // \note The default level is normally 2.
108  static int level;
109 
110 
111  // Constructors
112 
113  //- Construct from components
115  (
116  const string& title,
117  const errorSeverity severity,
118  const int maxErrors = 0
119  );
120 
121  //- Construct as Fatal from dictionary, extracting the 'title'.
122  explicit messageStream(const dictionary& dict);
123 
124 
125  // Member functions
126 
127  //- The title of this error type
128  const string& title() const
129  {
130  return title_;
131  }
132 
133  //- The maximum number of errors before program termination
134  int maxErrors() const
135  {
136  return maxErrors_;
137  }
138 
139  //- Non-const access to the maximum number of errors before
140  //- program termination to enable user to reset it
141  int& maxErrors()
142  {
143  return maxErrors_;
144  }
145 
146  //- Convert to OSstream
147  // Prints to Pout for the master stream
148  OSstream& masterStream(const label communicator);
149 
150  //- Print basic message
151  // \return OSstream for further info.
152  OSstream& operator()
153  (
154  const string& functionName
155  );
156 
157  //- Print basic message
158  // \return OSstream for further info.
159  OSstream& operator()
160  (
161  const char* functionName,
162  const char* sourceFileName,
163  const int sourceFileLineNumber = 0
164  );
165 
166  //- Print basic message
167  // \return OSstream for further info.
168  OSstream& operator()
169  (
170  const string& functionName,
171  const char* sourceFileName,
172  const int sourceFileLineNumber = 0
173  );
174 
175  //- Print basic message
176  // \return OSstream for further info.
177  OSstream& operator()
178  (
179  const char* functionName,
180  const char* sourceFileName,
181  const int sourceFileLineNumber,
182  const string& ioFileName,
183  const label ioStartLineNumber = -1,
184  const label ioEndLineNumber = -1
185  );
186 
187  //- Print basic message
188  // \return OSstream for further info.
189  OSstream& operator()
190  (
191  const char* functionName,
192  const char* sourceFileName,
193  const int sourceFileLineNumber,
194  const IOstream&
195  );
196 
197  //- Print basic message
198  // \return OSstream for further info.
199  OSstream& operator()
200  (
201  const char* functionName,
202  const char* sourceFileName,
203  const int sourceFileLineNumber,
204  const dictionary&
205  );
206 
207  //- Convert to OSstream for << operations
208  operator OSstream&();
209 
210  //- Explicitly convert to OSstream for << operations
212  {
213  return operator OSstream&();
214  }
215 };
216 
217 
218 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
219 // Global error declarations: defined in messageStream.C
220 
221 //- Global for selective suppression of Info output.
222 // This is normally accessed implicitly via the DetailInfo macro and is often
223 // associated applications with suppressed banners. For example,
224 //
225 // \code
226 // DetailInfo << "Hello, I'm running from program xyz" << nl;
227 // Info<< "Found ... invalid items" << nl;
228 // \endcode
229 //
230 // The values are normally 0 or a positive value.
231 // \note This flag is initialized to 1 by default.
232 extern int infoDetailLevel;
233 
234 //- Information stream (uses stdout - output is on the master only)
235 extern messageStream Info;
236 
237 //- Information stream (uses stderr - output is on the master only)
238 extern messageStream InfoErr;
239 
240 //- Warning stream (uses stdout - output is on the master only),
241 //- with additional 'FOAM Warning' header text.
242 extern messageStream Warning;
243 
244 //- Error stream (uses stdout - output on all processes),
245 //- with additional 'FOAM Serious Error' header text.
246 extern messageStream SeriousError;
247 
248 
249 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
250 
251 } // End namespace Foam
252 
253 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
254 
255 #include "OSstream.H"
256 
257 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
258 // Convenience macros to add the file name and line number to the function name
259 
260 // Compiler provided function name string:
261 // for gcc-compatible compilers use __PRETTY_FUNCTION__
262 // otherwise use the standard __func__
263 #ifdef __GNUC__
264  #define FUNCTION_NAME __PRETTY_FUNCTION__
265 #else
266  #define FUNCTION_NAME __func__
267 #endif
268 
269 
270 //- Report an error message using Foam::SeriousError
271 // for functionName in file __FILE__ at line __LINE__
272 #define SeriousErrorIn(functionName) \
273  ::Foam::SeriousError((functionName), __FILE__, __LINE__)
274 
275 //- Report an error message using Foam::SeriousError
276 // for FUNCTION_NAME in file __FILE__ at line __LINE__
277 #define SeriousErrorInFunction SeriousErrorIn(FUNCTION_NAME)
278 
279 
280 //- Report an IO error message using Foam::SeriousError
281 // for functionName in file __FILE__ at line __LINE__
282 // for a particular IOstream
283 #define SeriousIOErrorIn(functionName, ios) \
284  ::Foam::SeriousError((functionName), __FILE__, __LINE__, ios)
285 
286 //- Report an IO error message using Foam::SeriousError
287 // for FUNCTION_NAME in file __FILE__ at line __LINE__
288 // for a particular IOstream
289 #define SeriousIOErrorInFunction(ios) SeriousIOErrorIn(FUNCTION_NAME, ios)
290 
291 
292 //- Report a warning using Foam::Warning
293 // for functionName in file __FILE__ at line __LINE__
294 #define WarningIn(functionName) \
295  ::Foam::Warning((functionName), __FILE__, __LINE__)
296 
297 //- Report a warning using Foam::Warning
298 // for FUNCTION_NAME in file __FILE__ at line __LINE__
299 #define WarningInFunction WarningIn(FUNCTION_NAME)
300 
301 
302 //- Report an IO warning using Foam::Warning
303 // for functionName in file __FILE__ at line __LINE__
304 // for a particular IOstream
305 #define IOWarningIn(functionName, ios) \
306  ::Foam::Warning((functionName), __FILE__, __LINE__, (ios))
307 
308 //- Report an IO warning using Foam::Warning
309 // for FUNCTION_NAME in file __FILE__ at line __LINE__
310 // for a particular IOstream
311 #define IOWarningInFunction(ios) IOWarningIn(FUNCTION_NAME, ios)
312 
313 
314 //- Report an information message using Foam::Info
315 // for functionName in file __FILE__ at line __LINE__
316 #define InfoIn(functionName) \
317  ::Foam::Info((functionName), __FILE__, __LINE__)
318 
319 //- Report an information message using Foam::Info
320 // for FUNCTION_NAME in file __FILE__ at line __LINE__
321 #define InfoInFunction InfoIn(FUNCTION_NAME)
322 
323 //- Report an information message using Foam::Pout
324 // for functionName in file __FILE__ at line __LINE__
325 #define PoutIn(functionName) \
326  ::Foam::Pout((functionName), __FILE__, __LINE__)
327 
328 //- Report an information message using Foam::Pout
329 // for FUNCTION_NAME in file __FILE__ at line __LINE__
330 #define PoutInFunction PoutIn(FUNCTION_NAME)
331 
332 //- Write to Foam::Info if the Foam::infoDetailLevel is +ve non-zero (default)
333 #define DetailInfo \
334  if (::Foam::infoDetailLevel > 0) Info
335 
336 //- Report write to Foam::Info if the local log switch is true
337 #define Log \
338  if (log) Info
339 
340 
341 //- Report an IO information message using Foam::Info
342 // for functionName in file __FILE__ at line __LINE__
343 // for a particular IOstream
344 #define IOInfoIn(functionName, ios) \
345  ::Foam::Info((functionName), __FILE__, __LINE__, (ios))
346 
347 //- Report an IO information message using Foam::Info
348 // for FUNCTION_NAME in file __FILE__ at line __LINE__
349 // for a particular IOstream
350 #define IOInfoInFunction(ios) IOInfoIn(FUNCTION_NAME, ios)
351 
352 
353 //- Report an information message using Foam::Info
354 // if the local debug switch is true
355 #define DebugInfo \
356  if (debug) Info
357 
358 //- Report an information message using Foam::Info
359 // for FUNCTION_NAME in file __FILE__ at line __LINE__
360 // if the local debug switch is true
361 #define DebugInFunction \
362  if (debug) InfoInFunction
363 
364 //- Report an information message using Foam::Pout
365 // if the local debug switch is true
366 #define DebugPout \
367  if (debug) Pout
368 
369 //- Report an information message using Foam::Pout
370 // for FUNCTION_NAME in file __FILE__ at line __LINE__
371 // if the local debug switch is true
372 #define DebugPoutInFunction \
373  if (debug) PoutInFunction
374 
375 //- Report a variable name and value
376 // using Foam::Pout in file __FILE__ at line __LINE__
377 #define DebugVar(var) \
378 { \
379  ::Foam::string oldPrefix(::Foam::Pout.prefix()); \
380  ::Foam::Pout<< "["<< __FILE__ << ":" << __LINE__ << "] "; \
381  ::Foam::Pout.prefix() = oldPrefix + #var " "; \
382  ::Foam::Pout<< var << ::Foam::endl; \
383  ::Foam::Pout.prefix() = oldPrefix; \
384 }
385 
386 
387 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
388 
389 #endif
390 
391 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::messageStream::WARNING
Warning of possible problem.
Definition: messageStream.H:79
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:320
InfoIn
#define InfoIn(functionName)
Report an information message using Foam::Info.
Definition: messageStream.H:315
Foam::messageStream
Class to handle messaging in a simple, consistent stream-based manner.
Definition: messageStream.H:71
if
if(patchID !=-1)
Definition: boundaryProcessorFaPatchPoints.H:35
Foam::messageStream::errorSeverity
errorSeverity
Message type, or error severity flags.
Definition: messageStream.H:76
Foam::messageStream::masterStream
OSstream & masterStream(const label communicator)
Convert to OSstream.
Definition: messageStream.C:69
Foam::Warning
messageStream Warning
Foam::messageStream::level
static int level
Control the output verbosity of messageStream.
Definition: messageStream.H:107
Foam::messageStream::title_
string title_
Definition: messageStream.H:90
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:75
SeriousIOErrorIn
#define SeriousIOErrorIn(functionName, ios)
Report an IO error message using Foam::SeriousError.
Definition: messageStream.H:282
string.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:73
Foam::messageStream::operator()
OSstream & operator()()
Explicitly convert to OSstream for << operations.
Definition: messageStream.H:210
Foam::Pout
prefixOSstream Pout
An Ostream wrapper for parallel output to std::cout.
Foam::InfoErr
messageStream InfoErr
Information stream (uses stderr - output is on the master only)
Foam::messageStream::maxErrors_
int maxErrors_
Definition: messageStream.H:92
SeriousErrorIn
#define SeriousErrorIn(functionName)
Report an error message using Foam::SeriousError.
Definition: messageStream.H:271
PoutIn
#define PoutIn(functionName)
Report an information message using Foam::Pout.
Definition: messageStream.H:324
Foam::messageStream::maxErrors
int & maxErrors()
Definition: messageStream.H:140
Foam::messageStream::errorCount_
int errorCount_
Definition: messageStream.H:93
IOWarningIn
#define IOWarningIn(functionName, ios)
Report an IO warning using Foam::Warning.
Definition: messageStream.H:304
Foam::messageStream::severity_
errorSeverity severity_
Definition: messageStream.H:91
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
IOInfoIn
#define IOInfoIn(functionName, ios)
Report an IO information message using Foam::Info.
Definition: messageStream.H:343
Foam::messageStream::maxErrors
int maxErrors() const
The maximum number of errors before program termination.
Definition: messageStream.H:133
Foam::OSstream
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:54
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::infoDetailLevel
int infoDetailLevel
Global for selective suppression of Info output.
PoutInFunction
#define PoutInFunction
Report an information message using Foam::Pout.
Definition: messageStream.H:329
Foam::messageStream::FATAL
A fatal error.
Definition: messageStream.H:81
Foam::SeriousError
messageStream SeriousError
label.H
Foam::messageStream::SERIOUS
A serious problem - eg, data corruption.
Definition: messageStream.H:80
Foam::messageStream::title
const string & title() const
The title of this error type.
Definition: messageStream.H:127
WarningIn
#define WarningIn(functionName)
Report a warning using Foam::Warning.
Definition: messageStream.H:293
Foam::messageStream::messageStream
messageStream(const string &title, const errorSeverity severity, const int maxErrors=0)
Construct from components.
Definition: messageStream.C:45
Foam::messageStream::INFO
General information output.
Definition: messageStream.H:78
Foam::messageStream::INFO_STDERR
Information, but on stderr.
Definition: messageStream.H:82
DebugVar
#define DebugVar(var)
Report a variable name and value.
Definition: messageStream.H:376
Foam::prefixOSstream::prefix
const string & prefix() const
Return the stream prefix.
Definition: prefixOSstream.H:101
OSstream.H