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