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-2025 OpenCFD Ltd.
10  Copyright (C) 2026 Keysight Technologies
11 -------------------------------------------------------------------------------
12 License
13  This file is part of OpenFOAM.
14 
15  OpenFOAM is free software: you can redistribute it and/or modify it
16  under the terms of the GNU General Public License as published by
17  the Free Software Foundation, either version 3 of the License, or
18  (at your option) any later version.
19 
20  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
21  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23  for more details.
24 
25  You should have received a copy of the GNU General Public License
26  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
27 
28 Class
29  Foam::messageStream
30 
31 Description
32  Handle output messages in a simple, consistent stream-based manner.
33 
34  The messageStream class is globally instantiated with a title
35  string and a severity (which controls the program termination),
36  optionally with a max number of errors before termination.
37 
38  Errors, messages and other data are sent to the messageStream class in
39  the standard manner.
40 
41  For parallel applications, the output for 'standard' messages
42  (Info, Warnings) is effectively suppressed on all sub-processes,
43  which results in a single output message instead of a flood of output
44  messages from each process. The error type of messages do, however,
45  retain output on all processes, which ensures that parallel termination
46  occurs correctly and the source of the problem is properly traceable to
47  the originating processor.
48 
49 SourceFiles
50  messageStream.C
51 
52 \*---------------------------------------------------------------------------*/
53 
54 #ifndef Foam_messageStream_H
55 #define Foam_messageStream_H
56 
57 #include "label.H"
58 #include "word.H"
59 
60 #include <iostream>
61 #include <string>
62 #include <string_view>
63 
64 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
65 
66 namespace Foam
67 {
68 
69 // Forward Declarations
70 class IOstream;
71 class OSstream;
72 class dictionary;
73 
74 /*---------------------------------------------------------------------------*\
75  Class messageStream Declaration
76 \*---------------------------------------------------------------------------*/
77 
78 class messageStream
79 {
80 public:
81 
82  //- Message type, error severity flags
83  enum errorSeverity : int
84  {
85  // Serial-only output:
86  INFO = 1,
88 
89  // Parallel-aware output:
90  SERIOUS,
92 
94  USE_STDERR = 0x80
95  };
96 
97 
98 protected:
99 
100  // Protected Data
101 
102  //- The title of this error type
103  string title_;
104 
105  //- The message type / error severity, possibly with USE_STDERR mask
106  int severity_;
108  //- The maximum number of errors before program termination
109  int maxErrors_;
110 
111  //- The current number of errors counted
113 
114 
115 public:
116 
117  // Static Data
118 
119  //- The output level (verbosity) of messages
120  //
121  // - level == 0 : suppress all output
122  // - level == 1 : normal output
123  // - level >= 2 : report source file name and line number if available
124  //
125  // \note The default level is normally 2.
126  static int level;
127 
128  //- The output redirection of messages
129  //
130  // - redirect == 2 : use stderr instead of stdout
131  static int redirect;
132 
133 
134  // Constructors
135 
136  //- Construct untitled with given characteristics
137  explicit messageStream
138  (
139  errorSeverity severity,
140  int maxErrors = 0,
141  bool use_stderr = false
142  );
143 
144  //- Construct from components
146  (
147  const char* title,
148  errorSeverity severity,
149  int maxErrors = 0,
150  bool use_stderr = false
151  );
152 
153  //- Construct from components
155  (
156  string title,
157  errorSeverity severity,
158  int maxErrors = 0,
159  bool use_stderr = false
160  );
161 
162 
163  //- Construct from dictionary as Fatal, extracting 'title'.
164  explicit messageStream(const dictionary& dict);
165 
166 
167  // Member Functions
168 
169  //- The title of this error type
170  const string& title() const noexcept
171  {
172  return title_;
173  }
174 
175  //- The maximum number of errors before program termination
176  int maxErrors() const noexcept
177  {
178  return maxErrors_;
179  }
180 
181  //- Set the maximum number of errors before program termination
182  // \return the previous value for maxErrors
183  int maxErrors(int nErrors) noexcept
184  {
185  int old(maxErrors_);
186  maxErrors_ = nErrors;
187  return old;
188  }
189 
190 
191  // Output
192 
193  //- Return OSstream for output operations.
195  (
197  OSstream* alternative = nullptr,
199  int communicator = -1
200  );
201 
202  //- Return OSstream for output operations on the master process only,
203  //- Snull on other processes.
204  // A negative communicator is treated like UPstream::worldComm
205  OSstream& masterStream(int communicator);
206 
207  //- Return std::ostream for output operations.
208  std::ostream& stdStream();
210  //- Report deprecation (after specified API version) with
211  //- 'From function-name, source file, line number'.
212  // \return OSstream for further operations
214  (
215  const int afterVersion,
217  std::string_view functionName = std::string_view(),
219  std::string_view sourceFileName = std::string_view(),
221  const int sourceFileLineNumber = 0
222  );
223 
224  //- Implicit cast to OSstream for << operations
225  operator OSstream&()
226  {
227  return this->stream();
228  }
229 
230  //- Explicitly convert to OSstream for << operations
232  {
233  return this->stream();
234  }
235 
236  //- Report 'From function-name',
237  //- optionally with 'source file, line number'
238  // \return OSstream for further operations
239  OSstream& operator()
240  (
242  std::string_view functionName,
244  std::string_view sourceFileName = std::string_view(),
246  const int sourceFileLineNumber = 0
247  );
248 
249  //- Report 'From function-name, source file, line number'
250  //- as well as io-file name and location
251  // \return OSstream for further operations
252  OSstream& operator()
253  (
255  std::string_view functionName,
257  std::string_view sourceFileName,
259  const int sourceFileLineNumber,
260  const std::string& ioFileName,
261  const label ioStartLineNumber = -1,
262  const label ioEndLineNumber = -1
263  );
264 
265  //- Report 'From function-name, source file, line number'
266  //- as well as io-file name and location
267  // \return OSstream for further operations
268  OSstream& operator()
269  (
271  std::string_view functionName,
273  std::string_view sourceFileName,
275  const int sourceFileLineNumber,
277  const IOstream&
278  );
279 
280  //- Report 'From function-name, source file, line number'
281  //- as well as io-file name and location
282  // \return OSstream for further operations
283  OSstream& operator()
284  (
286  std::string_view functionName,
288  std::string_view sourceFileName,
290  const int sourceFileLineNumber,
292  const dictionary& dict
293  );
294 };
295 
296 
297 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
298 // Global error declarations: defined in messageStream.C
299 
300 //- Global for selective suppression of Info output.
301 // This is normally accessed implicitly via the DetailInfo macro and is often
302 // associated with applications with suppressed banners. For example,
303 //
304 // \code
305 // DetailInfo << "Hello, I'm running from program xyz" << nl;
306 // Info<< "Found ... invalid items" << nl;
307 // \endcode
308 //
309 // The values are normally 0 or a positive value.
310 // \note This flag is initialized to 1 by default.
311 extern int infoDetailLevel;
312 
313 //- Information stream (stdout output on master, null elsewhere)
314 extern messageStream Info;
315 
316 //- Information stream (stderr output on master, null elsewhere)
317 extern messageStream InfoErr;
318 
319 //- Warning stream (stdout output on master, null elsewhere),
320 //- with additional 'FOAM Warning' header text.
321 extern messageStream Warning;
322 
323 //- Error stream (stdout output on all processes),
324 //- with additional 'FOAM Serious Error' header text.
326 
327 
328 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
329 
330 } // End namespace Foam
331 
332 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
333 
334 #include "OSstream.H"
335 
336 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
337 // Convenience macros to add the file name and line number to the function name
338 
339 // Compiler provided function name string:
340 // for gcc-compatible compilers use __PRETTY_FUNCTION__
341 // otherwise use the standard __func__
342 #ifdef __GNUC__
343  #define FUNCTION_NAME __PRETTY_FUNCTION__
344 #else
345  #define FUNCTION_NAME __func__
346 #endif
347 
348 
349 //- Report an error message using Foam::SeriousError
350 // for functionName in file __FILE__ at line __LINE__
351 #define SeriousErrorIn(functionName) \
352  ::Foam::SeriousError((functionName), __FILE__, __LINE__)
353 
354 //- Report an error message using Foam::SeriousError
355 // for FUNCTION_NAME in file __FILE__ at line __LINE__
356 #define SeriousErrorInFunction SeriousErrorIn(FUNCTION_NAME)
357 
358 
359 //- Report an IO error message using Foam::SeriousError
360 // for functionName in file __FILE__ at line __LINE__
361 // for a particular IOstream
362 #define SeriousIOErrorIn(functionName, ios) \
363  ::Foam::SeriousError((functionName), __FILE__, __LINE__, ios)
364 
365 //- Report an IO error message using Foam::SeriousError
366 // for FUNCTION_NAME in file __FILE__ at line __LINE__
367 // for a particular IOstream
368 #define SeriousIOErrorInFunction(ios) SeriousIOErrorIn(FUNCTION_NAME, ios)
369 
370 
371 //- Report a warning using Foam::Warning
372 // for functionName in file __FILE__ at line __LINE__
373 #define WarningIn(functionName) \
374  ::Foam::Warning((functionName), __FILE__, __LINE__)
375 
376 //- Report a warning using Foam::Warning
377 // for FUNCTION_NAME in file __FILE__ at line __LINE__
378 #define WarningInFunction WarningIn(FUNCTION_NAME)
379 
380 //- Report a warning using Foam::Warning
381 // for FUNCTION_NAME in file __FILE__ at line __LINE__
382 #define DeprecatedInFunction(afterVersion) \
383  ::Foam::Warning.deprecated(afterVersion, FUNCTION_NAME, __FILE__, __LINE__)
384 
385 
386 //- Report an IO warning using Foam::Warning
387 // for functionName in file __FILE__ at line __LINE__
388 // for a particular IOstream
389 #define IOWarningIn(functionName, ios) \
390  ::Foam::Warning((functionName), __FILE__, __LINE__, (ios))
391 
392 //- Report an IO warning using Foam::Warning
393 // for FUNCTION_NAME in file __FILE__ at line __LINE__
394 // for a particular IOstream
395 #define IOWarningInFunction(ios) IOWarningIn(FUNCTION_NAME, ios)
396 
397 
398 //- Report an information message using Foam::Info
399 // for functionName in file __FILE__ at line __LINE__
400 #define InfoIn(functionName) \
401  ::Foam::Info((functionName), __FILE__, __LINE__)
402 
403 //- Report an information message using Foam::Info
404 // for FUNCTION_NAME in file __FILE__ at line __LINE__
405 #define InfoInFunction InfoIn(FUNCTION_NAME)
406 
407 //- Report using Foam::Pout with functionName: prefix
408 #define PoutIn(functionName) \
409  ::Foam::Pout << (functionName) << ':'
410 
411 //- Report using Foam::Pout with FUNCTION_NAME prefix
412 #define PoutInFunction PoutIn(FUNCTION_NAME)
413 
414 //- Write to Foam::Info if the Foam::infoDetailLevel is +ve non-zero (default)
415 #define DetailInfo \
416  if (::Foam::infoDetailLevel > 0) ::Foam::Info
418 //- Report write to Foam::Info if the local log switch is true
419 #define Log \
420  if (log) ::Foam::Info
421 
422 //- Report write to Foam::Info if the class log switch is true
423 #define Log_ \
424  if (this->log) ::Foam::Info
426 
427 //- Report an IO information message using Foam::Info
428 // for functionName in file __FILE__ at line __LINE__
429 // for a particular IOstream
430 #define IOInfoIn(functionName, ios) \
431  ::Foam::Info((functionName), __FILE__, __LINE__, (ios))
432 
433 //- Report an IO information message using Foam::Info
434 // for FUNCTION_NAME in file __FILE__ at line __LINE__
435 // for a particular IOstream
436 #define IOInfoInFunction(ios) IOInfoIn(FUNCTION_NAME, ios)
437 
438 
439 //- Report an information message using Foam::Info
440 // if the local debug switch is true
441 #define DebugInfo \
442  if (debug) ::Foam::Info
444 //- Report an information message using Foam::Info
445 // for FUNCTION_NAME in file __FILE__ at line __LINE__
446 // if the local debug switch is true
447 #define DebugInFunction \
448  if (debug) InfoInFunction
449 
450 //- Report an information message using Foam::Pout
451 // if the local debug switch is true
452 #define DebugPout \
453  if (debug) ::Foam::Pout
454 
455 //- Report an information message using Foam::Pout
456 // for FUNCTION_NAME in file __FILE__ at line __LINE__
457 // if the local debug switch is true
458 #define DebugPoutInFunction \
459  if (debug) PoutInFunction
460 
461 //- Report a variable name and value
462 // using Foam::Pout in file __FILE__ at line __LINE__
463 #define DebugVar(var) \
464 { \
465  ::Foam::string oldPrefix(::Foam::Pout.prefix()); \
466  ::Foam::Pout<< "["<< __FILE__ << ":" << __LINE__ << "] "; \
467  ::Foam::Pout.prefix() = oldPrefix + #var " "; \
468  ::Foam::Pout<< var << ::Foam::endl; \
469  ::Foam::Pout.prefix() = oldPrefix; \
470 }
471 
472 
473 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
474 
475 #endif
477 // ************************************************************************* //
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
OSstream & deprecated(const int afterVersion, std::string_view functionName=std::string_view(), std::string_view sourceFileName=std::string_view(), const int sourceFileLineNumber=0)
Report deprecation (after specified API version) with &#39;From function-name, source file...
messageStream InfoErr
Information stream (stderr output on master, null elsewhere)
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:130
Warning of possible problem.
Definition: messageStream.H:84
int infoDetailLevel
Global for selective suppression of Info output.
std::ostream & stdStream()
Return std::ostream for output operations.
int errorCount_
The current number of errors counted.
Handle output messages in a simple, consistent stream-based manner.
Definition: messageStream.H:73
static int redirect
The output redirection of messages.
messageStream SeriousError
Error stream (stdout output on all processes), with additional &#39;FOAM Serious Error&#39; header text...
int maxErrors_
The maximum number of errors before program termination.
OSstream & stream(OSstream *alternative=nullptr, int communicator=-1)
Return OSstream for output operations.
const direction noexcept
Definition: scalarImpl.H:265
messageStream(errorSeverity severity, int maxErrors=0, bool use_stderr=false)
Construct untitled with given characteristics.
Definition: messageStream.C:45
messageStream Warning
Warning stream (stdout output on master, null elsewhere), with additional &#39;FOAM Warning&#39; header text...
An IOstream is an abstract base class for all input/output systems; be they streams, files, token lists etc.
Definition: IOstream.H:82
General information output (stdout)
Definition: messageStream.H:83
OSstream & masterStream(int communicator)
Return OSstream for output operations on the master process only, Snull on other processes.
int maxErrors() const noexcept
The maximum number of errors before program termination.
errorSeverity
Message type, error severity flags.
Definition: messageStream.H:80
messageStream Info
Information stream (stdout output on master, null elsewhere)
int severity_
The message type / error severity, possibly with USE_STDERR mask.
A serious problem - eg, data corruption.
Definition: messageStream.H:87
static int level
The output level (verbosity) of messages.
Bitmask for stderr output (for the above enums)
Definition: messageStream.H:91
OSstream & operator()()
Explicitly convert to OSstream for << operations.
string title_
The title of this error type.
Namespace for OpenFOAM.