messageStream.C
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) 2017-2021 OpenCFD Ltd.
10-------------------------------------------------------------------------------
11License
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\*---------------------------------------------------------------------------*/
28
29#include "error.H"
30#include "dictionary.H"
31#include "Pstream.H"
32
33// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
34
35// Default is 2 : report source file name and line number if available
37
39
40// Default is 1 : report to Info
42
43
44// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
45
47(
48 const string& title,
49 const errorSeverity severity,
50 const int maxErrors
51)
52:
53 title_(title),
54 severity_(severity),
55 maxErrors_(maxErrors),
56 errorCount_(0)
57{}
58
59
61:
62 title_(dict.get<string>("title")),
63 severity_(FATAL),
64 maxErrors_(0),
65 errorCount_(0)
66{}
67
68
69// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
70
72{
73 if (level)
74 {
75 // Serlal (master only) output?
76 const bool serialOnly
77 (
78 (
79 severity_ == INFO
80 || severity_ == INFO_STDERR
81 || severity_ == WARNING
82 )
84 );
85
86 if (serialOnly && (UPstream::parRun() && !UPstream::master()))
87 {
88 return Snull; // Non-serial, non-master: exit early
89 }
90
91
92 // Use stderr instead of stdout:
93 // - requested via static <redirect> variable
94 // - explicit: INFO_STDERR
95 // - inferred: WARNING -> stderr when infoDetailLevel == 0
96 const bool useStderr =
97 (
98 (redirect == 2)
99 || (severity_ == INFO_STDERR)
100 || (severity_ == WARNING && Foam::infoDetailLevel == 0)
101 );
102
103 OSstream* osptr;
104
105 if (serialOnly)
106 {
107 // Use supplied alternative? Valid for serial only
108 osptr = alternative;
109
110 if (!osptr)
111 {
112 osptr = (useStderr ? &Serr : &Sout);
113 }
114 }
115 else
116 {
117 // Non-serial
118 osptr = (useStderr ? &Perr : &Pout);
119 }
120
121 if (!title_.empty())
122 {
123 (*osptr) << title_.c_str();
124 }
125
126 if (maxErrors_ && (++errorCount_ >= maxErrors_))
127 {
129 << "Too many errors..."
130 << abort(FatalError);
131 }
132
133 return *osptr;
134 }
135
136 return Snull;
137}
138
139
141{
142 if (UPstream::warnComm != -1 && communicator != UPstream::warnComm)
143 {
144 Pout<< "** messageStream with comm:" << communicator << endl;
146 }
147
148 if (communicator == UPstream::worldComm || UPstream::master(communicator))
149 {
150 return this->stream();
151 }
152
153 return Snull;
154}
155
156
158{
159 return this->stream().stdStream();
160}
161
162
163// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
164
166(
167 const string& functionName
168)
169{
170 OSstream& os = this->stream();
171
172 if (!functionName.empty())
173 {
174 os << nl
175 << " From " << functionName.c_str() << nl;
176 }
177
178 return os;
179}
180
181
183(
184 const char* functionName,
185 const char* sourceFileName,
186 const int sourceFileLineNumber
187)
188{
189 OSstream& os = this->stream();
190
191 os << nl
192 << " From " << functionName << nl
193 << " in file " << sourceFileName
194 << " at line " << sourceFileLineNumber << endl
195 << " ";
196
197 return os;
198}
199
200
202(
203 const string& functionName,
204 const char* sourceFileName,
205 const int sourceFileLineNumber
206)
207{
208 return operator()
209 (
210 functionName.c_str(),
211 sourceFileName,
212 sourceFileLineNumber
213 );
214}
215
216
218(
219 const char* functionName,
220 const char* sourceFileName,
221 const int sourceFileLineNumber,
222 const string& ioFileName,
223 const label ioStartLineNumber,
224 const label ioEndLineNumber
225)
226{
227 OSstream& os = this->stream();
228
229 os << nl
230 << " From " << functionName << nl
231 << " in file " << sourceFileName
232 << " at line " << sourceFileLineNumber << nl
233 << " Reading " << ioFileName;
234
235 if (ioStartLineNumber >= 0)
236 {
237 os << " at line " << ioStartLineNumber;
238
239 if (ioStartLineNumber < ioEndLineNumber)
240 {
241 os << " to " << ioEndLineNumber;
242 }
243 }
244
245 os << endl << " ";
246
247 return os;
248}
249
250
252(
253 const char* functionName,
254 const char* sourceFileName,
255 const int sourceFileLineNumber,
256 const IOstream& ioStream
257)
258{
259 return operator()
260 (
261 functionName,
262 sourceFileName,
263 sourceFileLineNumber,
264 ioStream.name(),
265 ioStream.lineNumber(),
266 -1 // No known endLineNumber
267 );
268}
269
270
272(
273 const char* functionName,
274 const char* sourceFileName,
275 const int sourceFileLineNumber,
276 const dictionary& dict
277)
278{
279 return operator()
280 (
281 functionName,
282 sourceFileName,
283 sourceFileLineNumber,
287 );
288}
289
290
291// * * * * * * * * * * * * * * * Global Variables * * * * * * * * * * * * * //
292
294
296
298(
299 "--> FOAM Warning : ",
301);
302
304(
305 "--> FOAM Serious Error : ",
307 100
308);
309
310
311// ************************************************************************* //
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:82
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:57
static label warnComm
Debugging: warn for use of any communicator differing from warnComm.
Definition: UPstream.H:296
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:293
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:433
virtual ITstream & stream() const
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:126
fileName relativeName(const bool caseTag=false) const
The dictionary name relative to the case.
Definition: dictionary.C:186
label endLineNumber() const
Return line number of last token in dictionary.
Definition: dictionary.C:216
label startLineNumber() const
Return line number of first token in dictionary.
Definition: dictionary.C:205
static void printStack(Ostream &os)
Helper function to print a stack.
friend Ostream & operator(Ostream &, const faMatrix< Type > &)
Handle output messages in a simple, consistent stream-based manner.
Definition: messageStream.H:74
OSstream & masterStream(const label communicator)
errorSeverity
Message type, error severity flags.
Definition: messageStream.H:79
@ INFO_STDERR
General information output (stderr)
Definition: messageStream.H:82
@ INFO
General information output (stdout)
Definition: messageStream.H:81
@ WARNING
Warning of possible problem.
Definition: messageStream.H:83
@ SERIOUS
A serious problem - eg, data corruption.
Definition: messageStream.H:86
std::ostream & stdStream()
Return std::ostream for output operations.
static int level
The output level (verbosity) of messages.
static int redirect
The output redirection of messages.
splitCell * master() const
Definition: splitCell.H:113
A class for handling character strings derived from std::string.
Definition: string.H:79
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
OBJstream os(runTime.globalPath()/outputName)
int infoSwitch(const char *name, const int deflt=0)
Lookup info switch or add default value.
Definition: debug.C:231
prefixOSstream Perr
OSstream wrapped stderr (std::cerr) with parallel prefix.
messageStream Info
Information stream (stdout output on master, null elsewhere)
OFstream Snull
Global predefined null output stream "/dev/null".
OSstream Sout
OSstream wrapped stdout (std::cout)
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:372
messageStream SeriousError
OSstream Serr
OSstream wrapped stderr (std::cerr)
errorManip< error > abort(error &err)
Definition: errorManip.H:144
int infoDetailLevel
Global for selective suppression of Info output.
messageStream InfoErr
Information stream (stderr output on master, null elsewhere)
error FatalError
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
messageStream Warning
constexpr char nl
The newline '\n' character (0x0a)
Definition: Ostream.H:53
dictionary dict