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-2024 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 globally 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,
107  const errorSeverity,
108  const int maxErrors = 0
109  );
110 
111 
112  // Member Functions
113 
114  //- Return the title of this error type
115  const string& title() const
116  {
117  return title_;
118  }
119 
120  //- Return the maximum number of errors before program termination
121  int maxErrors() const
122  {
123  return maxErrors_;
124  }
125 
126  //- Return non-const access to the maximum number of errors before
127  // program termination to enable user to reset it
128  int& maxErrors()
129  {
130  return maxErrors_;
131  }
132 
133  //- Convert to OSstream
134  // Prints basic message and returns OSstream for further info.
135  OSstream& operator()
136  (
137  const char* functionName,
138  const char* sourceFileName,
139  const int sourceFileLineNumber = 0
140  );
141 
142  //- Convert to OSstream
143  // Prints basic message and returns OSstream for further info.
144  OSstream& operator()
145  (
146  const string& 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 char* functionName,
156  const char* sourceFileName,
157  const int sourceFileLineNumber,
158  const string& ioFileName,
159  const label ioLineNumber = -1
160  );
161 
162  //- Convert to OSstream
163  // Prints basic message and returns OSstream for further info.
164  OSstream& operator()
165  (
166  const char* functionName,
167  const char* sourceFileName,
168  const int sourceFileLineNumber,
169  const IOstream&
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 dictionary&
180  );
181 
182  //- Explicitly convert to OSstream for << operations
183  OSstream& operator()(const label communicator = -1);
184 
185  //- Convert to OSstream for << operations
186  operator OSstream&()
187  {
188  return this->operator()();
189  }
190 };
191 
192 
193 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
194 // Global error declarations: defined in messageStream.C
195 
196 extern messageStream SeriousError;
197 extern messageStream Warning;
198 extern messageStream Info;
199 extern bool writeInfoHeader;
200 
201 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
202 
203 } // End namespace Foam
204 
205 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
206 
207 #include "OSstream.H"
208 
209 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
210 // Convenience macros to add the file name and line number to the function name
211 
212 // Compiler provided function name string:
213 // for gcc-compatible compilers use __PRETTY_FUNCTION__
214 // otherwise use the standard __func__
215 #ifdef __GNUC__
216  #define FUNCTION_NAME __PRETTY_FUNCTION__
217 #else
218  #define FUNCTION_NAME __func__
219 #endif
220 
221 
222 //- Report an error message using Foam::SeriousError
223 // for functionName in file __FILE__ at line __LINE__
224 #define SeriousErrorIn(functionName) \
225  ::Foam::SeriousError((functionName), __FILE__, __LINE__)
226 
227 //- Report an error message using Foam::SeriousError
228 // for FUNCTION_NAME in file __FILE__ at line __LINE__
229 #define SeriousErrorInFunction SeriousErrorIn(FUNCTION_NAME)
230 
231 
232 //- Report an IO error message using Foam::SeriousError
233 // for functionName in file __FILE__ at line __LINE__
234 // for a particular IOstream
235 #define SeriousIOErrorIn(functionName, ios) \
236  ::Foam::SeriousError((functionName), __FILE__, __LINE__, ios)
237 
238 //- Report an IO error message using Foam::SeriousError
239 // for FUNCTION_NAME in file __FILE__ at line __LINE__
240 // for a particular IOstream
241 #define SeriousIOErrorInFunction(ios) SeriousIOErrorIn(FUNCTION_NAME, ios)
242 
243 
244 //- Report a warning using Foam::Warning
245 // for functionName in file __FILE__ at line __LINE__
246 #define WarningIn(functionName) \
247  ::Foam::Warning((functionName), __FILE__, __LINE__)
248 
249 //- Report a warning using Foam::Warning
250 // for FUNCTION_NAME in file __FILE__ at line __LINE__
251 #define WarningInFunction WarningIn(FUNCTION_NAME)
252 
253 
254 //- Report an IO warning using Foam::Warning
255 // for functionName in file __FILE__ at line __LINE__
256 // for a particular IOstream
257 #define IOWarningIn(functionName, ios) \
258  ::Foam::Warning((functionName), __FILE__, __LINE__, (ios))
259 
260 //- Report an IO warning using Foam::Warning
261 // for FUNCTION_NAME in file __FILE__ at line __LINE__
262 // for a particular IOstream
263 #define IOWarningInFunction(ios) IOWarningIn(FUNCTION_NAME, ios)
264 
265 
266 //- Report an information message using Foam::Info
267 // for functionName in file __FILE__ at line __LINE__
268 #define InfoIn(functionName) \
269  ::Foam::Info((functionName), __FILE__, __LINE__)
270 
271 //- Report an information message using Foam::Info
272 // for FUNCTION_NAME in file __FILE__ at line __LINE__
273 #define InfoInFunction InfoIn(FUNCTION_NAME)
274 
275 //- Report write to Foam::Info if the local log switch is true
276 #define InfoHeader \
277  if (::Foam::writeInfoHeader) Info
278 
279 //- Report write to Foam::Info if the local log switch is true
280 #define Log \
281  if (log) Info
282 
283 
284 //- Report an IO information message using Foam::Info
285 // for functionName in file __FILE__ at line __LINE__
286 // for a particular IOstream
287 #define IOInfoIn(functionName, ios) \
288  ::Foam::Info((functionName), __FILE__, __LINE__, (ios))
289 
290 //- Report an IO information message using Foam::Info
291 // for FUNCTION_NAME in file __FILE__ at line __LINE__
292 // for a particular IOstream
293 #define IOInfoInFunction(ios) IOInfoIn(FUNCTION_NAME, ios)
294 
295 
296 //- Report an information message using Foam::Info
297 // if the local debug switch is true
298 #define DebugInfo \
299  if (debug) Info
300 
301 //- Report an information message using Foam::Info
302 // for FUNCTION_NAME in file __FILE__ at line __LINE__
303 // if the local debug switch is true
304 #define DebugInFunction \
305  if (debug) InfoInFunction
306 
307 //- Report a variable name and value
308 // using Foam::Pout in file __FILE__ at line __LINE__
309 #define DebugVar(var) \
310 { \
311  ::Foam::string oldPrefix(::Foam::Pout.prefix()); \
312  ::Foam::Pout<< "["<< __FILE__ << ":" << __LINE__ << "] "; \
313  ::Foam::Pout.prefix() = oldPrefix + #var " "; \
314  ::Foam::Pout<< var << ::Foam::endl; \
315  ::Foam::Pout.prefix() = oldPrefix; \
316 }
317 
318 
319 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
320 
321 #endif
322 
323 // ************************************************************************* //
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:72
Generic output stream.
Definition: OSstream.H:54
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
A functionName is a word starting with '#'.
Definition: functionName.H:60
Class to handle messaging in a simple, consistent stream-based manner.
Definition: messageStream.H:69
errorSeverity
Severity flags.
Definition: messageStream.H:75
errorSeverity severity_
Definition: messageStream.H:88
OSstream & operator()(const char *functionName, const char *sourceFileName, const int sourceFileLineNumber=0)
Convert to OSstream.
Definition: messageStream.C:51
int maxErrors() const
Return the maximum number of errors before program termination.
messageStream(const string &title, const errorSeverity, const int maxErrors=0)
Construct from components.
Definition: messageStream.C:37
const string & title() const
Return the title of this error type.
Namespace for OpenFOAM.
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
bool writeInfoHeader
messageStream Info
messageStream SeriousError
messageStream Warning