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-2023 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 "foamVersion.H"
35 #include "Pstream.H"
36 
37 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
38 
39 // Default is 2 : report source file name and line number if available
41 
43 
44 // Default is 1 : report to Info
46 
47 
48 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
49 
51 (
52  const string& title,
53  const errorSeverity severity,
54  const int maxErrors
55 )
56 :
57  title_(title),
58  severity_(severity),
59  maxErrors_(maxErrors),
60  errorCount_(0)
61 {}
62 
63 
65 :
66  title_(dict.get<string>("title")),
67  severity_(FATAL),
68  maxErrors_(0),
69  errorCount_(0)
70 {}
71 
72 
73 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
74 
76 {
77  if (level)
78  {
79  // Serlal (master only) output?
80  const bool serialOnly
81  (
82  (
83  severity_ == INFO
85  || severity_ == WARNING
86  )
87  || !UPstream::parRun()
88  );
89 
90  if (serialOnly && (UPstream::parRun() && !UPstream::master()))
91  {
92  return Snull; // Non-serial, non-master: exit early
93  }
94 
95 
96  // Use stderr instead of stdout:
97  // - requested via static <redirect> variable
98  // - explicit: INFO_STDERR
99  // - inferred: WARNING -> stderr when infoDetailLevel == 0
100  const bool useStderr =
101  (
102  (redirect == 2)
103  || (severity_ == INFO_STDERR)
104  || (severity_ == WARNING && Foam::infoDetailLevel == 0)
105  );
106 
107  OSstream* osptr;
108 
109  if (serialOnly)
110  {
111  // Use supplied alternative? Valid for serial only
112  osptr = alternative;
113 
114  if (!osptr)
115  {
116  osptr = (useStderr ? &Serr : &Sout);
117  }
118  }
119  else
120  {
121  // Non-serial
122  osptr = (useStderr ? &Perr : &Pout);
123  }
124 
125  if (!title_.empty())
126  {
127  (*osptr) << title_.c_str();
128  }
129 
130  if (maxErrors_ && (++errorCount_ >= maxErrors_))
131  {
133  << "Too many errors..."
134  << abort(FatalError);
135  }
136 
137  return *osptr;
138  }
139 
140  return Snull;
141 }
142 
143 
144 Foam::OSstream& Foam::messageStream::masterStream(const label communicator)
145 {
146  if (UPstream::warnComm >= 0 && communicator != UPstream::warnComm)
147  {
148  Pout<< "** messageStream with comm:" << communicator << endl;
150  }
151 
152  if (communicator == UPstream::worldComm || UPstream::master(communicator))
153  {
154  return this->stream();
155  }
156 
157  return Snull;
158 }
159 
160 
161 std::ostream& Foam::messageStream::stdStream()
162 {
163  return this->stream().stdStream();
164 }
165 
166 
167 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
168 
169 Foam::OSstream& Foam::messageStream::operator()
170 (
171  const std::string& functionName
172 )
173 {
174  OSstream& os = this->stream();
175 
176  if (!functionName.empty())
177  {
178  os << nl
179  << " From " << functionName.c_str() << nl;
180  }
181 
182  return os;
183 }
184 
185 
187 (
188  const int afterVersion,
189  const char* functionName,
190  const char* sourceFileName,
191  const int sourceFileLineNumber
192 )
193 {
194  OSstream& os = this->stream();
195 
196  // No warning for 0 (unversioned) or -ve values (silent versioning).
197  // Also no warning for (version >= foamVersion::api), which
198  // can be used to denote future expiry dates of transition features.
199 
200  if (afterVersion > 0 && afterVersion < foamVersion::api)
201  {
202  const int months =
203  (
204  // YYMM -> months
205  (12 * (foamVersion::api/100) + (foamVersion::api % 100))
206  - (12 * (afterVersion/100) + (afterVersion % 100))
207  );
208 
209  os << nl
210  << ">>> DEPRECATED after version " << afterVersion;
211 
212  if (afterVersion < 1000)
213  {
214  // Predates YYMM versioning (eg, 240 for version 2.4)
215  os << ". This is very old! <<<" << nl;
216  }
217  else
218  {
219  os << ". This is about " << months << " months old. <<<" << nl;
220  }
221  }
222 
223  os << nl;
224 
225  if (functionName) // nullptr check
226  {
227  os << " From " << functionName << nl
228  << " in file " << sourceFileName
229  << " at line " << sourceFileLineNumber << nl;
230  }
231  os << " ";
232 
233  return os;
234 }
235 
236 
237 Foam::OSstream& Foam::messageStream::operator()
238 (
239  const char* functionName,
240  const char* sourceFileName,
241  const int sourceFileLineNumber
242 )
243 {
244  OSstream& os = this->stream();
245 
246  os << nl
247  << " From " << functionName << nl
248  << " in file " << sourceFileName
249  << " at line " << sourceFileLineNumber << nl
250  << " ";
251 
252  return os;
253 }
254 
255 
256 Foam::OSstream& Foam::messageStream::operator()
257 (
258  const std::string& functionName,
259  const char* sourceFileName,
260  const int sourceFileLineNumber
261 )
262 {
263  return operator()
264  (
265  functionName.c_str(),
266  sourceFileName,
267  sourceFileLineNumber
268  );
269 }
270 
271 
272 Foam::OSstream& Foam::messageStream::operator()
273 (
274  const char* functionName,
275  const char* sourceFileName,
276  const int sourceFileLineNumber,
277  const std::string& ioFileName,
278  const label ioStartLineNumber,
279  const label ioEndLineNumber
280 )
281 {
282  OSstream& os = this->stream();
283 
284  os << nl
285  << " From " << functionName << nl
286  << " in file " << sourceFileName
287  << " at line " << sourceFileLineNumber << nl
288  << " Reading \"" << ioFileName.c_str() << '"';
289 
290  if (ioStartLineNumber >= 0)
291  {
292  os << " at line " << ioStartLineNumber;
293 
294  if (ioStartLineNumber < ioEndLineNumber)
295  {
296  os << " to " << ioEndLineNumber;
297  }
298  }
299 
300  os << endl << " ";
301 
302  return os;
303 }
304 
305 
306 Foam::OSstream& Foam::messageStream::operator()
307 (
308  const char* functionName,
309  const char* sourceFileName,
310  const int sourceFileLineNumber,
311  const IOstream& ioStream
312 )
313 {
314  return operator()
315  (
316  functionName,
317  sourceFileName,
318  sourceFileLineNumber,
319  ioStream.relativeName(),
320  ioStream.lineNumber(),
321  -1 // No known endLineNumber
322  );
323 }
324 
325 
326 Foam::OSstream& Foam::messageStream::operator()
327 (
328  const char* functionName,
329  const char* sourceFileName,
330  const int sourceFileLineNumber,
331  const dictionary& dict
332 )
333 {
334  return operator()
335  (
336  functionName,
337  sourceFileName,
338  sourceFileLineNumber,
339  dict.relativeName(),
342  );
343 }
344 
345 
346 // * * * * * * * * * * * * * * * Global Variables * * * * * * * * * * * * * //
347 
349 
351 
353 (
354  "--> FOAM Warning : ",
356 );
357 
359 (
360  "--> FOAM Serious Error : ",
362  100
363 );
364 
365 
366 // ************************************************************************* //
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:77
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:129
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:598
int infoDetailLevel
Global for selective suppression of Info output.
constexpr char nl
The newline &#39;\n&#39; character (0x0a)
Definition: Ostream.H:50
std::ostream & stdStream()
Return std::ostream for output operations.
A serious problem - eg, data corruption.
Definition: messageStream.H:85
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:531
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:1049
OFstream Snull
Global predefined null output stream "/dev/null".
Handle output messages in a simple, consistent stream-based manner.
Definition: messageStream.H:70
static label worldComm
Communicator for all ranks. May differ from commGlobal() if local worlds are in use.
Definition: UPstream.H:409
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:95
messageStream SeriousError
Error stream (stdout output on all processes), with additional &#39;FOAM Serious Error&#39; header text...
OSstream Sout
OSstream wrapped stdout (std::cout)
static label warnComm
Debugging: warn for use of any communicator differing from warnComm.
Definition: UPstream.H:414
errorManip< error > abort(error &err)
Definition: errorManip.H:139
const int api
OpenFOAM api number (integer) corresponding to the value of OPENFOAM at the time of compilation...
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:68
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 & deprecated(const int afterVersion, const char *functionName, const char *sourceFileName, const int sourceFileLineNumber=0)
Report deprecation (after specified API version) with &#39;From function-name, source file...
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:82
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1082
OSstream Serr
OSstream wrapped stderr (std::cerr)
messageStream Info
Information stream (stdout output on master, null elsewhere)
static void printStack(Ostream &os, int size=-1)
Helper function to print a stack, with optional upper limit.
Warning of possible problem.
Definition: messageStream.H:82
messageStream(const string &title, const errorSeverity severity, const int maxErrors=0)
Construct from components.
Definition: messageStream.C:44
General information output (stderr)
Definition: messageStream.H:81
A class for handling character strings derived from std::string.
Definition: string.H:72
General information output (stdout)
Definition: messageStream.H:80
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