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-2020 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  Class to handle messaging in a simple, consistent stream-based
32  manner.
33 
34  The messageStream class is globally instantiated with a title string and
35  a severity (which controls the program termination) and a number of
36  errors before termination. Errors, messages and other data are sent to
37  the messageStream class in the standard manner.
38 
39 Usage
40  \code
41  messageStream
42  << "message1" << "message2" << FoamDataType << endl;
43  \endcode
44 
45 SourceFiles
46  messageStream.C
47 
48 \*---------------------------------------------------------------------------*/
49 
50 #ifndef messageStream_H
51 #define messageStream_H
52 
53 #include "label.H"
54 #include "string.H"
55 
56 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
57 
58 namespace Foam
59 {
60 
61 // Forward Declarations
62 class IOstream;
63 class Ostream;
64 class OSstream;
65 class OStringStream;
66 class dictionary;
67 
68 /*---------------------------------------------------------------------------*\
69  Class messageStream Declaration
70 \*---------------------------------------------------------------------------*/
71 
72 class messageStream
73 {
74 public:
75 
76  //- Message type, or error severity flags
77  enum errorSeverity
78  {
79  INFO = 1,
83  INFO_STDERR = INFO | 0x10,
84  };
85 
86 
87 protected:
88 
89  // Protected Data
90 
91  string title_;
94  int errorCount_;
95 
96 
97 public:
98 
99  // Static Data
100 
101  //- The output level (verbosity) of messages
102  //
103  // - level == 0 : suppress all output
104  // - level == 1 : normal output
105  // - level >= 2 : report source file name and line number if available
106  //
107  // \note The default level is normally 2.
108  static int level;
109 
110  //- The output redirection of messages
111  //
112  // - redirect == 2 : use stderr instead of stdout
113  static int redirect;
114 
115 
116  // Constructors
117 
118  //- Construct from components
120  (
121  const string& title,
122  const errorSeverity severity,
123  const int maxErrors = 0
124  );
125 
126  //- Construct as Fatal from dictionary, extracting the 'title'.
127  explicit messageStream(const dictionary& dict);
128 
129 
130  // Member functions
131 
132  //- The title of this error type
133  const string& title() const
134  {
135  return title_;
136  }
137 
138  //- The maximum number of errors before program termination
139  int maxErrors() const
140  {
141  return maxErrors_;
142  }
143 
144  //- Non-const access to the maximum number of errors before
145  //- program termination to enable user to reset it
146  int& maxErrors()
147  {
148  return maxErrors_;
149  }
150 
151  //- Convert to OSstream
152  // Prints to Pout for the master stream
153  OSstream& masterStream(const label communicator);
154 
155  //- Print basic message
156  // \return OSstream for further info.
157  OSstream& operator()
158  (
159  const string& functionName
160  );
161 
162  //- Print basic message
163  // \return OSstream for further info.
164  OSstream& operator()
165  (
166  const char* functionName,
167  const char* sourceFileName,
168  const int sourceFileLineNumber = 0
169  );
170 
171  //- Print basic message
172  // \return OSstream for further info.
173  OSstream& operator()
174  (
175  const string& functionName,
176  const char* sourceFileName,
177  const int sourceFileLineNumber = 0
178  );
179 
180  //- Print basic message
181  // \return OSstream for further info.
182  OSstream& operator()
183  (
184  const char* functionName,
185  const char* sourceFileName,
186  const int sourceFileLineNumber,
187  const string& ioFileName,
188  const label ioStartLineNumber = -1,
189  const label ioEndLineNumber = -1
190  );
191 
192  //- Print basic message
193  // \return OSstream for further info.
194  OSstream& operator()
195  (
196  const char* functionName,
197  const char* sourceFileName,
198  const int sourceFileLineNumber,
199  const IOstream&
200  );
201 
202  //- Print basic message
203  // \return OSstream for further info.
204  OSstream& operator()
205  (
206  const char* functionName,
207  const char* sourceFileName,
208  const int sourceFileLineNumber,
209  const dictionary&
210  );
211 
212  //- Convert to OSstream for << operations
213  operator OSstream&();
214 
215  //- Explicitly convert to OSstream for << operations
217  {
218  return operator OSstream&();
219  }
220 };
221 
222 
223 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
224 // Global error declarations: defined in messageStream.C
225 
226 //- Global for selective suppression of Info output.
227 // This is normally accessed implicitly via the DetailInfo macro and is often
228 // associated applications with suppressed banners. For example,
229 //
230 // \code
231 // DetailInfo << "Hello, I'm running from program xyz" << nl;
232 // Info<< "Found ... invalid items" << nl;
233 // \endcode
234 //
235 // The values are normally 0 or a positive value.
236 // \note This flag is initialized to 1 by default.
237 extern int infoDetailLevel;
238 
239 //- Information stream (uses stdout - output is on the master only)
240 extern messageStream Info;
241 
242 //- Information stream (uses stderr - output is on the master only)
243 extern messageStream InfoErr;
244 
245 //- Warning stream (uses stdout - output is on the master only),
246 //- with additional 'FOAM Warning' header text.
247 extern messageStream Warning;
248 
249 //- Error stream (uses stdout - output on all processes),
250 //- with additional 'FOAM Serious Error' header text.
251 extern messageStream SeriousError;
252 
253 
254 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
255 
256 } // End namespace Foam
257 
258 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
259 
260 #include "OSstream.H"
261 
262 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
263 // Convenience macros to add the file name and line number to the function name
264 
265 // Compiler provided function name string:
266 // for gcc-compatible compilers use __PRETTY_FUNCTION__
267 // otherwise use the standard __func__
268 #ifdef __GNUC__
269  #define FUNCTION_NAME __PRETTY_FUNCTION__
270 #else
271  #define FUNCTION_NAME __func__
272 #endif
273 
274 
275 //- Report an error message using Foam::SeriousError
276 // for functionName in file __FILE__ at line __LINE__
277 #define SeriousErrorIn(functionName) \
278  ::Foam::SeriousError((functionName), __FILE__, __LINE__)
279 
280 //- Report an error message using Foam::SeriousError
281 // for FUNCTION_NAME in file __FILE__ at line __LINE__
282 #define SeriousErrorInFunction SeriousErrorIn(FUNCTION_NAME)
283 
284 
285 //- Report an IO error message using Foam::SeriousError
286 // for functionName in file __FILE__ at line __LINE__
287 // for a particular IOstream
288 #define SeriousIOErrorIn(functionName, ios) \
289  ::Foam::SeriousError((functionName), __FILE__, __LINE__, ios)
290 
291 //- Report an IO error message using Foam::SeriousError
292 // for FUNCTION_NAME in file __FILE__ at line __LINE__
293 // for a particular IOstream
294 #define SeriousIOErrorInFunction(ios) SeriousIOErrorIn(FUNCTION_NAME, ios)
295 
296 
297 //- Report a warning using Foam::Warning
298 // for functionName in file __FILE__ at line __LINE__
299 #define WarningIn(functionName) \
300  ::Foam::Warning((functionName), __FILE__, __LINE__)
301 
302 //- Report a warning using Foam::Warning
303 // for FUNCTION_NAME in file __FILE__ at line __LINE__
304 #define WarningInFunction WarningIn(FUNCTION_NAME)
305 
306 
307 //- Report an IO warning using Foam::Warning
308 // for functionName in file __FILE__ at line __LINE__
309 // for a particular IOstream
310 #define IOWarningIn(functionName, ios) \
311  ::Foam::Warning((functionName), __FILE__, __LINE__, (ios))
312 
313 //- Report an IO warning using Foam::Warning
314 // for FUNCTION_NAME in file __FILE__ at line __LINE__
315 // for a particular IOstream
316 #define IOWarningInFunction(ios) IOWarningIn(FUNCTION_NAME, ios)
317 
318 
319 //- Report an information message using Foam::Info
320 // for functionName in file __FILE__ at line __LINE__
321 #define InfoIn(functionName) \
322  ::Foam::Info((functionName), __FILE__, __LINE__)
323 
324 //- Report an information message using Foam::Info
325 // for FUNCTION_NAME in file __FILE__ at line __LINE__
326 #define InfoInFunction InfoIn(FUNCTION_NAME)
327 
328 //- Report an information message using Foam::Pout
329 // for functionName in file __FILE__ at line __LINE__
330 #define PoutIn(functionName) \
331  ::Foam::Pout((functionName), __FILE__, __LINE__)
332 
333 //- Report an information message using Foam::Pout
334 // for FUNCTION_NAME in file __FILE__ at line __LINE__
335 #define PoutInFunction PoutIn(FUNCTION_NAME)
336 
337 //- Write to Foam::Info if the Foam::infoDetailLevel is +ve non-zero (default)
338 #define DetailInfo \
339  if (::Foam::infoDetailLevel > 0) Info
340 
341 //- Report write to Foam::Info if the local log switch is true
342 #define Log \
343  if (log) Info
344 
345 
346 //- Report an IO information message using Foam::Info
347 // for functionName in file __FILE__ at line __LINE__
348 // for a particular IOstream
349 #define IOInfoIn(functionName, ios) \
350  ::Foam::Info((functionName), __FILE__, __LINE__, (ios))
351 
352 //- Report an IO information message using Foam::Info
353 // for FUNCTION_NAME in file __FILE__ at line __LINE__
354 // for a particular IOstream
355 #define IOInfoInFunction(ios) IOInfoIn(FUNCTION_NAME, ios)
356 
357 
358 //- Report an information message using Foam::Info
359 // if the local debug switch is true
360 #define DebugInfo \
361  if (debug) Info
362 
363 //- Report an information message using Foam::Info
364 // for FUNCTION_NAME in file __FILE__ at line __LINE__
365 // if the local debug switch is true
366 #define DebugInFunction \
367  if (debug) InfoInFunction
368 
369 //- Report an information message using Foam::Pout
370 // if the local debug switch is true
371 #define DebugPout \
372  if (debug) Pout
373 
374 //- Report an information message using Foam::Pout
375 // for FUNCTION_NAME in file __FILE__ at line __LINE__
376 // if the local debug switch is true
377 #define DebugPoutInFunction \
378  if (debug) PoutInFunction
379 
380 //- Report a variable name and value
381 // using Foam::Pout in file __FILE__ at line __LINE__
382 #define DebugVar(var) \
383 { \
384  ::Foam::string oldPrefix(::Foam::Pout.prefix()); \
385  ::Foam::Pout<< "["<< __FILE__ << ":" << __LINE__ << "] "; \
386  ::Foam::Pout.prefix() = oldPrefix + #var " "; \
387  ::Foam::Pout<< var << ::Foam::endl; \
388  ::Foam::Pout.prefix() = oldPrefix; \
389 }
390 
391 
392 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
393 
394 #endif
395 
396 // ************************************************************************* //
Foam::expressions::patchExpr::debug
int debug
Static debugging option.
Foam::messageStream::WARNING
Warning of possible problem.
Definition: messageStream.H:79
InfoInFunction
#define InfoInFunction
Report an information message using Foam::Info.
Definition: messageStream.H:325
InfoIn
#define InfoIn(functionName)
Report an information message using Foam::Info.
Definition: messageStream.H:320
Foam::messageStream
Class to handle messaging in a simple, consistent stream-based manner.
Definition: messageStream.H:71
if
if(patchID !=-1)
Definition: boundaryProcessorFaPatchPoints.H:35
Foam::messageStream::errorSeverity
errorSeverity
Message type, or error severity flags.
Definition: messageStream.H:76
Foam::messageStream::masterStream
OSstream & masterStream(const label communicator)
Convert to OSstream.
Definition: messageStream.C:71
Foam::Warning
messageStream Warning
Foam::messageStream::level
static int level
The output level (verbosity) of messages.
Definition: messageStream.H:107
Foam::messageStream::redirect
static int redirect
The output redirection of messages.
Definition: messageStream.H:112
Foam::messageStream::title_
string title_
Definition: messageStream.H:90
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:75
SeriousIOErrorIn
#define SeriousIOErrorIn(functionName, ios)
Report an IO error message using Foam::SeriousError.
Definition: messageStream.H:287
string.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:73
Foam::messageStream::operator()
OSstream & operator()()
Explicitly convert to OSstream for << operations.
Definition: messageStream.H:215
Foam::Pout
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Foam::InfoErr
messageStream InfoErr
Information stream (uses stderr - output is on the master only)
Foam::messageStream::maxErrors_
int maxErrors_
Definition: messageStream.H:92
SeriousErrorIn
#define SeriousErrorIn(functionName)
Report an error message using Foam::SeriousError.
Definition: messageStream.H:276
PoutIn
#define PoutIn(functionName)
Report an information message using Foam::Pout.
Definition: messageStream.H:329
Foam::messageStream::maxErrors
int & maxErrors()
Definition: messageStream.H:145
Foam::messageStream::errorCount_
int errorCount_
Definition: messageStream.H:93
IOWarningIn
#define IOWarningIn(functionName, ios)
Report an IO warning using Foam::Warning.
Definition: messageStream.H:309
Foam::messageStream::severity_
errorSeverity severity_
Definition: messageStream.H:91
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
IOInfoIn
#define IOInfoIn(functionName, ios)
Report an IO information message using Foam::Info.
Definition: messageStream.H:348
Foam::messageStream::maxErrors
int maxErrors() const
The maximum number of errors before program termination.
Definition: messageStream.H:138
Foam::OSstream
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:54
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:121
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::infoDetailLevel
int infoDetailLevel
Global for selective suppression of Info output.
PoutInFunction
#define PoutInFunction
Report an information message using Foam::Pout.
Definition: messageStream.H:334
Foam::messageStream::FATAL
A fatal error.
Definition: messageStream.H:81
Foam::SeriousError
messageStream SeriousError
label.H
Foam::messageStream::SERIOUS
A serious problem - eg, data corruption.
Definition: messageStream.H:80
Foam::messageStream::title
const string & title() const
The title of this error type.
Definition: messageStream.H:132
WarningIn
#define WarningIn(functionName)
Report a warning using Foam::Warning.
Definition: messageStream.H:298
Foam::messageStream::messageStream
messageStream(const string &title, const errorSeverity severity, const int maxErrors=0)
Construct from components.
Definition: messageStream.C:47
Foam::messageStream::INFO
General information output.
Definition: messageStream.H:78
Foam::messageStream::INFO_STDERR
Information, but on stderr.
Definition: messageStream.H:82
DebugVar
#define DebugVar(var)
Report a variable name and value.
Definition: messageStream.H:381
Foam::prefixOSstream::prefix
const string & prefix() const
Return the stream prefix.
Definition: prefixOSstream.H:101
OSstream.H