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-2021 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,
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 ioStartLineNumber = -1,
160  const label ioEndLineNumber = -1
161  );
162 
163  //- Convert to OSstream
164  // Prints basic message and returns OSstream for further info.
165  OSstream& operator()
166  (
167  const char* functionName,
168  const char* sourceFileName,
169  const int sourceFileLineNumber,
170  const IOstream&
171  );
172 
173  //- Convert to OSstream
174  // Prints basic message and returns OSstream for further info.
175  OSstream& operator()
176  (
177  const char* functionName,
178  const char* sourceFileName,
179  const int sourceFileLineNumber,
180  const dictionary&
181  );
182 
183  //- Explicitly convert to OSstream for << operations
184  OSstream& operator()(const label communicator = -1);
185 
186  //- Convert to OSstream for << operations
187  operator OSstream&()
188  {
189  return this->operator()();
190  }
191 };
192 
193 
194 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
195 // Global error declarations: defined in messageStream.C
196 
198 extern messageStream Warning;
199 extern messageStream Info;
200 extern bool writeInfoHeader;
201 
202 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
203 
204 } // End namespace Foam
205 
206 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
207 
208 #include "OSstream.H"
209 
210 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
211 // Convenience macros to add the file name and line number to the function name
212 
213 // Compiler provided function name string:
214 // for gcc-compatible compilers use __PRETTY_FUNCTION__
215 // otherwise use the standard __func__
216 #ifdef __GNUC__
217  #define FUNCTION_NAME __PRETTY_FUNCTION__
218 #else
219  #define FUNCTION_NAME __func__
220 #endif
221 
222 
223 //- Report an error message using Foam::SeriousError
224 // for functionName in file __FILE__ at line __LINE__
225 #define SeriousErrorIn(functionName) \
226  ::Foam::SeriousError((functionName), __FILE__, __LINE__)
227 
228 //- Report an error message using Foam::SeriousError
229 // for FUNCTION_NAME in file __FILE__ at line __LINE__
230 #define SeriousErrorInFunction SeriousErrorIn(FUNCTION_NAME)
231 
232 
233 //- Report an IO error message using Foam::SeriousError
234 // for functionName in file __FILE__ at line __LINE__
235 // for a particular IOstream
236 #define SeriousIOErrorIn(functionName, ios) \
237  ::Foam::SeriousError((functionName), __FILE__, __LINE__, ios)
238 
239 //- Report an IO error message using Foam::SeriousError
240 // for FUNCTION_NAME in file __FILE__ at line __LINE__
241 // for a particular IOstream
242 #define SeriousIOErrorInFunction(ios) SeriousIOErrorIn(FUNCTION_NAME, ios)
243 
244 
245 //- Report a warning using Foam::Warning
246 // for functionName in file __FILE__ at line __LINE__
247 #define WarningIn(functionName) \
248  ::Foam::Warning((functionName), __FILE__, __LINE__)
249 
250 //- Report a warning using Foam::Warning
251 // for FUNCTION_NAME in file __FILE__ at line __LINE__
252 #define WarningInFunction WarningIn(FUNCTION_NAME)
253 
254 
255 //- Report an IO warning using Foam::Warning
256 // for functionName in file __FILE__ at line __LINE__
257 // for a particular IOstream
258 #define IOWarningIn(functionName, ios) \
259  ::Foam::Warning((functionName), __FILE__, __LINE__, (ios))
260 
261 //- Report an IO warning using Foam::Warning
262 // for FUNCTION_NAME in file __FILE__ at line __LINE__
263 // for a particular IOstream
264 #define IOWarningInFunction(ios) IOWarningIn(FUNCTION_NAME, ios)
265 
266 
267 //- Report an information message using Foam::Info
268 // for functionName in file __FILE__ at line __LINE__
269 #define InfoIn(functionName) \
270  ::Foam::Info((functionName), __FILE__, __LINE__)
271 
272 //- Report an information message using Foam::Info
273 // for FUNCTION_NAME in file __FILE__ at line __LINE__
274 #define InfoInFunction InfoIn(FUNCTION_NAME)
275 
276 //- Report write to Foam::Info if the local log switch is true
277 #define InfoHeader \
278  if (::Foam::writeInfoHeader) Info
279 
280 //- Report write to Foam::Info if the local log switch is true
281 #define Log \
282  if (log) Info
283 
284 
285 //- Report an IO information message using Foam::Info
286 // for functionName in file __FILE__ at line __LINE__
287 // for a particular IOstream
288 #define IOInfoIn(functionName, ios) \
289  ::Foam::Info((functionName), __FILE__, __LINE__, (ios))
290 
291 //- Report an IO information message using Foam::Info
292 // for FUNCTION_NAME in file __FILE__ at line __LINE__
293 // for a particular IOstream
294 #define IOInfoInFunction(ios) IOInfoIn(FUNCTION_NAME, ios)
295 
296 
297 //- Report an information message using Foam::Info
298 // if the local debug switch is true
299 #define DebugInfo \
300  if (debug) Info
301 
302 //- Report an information message using Foam::Info
303 // for FUNCTION_NAME in file __FILE__ at line __LINE__
304 // if the local debug switch is true
305 #define DebugInFunction \
306  if (debug) InfoInFunction
307 
308 //- Report a variable name and value
309 // using Foam::Pout in file __FILE__ at line __LINE__
310 #define DebugVar(var) \
311 { \
312  ::Foam::string oldPrefix(::Foam::Pout.prefix()); \
313  ::Foam::Pout<< "["<< __FILE__ << ":" << __LINE__ << "] "; \
314  ::Foam::Pout.prefix() = oldPrefix + #var " "; \
315  ::Foam::Pout<< var << ::Foam::endl; \
316  ::Foam::Pout.prefix() = oldPrefix; \
317 }
318 
319 
320 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
321 
322 #endif
323 
324 // ************************************************************************* //
Generic output stream.
Definition: OSstream.H:51
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
OSstream & operator()(const char *functionName, const char *sourceFileName, const int sourceFileLineNumber=0)
Convert to OSstream.
Definition: messageStream.C:51
messageStream(const string &title, const errorSeverity, const int maxErrors=0)
Construct from components.
Definition: messageStream.C:37
errorSeverity severity_
Definition: messageStream.H:88
A functionName is a word starting with &#39;#&#39;.
Definition: functionName.H:57
messageStream SeriousError
messageStream Warning
An IOstream is an abstract base class for all input/output systems; be they streams, files, token lists etc.
Definition: IOstream.H:71
messageStream Info
int maxErrors() const
Return the maximum number of errors before program termination.
Namespace for OpenFOAM.