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 Foam_messageStream_H
54 #define Foam_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
80  {
81  // Serial-only output:
82  INFO = 1,
83  INFO_STDERR,
85 
86  // Parallel-aware output:
87  SERIOUS,
88  FATAL
89  };
90 
91 
92 protected:
93 
94  // Protected Data
95 
96  string title_;
98  int maxErrors_;
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);
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.
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 //- Report write to Foam::Info if the class log switch is true
369 #define Log_ \
370  if (this->log) Info
371 
372 
373 //- Report an IO information message using Foam::Info
374 // for functionName in file __FILE__ at line __LINE__
375 // for a particular IOstream
376 #define IOInfoIn(functionName, ios) \
377  ::Foam::Info((functionName), __FILE__, __LINE__, (ios))
378 
379 //- Report an IO information message using Foam::Info
380 // for FUNCTION_NAME in file __FILE__ at line __LINE__
381 // for a particular IOstream
382 #define IOInfoInFunction(ios) IOInfoIn(FUNCTION_NAME, ios)
383 
385 //- Report an information message using Foam::Info
386 // if the local debug switch is true
387 #define DebugInfo \
388  if (debug) Info
389 
390 //- Report an information message using Foam::Info
391 // for FUNCTION_NAME in file __FILE__ at line __LINE__
392 // if the local debug switch is true
393 #define DebugInFunction \
394  if (debug) InfoInFunction
395 
396 //- Report an information message using Foam::Pout
397 // if the local debug switch is true
398 #define DebugPout \
399  if (debug) Pout
401 //- Report an information message using Foam::Pout
402 // for FUNCTION_NAME in file __FILE__ at line __LINE__
403 // if the local debug switch is true
404 #define DebugPoutInFunction \
405  if (debug) PoutInFunction
406 
407 //- Report a variable name and value
408 // using Foam::Pout in file __FILE__ at line __LINE__
409 #define DebugVar(var) \
410 { \
411  ::Foam::string oldPrefix(::Foam::Pout.prefix()); \
412  ::Foam::Pout<< "["<< __FILE__ << ":" << __LINE__ << "] "; \
413  ::Foam::Pout.prefix() = oldPrefix + #var " "; \
414  ::Foam::Pout<< var << ::Foam::endl; \
415  ::Foam::Pout.prefix() = oldPrefix; \
416 }
417 
419 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
420 
421 #endif
422 
423 // ************************************************************************* //
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:76
A list of keyword definitions, which are a keyword followed by a number of values (eg...
Definition: dictionary.H:120
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:84
Handle output messages in a simple, consistent stream-based manner.
Definition: messageStream.H:69
static int redirect
The output redirection of messages.
errorSeverity severity_
Definition: messageStream.H:94
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:67
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:75
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:81
messageStream(const string &title, const errorSeverity severity, const int maxErrors=0)
Construct from components.
Definition: messageStream.C:43
General information output (stderr)
Definition: messageStream.H:80
General information output (stdout)
Definition: messageStream.H:79
static int level
The output level (verbosity) of messages.
OSstream & operator()()
Explicitly convert to OSstream for << operations.
Namespace for OpenFOAM.