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-2024 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 char* title,
53  errorSeverity severity,
54  int maxErrors,
55  bool use_stderr
56 )
57 :
58  title_(),
59  severity_(severity),
60  maxErrors_(maxErrors),
61  errorCount_(0)
62 {
63  if (title)
64  {
65  title_ = title;
66  }
67  if (use_stderr)
68  {
69  severity_ |= errorSeverity::USE_STDERR;
70  }
71 }
72 
73 
75 :
76  title_(dict.get<string>("title")),
77  severity_(errorSeverity::FATAL),
78  maxErrors_(0),
79  errorCount_(0)
80 {}
81 
82 
83 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
84 
86 (
87  OSstream* alternative
88 )
89 {
90  if (level)
91  {
92  // Serlal (master only) output?
93  const bool serialOnly
94  (
96  || ((severity_ & ~errorSeverity::USE_STDERR) == errorSeverity::INFO)
97  || ((severity_ & ~errorSeverity::USE_STDERR) == errorSeverity::WARNING)
98  );
99 
100  if (serialOnly && (UPstream::parRun() && !UPstream::master()))
101  {
102  return Snull; // Non-serial, non-master: exit early
103  }
104 
105 
106  // Use stderr instead of stdout:
107  // - requested via static <redirect> variable
108  // - explicit: with USE_STDERR mask
109  // - inferred: WARNING -> stderr when infoDetailLevel == 0
110  const bool use_stderr =
111  (
112  (redirect == 2)
113  || (severity_ & errorSeverity::USE_STDERR)
114  || (severity_ == errorSeverity::WARNING && Foam::infoDetailLevel == 0)
115  );
116 
117 
118  OSstream* osptr;
119 
120  if (serialOnly)
121  {
122  // Use supplied alternative? Valid for serial only
123 
124  osptr =
125  (
126  alternative
127  ? alternative
128  : (use_stderr ? &Serr : &Sout)
129  );
130  }
131  else
132  {
133  // Non-serial
134  osptr = (use_stderr ? &Perr : &Pout);
135  }
136 
137  if (!title_.empty())
138  {
139  (*osptr) << title_.c_str();
140  }
141 
142  if (maxErrors_ && (++errorCount_ >= maxErrors_))
143  {
145  << "Too many errors..."
146  << abort(FatalError);
147  }
148 
149  return *osptr;
150  }
151 
152  return Snull;
153 }
154 
155 
156 Foam::OSstream& Foam::messageStream::masterStream(const label communicator)
157 {
158  if (UPstream::warnComm >= 0 && communicator != UPstream::warnComm)
159  {
160  Perr<< "** messageStream with comm:" << communicator << endl;
162  }
163 
164  if (communicator == UPstream::worldComm || UPstream::master(communicator))
165  {
166  return this->stream();
167  }
168 
169  return Snull;
170 }
171 
172 
173 std::ostream& Foam::messageStream::stdStream()
174 {
175  return this->stream().stdStream();
176 }
177 
178 
179 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
180 
181 Foam::OSstream& Foam::messageStream::operator()
182 (
183  const std::string& functionName
184 )
185 {
186  OSstream& os = this->stream();
187 
188  if (!functionName.empty())
189  {
190  os << nl
191  << " From " << functionName.c_str() << nl;
192  }
193 
194  return os;
195 }
196 
197 
199 (
200  const int afterVersion,
201  const char* functionName,
202  const char* sourceFileName,
203  const int sourceFileLineNumber
204 )
205 {
206  OSstream& os = this->stream();
207 
208  // No warning for 0 (unversioned) or -ve values (silent versioning).
209  // Also no warning for (version >= foamVersion::api), which
210  // can be used to denote future expiry dates of transition features.
211 
212  if (afterVersion > 0 && afterVersion < foamVersion::api)
213  {
214  const int months =
215  (
216  // YYMM -> months
217  (12 * (foamVersion::api/100) + (foamVersion::api % 100))
218  - (12 * (afterVersion/100) + (afterVersion % 100))
219  );
220 
221  os << nl
222  << ">>> DEPRECATED after version " << afterVersion;
223 
224  if (afterVersion < 1000)
225  {
226  // Predates YYMM versioning (eg, 240 for version 2.4)
227  os << ". This is very old! <<<" << nl;
228  }
229  else
230  {
231  os << ". This is about " << months << " months old. <<<" << nl;
232  }
233  }
234 
235 
236  os << nl;
237  if (functionName) // nullptr check
238  {
239  {
240  os << " From " << functionName << nl;
241  }
242  if (sourceFileName)
243  {
244  os << " in file " << sourceFileName
245  << " at line " << sourceFileLineNumber << nl;
246  }
247  }
248  os << " ";
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 )
260 {
261  OSstream& os = this->stream();
262 
263  os << nl
264  << " From " << functionName << nl
265  << " in file " << sourceFileName
266  << " at line " << sourceFileLineNumber << nl
267  << " ";
268 
269  return os;
270 }
271 
272 
273 Foam::OSstream& Foam::messageStream::operator()
274 (
275  const std::string& functionName,
276  const char* sourceFileName,
277  const int sourceFileLineNumber
278 )
279 {
280  return operator()
281  (
282  functionName.c_str(),
283  sourceFileName,
284  sourceFileLineNumber
285  );
286 }
287 
288 
289 Foam::OSstream& Foam::messageStream::operator()
290 (
291  const char* functionName,
292  const char* sourceFileName,
293  const int sourceFileLineNumber,
294  const std::string& ioFileName,
295  const label ioStartLineNumber,
296  const label ioEndLineNumber
297 )
298 {
299  OSstream& os = operator()
300  (
301  functionName,
302  sourceFileName,
303  sourceFileLineNumber
304  );
305 
306  os << "Reading \"" << ioFileName.c_str() << '"';
307 
308  if (ioStartLineNumber >= 0)
309  {
310  os << " at line " << ioStartLineNumber;
311 
312  if (ioStartLineNumber < ioEndLineNumber)
313  {
314  os << " to " << ioEndLineNumber;
315  }
316  }
317 
318  os << endl << " ";
319 
320  return os;
321 }
322 
323 
324 Foam::OSstream& Foam::messageStream::operator()
325 (
326  const char* functionName,
327  const char* sourceFileName,
328  const int sourceFileLineNumber,
329  const IOstream& ioStream
330 )
331 {
332  return operator()
333  (
334  functionName,
335  sourceFileName,
336  sourceFileLineNumber,
337  ioStream.relativeName(),
338  ioStream.lineNumber(),
339  -1 // No known endLineNumber
340  );
341 }
342 
343 
344 Foam::OSstream& Foam::messageStream::operator()
345 (
346  const char* functionName,
347  const char* sourceFileName,
348  const int sourceFileLineNumber,
349  const dictionary& dict
350 )
351 {
352  return operator()
353  (
354  functionName,
355  sourceFileName,
356  sourceFileLineNumber,
357  dict.relativeName(),
360  );
361 }
362 
363 
364 // * * * * * * * * * * * * * * * Global Variables * * * * * * * * * * * * * //
365 
367 (
368  "", // No title
370 );
371 
373 (
374  "", // No title
376  0,
377  true // use_stderr = true
378 );
379 
381 (
382  "--> FOAM Warning : ",
384 );
385 
387 (
388  "--> FOAM Serious Error : ",
390  100
391 );
392 
393 
394 // ************************************************************************* //
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
List of values generated by applying the access operation to each list item.
const string & title() const noexcept
The title of this error type.
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)
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:608
Warning of possible problem.
Definition: messageStream.H:81
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.
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:1061
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:421
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.
messageStream SeriousError
Error stream (stdout output on all processes), with additional &#39;FOAM Serious Error&#39; header text...
messageStream(const char *title, errorSeverity severity, int maxErrors=0, bool use_stderr=false)
Construct from components.
Definition: messageStream.C:44
OSstream & deprecated(const int afterVersion, const char *functionName=nullptr, const char *sourceFileName=nullptr, const int sourceFileLineNumber=0)
Report deprecation (after specified API version) with &#39;From function-name, source file...
OSstream Sout
OSstream wrapped stdout (std::cout)
static label warnComm
Debugging: warn for use of any communicator differing from warnComm.
Definition: UPstream.H:426
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.
Definition: messageStream.C:79
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:82
General information output (stdout)
Definition: messageStream.H:80
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1094
OSstream Serr
OSstream wrapped stderr (std::cerr)
errorSeverity
Message type, error severity flags.
Definition: messageStream.H:77
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.
int severity_
The message type / error severity, possibly with USE_STDERR mask.
A serious problem - eg, data corruption.
Definition: messageStream.H:84
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
string title_
The title of this error type.
Definition: messageStream.H:99