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-2021 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  Handle output messages in a simple, consistent stream-based manner.
32 
33  The messageStream class is globally instantiated with a title
34  string and a severity (which controls the program termination),
35  optionally with a max number of errors before termination.
36 
37  Errors, messages and other data are sent to the messageStream class in
38  the standard manner.
39 
40  For parallel applications, the output for 'standard' messages
41  (Info, Warnings) is effectively suppressed on all sub-processes,
42  which results in a single output message instead of a flood of output
43  messages from each process. The error type of messages do, however,
44  retain output on all processes, which ensures that parallel termination
45  occurs correctly and the source of the problem is properly traceable to
46  the originating processor.
47 
48 SourceFiles
49  messageStream.C
50 
51 \*---------------------------------------------------------------------------*/
52 
53 #ifndef messageStream_H
54 #define messageStream_H
55 
56 #include "label.H"
57 #include "string.H"
58 #include <iostream>
59 
60 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
61 
62 namespace Foam
63 {
64 
65 // Forward Declarations
66 class IOstream;
67 class OSstream;
68 class dictionary;
69 
70 /*---------------------------------------------------------------------------*\
71  Class messageStream Declaration
72 \*---------------------------------------------------------------------------*/
73 
74 class messageStream
75 {
76 public:
77 
78  //- Message type, error severity flags
79  enum errorSeverity
80  {
81  // Serial-only output:
82  INFO = 1,
84  WARNING,
85 
86  // Parallel-aware output:
88  FATAL
89  };
90 
91 
92 protected:
93 
94  // Protected Data
95 
96  string title_;
99  int errorCount_;
100 
101 
102 public:
103 
104  // Static Data
105 
106  //- The output level (verbosity) of messages
107  //
108  // - level == 0 : suppress all output
109  // - level == 1 : normal output
110  // - level >= 2 : report source file name and line number if available
111  //
112  // \note The default level is normally 2.
113  static int level;
114 
115  //- The output redirection of messages
116  //
117  // - redirect == 2 : use stderr instead of stdout
118  static int redirect;
119 
120 
121  // Constructors
122 
123  //- Construct from components
125  (
126  const string& title,
127  const errorSeverity severity,
128  const int maxErrors = 0
129  );
130 
131  //- Construct as Fatal from dictionary, extracting the 'title'.
132  explicit messageStream(const dictionary& dict);
133 
134 
135  // Member Functions
136 
137  //- The title of this error type
138  const string& title() const noexcept
139  {
140  return title_;
141  }
142 
143  //- The maximum number of errors before program termination
144  int maxErrors() const noexcept
145  {
146  return maxErrors_;
147  }
148 
149  //- Set the maximum number of errors before program termination
150  // \return the previous value for maxErrors
151  int maxErrors(int nErrors) noexcept
152  {
153  int old = maxErrors_;
154  maxErrors_ = nErrors;
155  return old;
156  }
157 
158 
159  // Output
160 
161  //- Return OSstream for output operations.
162  //- Use the \c alternative stream for serial-only output
163  //- if it is a valid pointer.
164  OSstream& stream(OSstream* alternative = nullptr);
165 
166  //- Return OSstream for output operations on the master process only,
167  //- Snull on other processes.
168  OSstream& masterStream(const label communicator);
169 
170  //- Return std::ostream for output operations.
171  std::ostream& stdStream();
172 
173  //- Implicit cast to OSstream for << operations
174  operator OSstream&()
175  {
176  return this->stream();
177  }
178 
179  //- Explicitly convert to OSstream for << operations
181  {
182  return this->stream();
183  }
184 
185  //- Report 'From function-name'
186  // \return OSstream for further operations
187  OSstream& operator()
188  (
189  const string& functionName
190  );
191 
192  //- Report 'From function-name, source file, line number'
193  //- Print basic message
194  // \return OSstream for further operations
195  OSstream& operator()
196  (
197  const char* functionName,
198  const char* sourceFileName,
199  const int sourceFileLineNumber = 0
200  );
201 
202  //- Report 'From function-name, source file, line number'
203  // \return OSstream for further operations
204  OSstream& operator()
205  (
206  const string& functionName,
207  const char* sourceFileName,
208  const int sourceFileLineNumber = 0
209  );
210 
211  //- Report 'From function-name, source file, line number'
212  //- as well as io-file name and location
213  // \return OSstream for further operations
214  OSstream& operator()
215  (
216  const char* functionName,
217  const char* sourceFileName,
218  const int sourceFileLineNumber,
219  const string& ioFileName,
220  const label ioStartLineNumber = -1,
221  const label ioEndLineNumber = -1
222  );
223 
224  //- Report 'From function-name, source file, line number'
225  //- as well as io-file name and location
226  // \return OSstream for further operations
227  OSstream& operator()
228  (
229  const char* functionName,
230  const char* sourceFileName,
231  const int sourceFileLineNumber,
232  const IOstream&
233  );
234 
235  //- Report 'From function-name, source file, line number'
236  //- as well as io-file name and location
237  // \return OSstream for further operations
238  OSstream& operator()
239  (
240  const char* functionName,
241  const char* sourceFileName,
242  const int sourceFileLineNumber,
243  const dictionary&
244  );
245 };
246 
247 
248 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
249 // Global error declarations: defined in messageStream.C
250 
251 //- Global for selective suppression of Info output.
252 // This is normally accessed implicitly via the DetailInfo macro and is often
253 // associated applications with suppressed banners. For example,
254 //
255 // \code
256 // DetailInfo << "Hello, I'm running from program xyz" << nl;
257 // Info<< "Found ... invalid items" << nl;
258 // \endcode
259 //
260 // The values are normally 0 or a positive value.
261 // \note This flag is initialized to 1 by default.
262 extern int infoDetailLevel;
263 
264 //- Information stream (stdout output on master, null elsewhere)
265 extern messageStream Info;
266 
267 //- Information stream (stderr output on master, null elsewhere)
268 extern messageStream InfoErr;
269 
270 //- Warning stream (stdout output on master, null elsewhere),
271 //- with additional 'FOAM Warning' header text.
272 extern messageStream Warning;
273 
274 //- Error stream (stdout output on all processes),
275 //- with additional 'FOAM Serious Error' header text.
276 extern messageStream SeriousError;
277 
278 
279 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
280 
281 } // End namespace Foam
282 
283 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
284 
285 #include "OSstream.H"
286 
287 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
288 // Convenience macros to add the file name and line number to the function name
289 
290 // Compiler provided function name string:
291 // for gcc-compatible compilers use __PRETTY_FUNCTION__
292 // otherwise use the standard __func__
293 #ifdef __GNUC__
294  #define FUNCTION_NAME __PRETTY_FUNCTION__
295 #else
296  #define FUNCTION_NAME __func__
297 #endif
298 
299 
300 //- Report an error message using Foam::SeriousError
301 // for functionName in file __FILE__ at line __LINE__
302 #define SeriousErrorIn(functionName) \
303  ::Foam::SeriousError((functionName), __FILE__, __LINE__)
304 
305 //- Report an error message using Foam::SeriousError
306 // for FUNCTION_NAME in file __FILE__ at line __LINE__
307 #define SeriousErrorInFunction SeriousErrorIn(FUNCTION_NAME)
308 
309 
310 //- Report an IO error message using Foam::SeriousError
311 // for functionName in file __FILE__ at line __LINE__
312 // for a particular IOstream
313 #define SeriousIOErrorIn(functionName, ios) \
314  ::Foam::SeriousError((functionName), __FILE__, __LINE__, ios)
315 
316 //- Report an IO error message using Foam::SeriousError
317 // for FUNCTION_NAME in file __FILE__ at line __LINE__
318 // for a particular IOstream
319 #define SeriousIOErrorInFunction(ios) SeriousIOErrorIn(FUNCTION_NAME, ios)
320 
321 
322 //- Report a warning using Foam::Warning
323 // for functionName in file __FILE__ at line __LINE__
324 #define WarningIn(functionName) \
325  ::Foam::Warning((functionName), __FILE__, __LINE__)
326 
327 //- Report a warning using Foam::Warning
328 // for FUNCTION_NAME in file __FILE__ at line __LINE__
329 #define WarningInFunction WarningIn(FUNCTION_NAME)
330 
331 
332 //- Report an IO warning using Foam::Warning
333 // for functionName in file __FILE__ at line __LINE__
334 // for a particular IOstream
335 #define IOWarningIn(functionName, ios) \
336  ::Foam::Warning((functionName), __FILE__, __LINE__, (ios))
337 
338 //- Report an IO warning using Foam::Warning
339 // for FUNCTION_NAME in file __FILE__ at line __LINE__
340 // for a particular IOstream
341 #define IOWarningInFunction(ios) IOWarningIn(FUNCTION_NAME, ios)
342 
343 
344 //- Report an information message using Foam::Info
345 // for functionName in file __FILE__ at line __LINE__
346 #define InfoIn(functionName) \
347  ::Foam::Info((functionName), __FILE__, __LINE__)
348 
349 //- Report an information message using Foam::Info
350 // for FUNCTION_NAME in file __FILE__ at line __LINE__
351 #define InfoInFunction InfoIn(FUNCTION_NAME)
352 
353 //- Report using Foam::Pout with functionName: prefix
354 #define PoutIn(functionName) \
355  ::Foam::Pout << (functionName) << ':'
356 
357 //- Report using Foam::Pout with FUNCTION_NAME prefix
358 #define PoutInFunction PoutIn(FUNCTION_NAME)
359 
360 //- Write to Foam::Info if the Foam::infoDetailLevel is +ve non-zero (default)
361 #define DetailInfo \
362  if (::Foam::infoDetailLevel > 0) Info
363 
364 //- Report write to Foam::Info if the local log switch is true
365 #define Log \
366  if (log) Info
367 
368 
369 //- Report an IO information message using Foam::Info
370 // for functionName in file __FILE__ at line __LINE__
371 // for a particular IOstream
372 #define IOInfoIn(functionName, ios) \
373  ::Foam::Info((functionName), __FILE__, __LINE__, (ios))
374 
375 //- Report an IO information message using Foam::Info
376 // for FUNCTION_NAME in file __FILE__ at line __LINE__
377 // for a particular IOstream
378 #define IOInfoInFunction(ios) IOInfoIn(FUNCTION_NAME, ios)
379 
380 
381 //- Report an information message using Foam::Info
382 // if the local debug switch is true
383 #define DebugInfo \
384  if (debug) Info
385 
386 //- Report an information message using Foam::Info
387 // for FUNCTION_NAME in file __FILE__ at line __LINE__
388 // if the local debug switch is true
389 #define DebugInFunction \
390  if (debug) InfoInFunction
391 
392 //- Report an information message using Foam::Pout
393 // if the local debug switch is true
394 #define DebugPout \
395  if (debug) Pout
396 
397 //- Report an information message using Foam::Pout
398 // for FUNCTION_NAME in file __FILE__ at line __LINE__
399 // if the local debug switch is true
400 #define DebugPoutInFunction \
401  if (debug) PoutInFunction
402 
403 //- Report a variable name and value
404 // using Foam::Pout in file __FILE__ at line __LINE__
405 #define DebugVar(var) \
406 { \
407  ::Foam::string oldPrefix(::Foam::Pout.prefix()); \
408  ::Foam::Pout<< "["<< __FILE__ << ":" << __LINE__ << "] "; \
409  ::Foam::Pout.prefix() = oldPrefix + #var " "; \
410  ::Foam::Pout<< var << ::Foam::endl; \
411  ::Foam::Pout.prefix() = oldPrefix; \
412 }
413 
414 
415 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
416 
417 #endif
418 
419 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::messageStream::WARNING
Warning of possible problem.
Definition: messageStream.H:83
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:350
InfoIn
#define InfoIn(functionName)
Report an information message using Foam::Info.
Definition: messageStream.H:345
Foam::messageStream
Handle output messages in a simple, consistent stream-based manner.
Definition: messageStream.H:73
if
if(patchID !=-1)
Definition: boundaryProcessorFaPatchPoints.H:35
Foam::messageStream::errorSeverity
errorSeverity
Message type, error severity flags.
Definition: messageStream.H:78
Foam::messageStream::masterStream
OSstream & masterStream(const label communicator)
Definition: messageStream.C:140
Foam::Warning
messageStream Warning
Foam::messageStream::level
static int level
The output level (verbosity) of messages.
Definition: messageStream.H:112
Foam::messageStream::redirect
static int redirect
The output redirection of messages.
Definition: messageStream.H:117
Foam::messageStream::title_
string title_
Definition: messageStream.H:95
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:79
SeriousIOErrorIn
#define SeriousIOErrorIn(functionName, ios)
Report an IO error message using Foam::SeriousError.
Definition: messageStream.H:312
string.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:76
Foam::messageStream::operator()
OSstream & operator()()
Explicitly convert to OSstream for << operations.
Definition: messageStream.H:179
Foam::Pout
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Foam::InfoErr
messageStream InfoErr
Information stream (stderr output on master, null elsewhere)
Foam::messageStream::maxErrors_
int maxErrors_
Definition: messageStream.H:97
SeriousErrorIn
#define SeriousErrorIn(functionName)
Report an error message using Foam::SeriousError.
Definition: messageStream.H:301
Foam::messageStream::errorCount_
int errorCount_
Definition: messageStream.H:98
IOWarningIn
#define IOWarningIn(functionName, ios)
Report an IO warning using Foam::Warning.
Definition: messageStream.H:334
Foam::messageStream::stream
OSstream & stream(OSstream *alternative=nullptr)
Definition: messageStream.C:71
Foam::messageStream::severity_
errorSeverity severity_
Definition: messageStream.H:96
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
IOInfoIn
#define IOInfoIn(functionName, ios)
Report an IO information message using Foam::Info.
Definition: messageStream.H:371
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:123
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::infoDetailLevel
int infoDetailLevel
Global for selective suppression of Info output.
PoutInFunction
#define PoutInFunction
Report using Foam::Pout with FUNCTION_NAME prefix.
Definition: messageStream.H:357
Foam::messageStream::FATAL
A fatal error.
Definition: messageStream.H:87
Foam::SeriousError
messageStream SeriousError
Foam::messageStream::maxErrors
int maxErrors() const noexcept
The maximum number of errors before program termination.
Definition: messageStream.H:143
label.H
Foam::messageStream::SERIOUS
A serious problem - eg, data corruption.
Definition: messageStream.H:86
WarningIn
#define WarningIn(functionName)
Report a warning using Foam::Warning.
Definition: messageStream.H:323
Foam::messageStream::messageStream
messageStream(const string &title, const errorSeverity severity, const int maxErrors=0)
Construct from components.
Definition: messageStream.C:47
Foam::messageStream::stdStream
std::ostream & stdStream()
Return std::ostream for output operations.
Definition: messageStream.C:157
Foam::messageStream::INFO
General information output (stdout)
Definition: messageStream.H:81
Foam::messageStream::INFO_STDERR
General information output (stderr)
Definition: messageStream.H:82
DebugVar
#define DebugVar(var)
Report a variable name and value.
Definition: messageStream.H:404
Foam::messageStream::title
const string & title() const noexcept
The title of this error type.
Definition: messageStream.H:137
Foam::prefixOSstream::prefix
const string & prefix() const noexcept
Return the stream prefix.
Definition: prefixOSstream.H:101
OSstream.H