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