messageStream.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2020 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Class
25  Foam::messageStream
26 
27 Description
28  Class to handle messaging in a simple, consistent stream-based
29  manner.
30 
31  The messageStream class is globaly instantiated with a title string a
32  given severity, which controls the program termination, and a number of
33  errors before termination. Errors, messages and other data are piped to
34  the messageStream class in the standard manner.
35 
36 Usage
37  \code
38  messageStream
39  << "message1" << "message2" << FoamDataType << endl;
40  \endcode
41 
42 SourceFiles
43  messageStream.C
44 
45 \*---------------------------------------------------------------------------*/
46 
47 #ifndef messageStream_H
48 #define messageStream_H
49 
50 #include "label.H"
51 #include "string.H"
52 
53 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
54 
55 namespace Foam
56 {
57 
58 // Forward declaration of classes
59 class IOstream;
60 class Ostream;
61 class OSstream;
62 class OStringStream;
63 class dictionary;
64 
65 /*---------------------------------------------------------------------------*\
66  Class messageStream Declaration
67 \*---------------------------------------------------------------------------*/
68 
69 class messageStream
70 {
71 
72 public:
73 
74  //- Severity flags
75  enum errorSeverity
76  {
77  INFO, // Debugging information in event of error
78  WARNING, // Warning of possible problem
79  SERIOUS, // A serious problem (data corruption?)
80  FATAL // Oh bugger!
81  };
82 
83 
84 protected:
85 
86  // Private Data
87 
88  string title_;
91  int errorCount_;
92 
93 
94 public:
95 
96  // Debug switches
97 
98  static int level;
99 
100 
101  // Constructors
102 
103  //- Construct from components
105  (
106  const string& title,
108  const int maxErrors = 0
109  );
110 
111 
112  //- Construct from dictionary
113  messageStream(const dictionary&);
114 
115 
116  // Member Functions
117 
118  //- Return the title of this error type
119  const string& title() const
120  {
121  return title_;
122  }
123 
124  //- Return the maximum number of errors before program termination
125  int maxErrors() const
126  {
127  return maxErrors_;
128  }
129 
130  //- Return non-const access to the maximum number of errors before
131  // program termination to enable user to reset it
132  int& maxErrors()
133  {
134  return maxErrors_;
135  }
136 
137  //- Convert to OSstream
138  // Prints to Pout for the master stream
139  OSstream& masterStream(const label communicator);
140 
141 
142  //- Convert to OSstream
143  // Prints basic message and returns OSstream for further info.
144  OSstream& operator()
145  (
146  const char* functionName,
147  const char* sourceFileName,
148  const int sourceFileLineNumber = 0
149  );
150 
151  //- Convert to OSstream
152  // Prints basic message and returns OSstream for further info.
153  OSstream& operator()
154  (
155  const string& functionName,
156  const char* sourceFileName,
157  const int sourceFileLineNumber = 0
158  );
159 
160  //- Convert to OSstream
161  // Prints basic message and returns OSstream for further info.
162  OSstream& operator()
163  (
164  const char* functionName,
165  const char* sourceFileName,
166  const int sourceFileLineNumber,
167  const string& ioFileName,
168  const label ioStartLineNumber = -1,
169  const label ioEndLineNumber = -1
170  );
171 
172  //- Convert to OSstream
173  // Prints basic message and returns OSstream for further info.
174  OSstream& operator()
175  (
176  const char* functionName,
177  const char* sourceFileName,
178  const int sourceFileLineNumber,
179  const IOstream&
180  );
181 
182  //- Convert to OSstream
183  // Prints basic message and returns OSstream for further info.
184  OSstream& operator()
185  (
186  const char* functionName,
187  const char* sourceFileName,
188  const int sourceFileLineNumber,
189  const dictionary&
190  );
191 
192  //- Convert to OSstream for << operations
193  operator OSstream&();
194 
195  //- Explicitly convert to OSstream for << operations
197  {
198  return operator OSstream&();
199  }
200 };
201 
202 
203 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
204 // Global error declarations: defined in messageStream.C
205 
207 extern messageStream Warning;
208 extern messageStream Info;
209 extern bool writeInfoHeader;
210 
211 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
212 
213 } // End namespace Foam
214 
215 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
216 
217 #include "OSstream.H"
218 
219 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
220 // Convenience macros to add the file name and line number to the function name
221 
222 // Compiler provided function name string:
223 // for gcc-compatible compilers use __PRETTY_FUNCTION__
224 // otherwise use the standard __func__
225 #ifdef __GNUC__
226  #define FUNCTION_NAME __PRETTY_FUNCTION__
227 #else
228  #define FUNCTION_NAME __func__
229 #endif
230 
231 
232 //- Report an error message using Foam::SeriousError
233 // for functionName in file __FILE__ at line __LINE__
234 #define SeriousErrorIn(functionName) \
235  ::Foam::SeriousError((functionName), __FILE__, __LINE__)
236 
237 //- Report an error message using Foam::SeriousError
238 // for FUNCTION_NAME in file __FILE__ at line __LINE__
239 #define SeriousErrorInFunction SeriousErrorIn(FUNCTION_NAME)
240 
241 
242 //- Report an IO error message using Foam::SeriousError
243 // for functionName in file __FILE__ at line __LINE__
244 // for a particular IOstream
245 #define SeriousIOErrorIn(functionName, ios) \
246  ::Foam::SeriousError((functionName), __FILE__, __LINE__, ios)
247 
248 //- Report an IO error message using Foam::SeriousError
249 // for FUNCTION_NAME in file __FILE__ at line __LINE__
250 // for a particular IOstream
251 #define SeriousIOErrorInFunction(ios) SeriousIOErrorIn(FUNCTION_NAME, ios)
252 
253 
254 //- Report a warning using Foam::Warning
255 // for functionName in file __FILE__ at line __LINE__
256 #define WarningIn(functionName) \
257  ::Foam::Warning((functionName), __FILE__, __LINE__)
258 
259 //- Report a warning using Foam::Warning
260 // for FUNCTION_NAME in file __FILE__ at line __LINE__
261 #define WarningInFunction WarningIn(FUNCTION_NAME)
262 
263 
264 //- Report an IO warning using Foam::Warning
265 // for functionName in file __FILE__ at line __LINE__
266 // for a particular IOstream
267 #define IOWarningIn(functionName, ios) \
268  ::Foam::Warning((functionName), __FILE__, __LINE__, (ios))
269 
270 //- Report an IO warning using Foam::Warning
271 // for FUNCTION_NAME in file __FILE__ at line __LINE__
272 // for a particular IOstream
273 #define IOWarningInFunction(ios) IOWarningIn(FUNCTION_NAME, ios)
274 
275 
276 //- Report an information message using Foam::Info
277 // for functionName in file __FILE__ at line __LINE__
278 #define InfoIn(functionName) \
279  ::Foam::Info((functionName), __FILE__, __LINE__)
280 
281 //- Report an information message using Foam::Info
282 // for FUNCTION_NAME in file __FILE__ at line __LINE__
283 #define InfoInFunction InfoIn(FUNCTION_NAME)
284 
285 //- Report write to Foam::Info if the local log switch is true
286 #define InfoHeader \
287  if (::Foam::writeInfoHeader) Info
288 
289 //- Report write to Foam::Info if the local log switch is true
290 #define Log \
291  if (log) Info
292 
293 
294 //- Report an IO information message using Foam::Info
295 // for functionName in file __FILE__ at line __LINE__
296 // for a particular IOstream
297 #define IOInfoIn(functionName, ios) \
298  ::Foam::Info((functionName), __FILE__, __LINE__, (ios))
299 
300 //- Report an IO information message using Foam::Info
301 // for FUNCTION_NAME in file __FILE__ at line __LINE__
302 // for a particular IOstream
303 #define IOInfoInFunction(ios) IOInfoIn(FUNCTION_NAME, ios)
304 
305 
306 //- Report an information message using Foam::Info
307 // if the local debug switch is true
308 #define DebugInfo \
309  if (debug) Info
310 
311 //- Report an information message using Foam::Info
312 // for FUNCTION_NAME in file __FILE__ at line __LINE__
313 // if the local debug switch is true
314 #define DebugInFunction \
315  if (debug) InfoInFunction
316 
317 //- Report a variable name and value
318 // using Foam::Pout in file __FILE__ at line __LINE__
319 #define DebugVar(var) \
320 { \
321  ::Foam::string oldPrefix(::Foam::Pout.prefix()); \
322  ::Foam::Pout<< "["<< __FILE__ << ":" << __LINE__ << "] "; \
323  ::Foam::Pout.prefix() = oldPrefix + #var " "; \
324  ::Foam::Pout<< var << ::Foam::endl; \
325  ::Foam::Pout.prefix() = oldPrefix; \
326 }
327 
328 
329 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
330 
331 #endif
332 
333 // ************************************************************************* //
Generic output stream.
Definition: OSstream.H:51
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:59
errorSeverity
Severity flags.
Definition: messageStream.H:74
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
bool writeInfoHeader
const string & title() const
Return the title of this error type.
Class to handle messaging in a simple, consistent stream-based manner.
Definition: messageStream.H:68
errorSeverity severity_
Definition: messageStream.H:88
A functionName is a word starting with &#39;#&#39;.
Definition: functionName.H:57
messageStream SeriousError
messageStream Warning
OSstream & masterStream(const label communicator)
Convert to OSstream.
Definition: messageStream.C:59
An IOstream is an abstract base class for all input/output systems; be they streams, files, token lists etc.
Definition: IOstream.H:71
messageStream(const string &title, errorSeverity, const int maxErrors=0)
Construct from components.
Definition: messageStream.C:37
messageStream Info
int maxErrors() const
Return the maximum number of errors before program termination.
OSstream & operator()()
Explicitly convert to OSstream for << operations.
Namespace for OpenFOAM.