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