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