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