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-2025 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 "UPstream.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  errorSeverity severity,
53  int maxErrors,
54  bool use_stderr
55 )
56 :
57  severity_(severity),
58  maxErrors_(maxErrors),
59  errorCount_(0)
60 {
61  if (use_stderr)
62  {
63  severity_ |= errorSeverity::USE_STDERR;
64  }
65 }
66 
67 
69 (
70  const char* title,
71  errorSeverity severity,
72  int maxErrors,
73  bool use_stderr
74 )
75 :
76  messageStream(severity, maxErrors, use_stderr)
77 {
78  if (title)
79  {
80  title_ = title;
81  }
82 }
83 
84 
86 (
87  string title,
88  errorSeverity severity,
89  int maxErrors,
90  bool use_stderr
91 )
92 :
93  messageStream(severity, maxErrors, use_stderr)
94 {
95  title_ = std::move(title);
96 }
97 
98 
100 :
102 {}
103 
104 
105 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
106 
108 (
109  OSstream* alternative,
110  int communicator
111 )
112 {
113  if (communicator < 0)
114  {
115  communicator = UPstream::worldComm;
116  }
117 
118  if (level)
119  {
120  // Master-only output?
121  const bool masterOnly
122  (
124  || ((severity_ & ~errorSeverity::USE_STDERR) == errorSeverity::INFO)
125  || ((severity_ & ~errorSeverity::USE_STDERR) == errorSeverity::WARNING)
126  );
127 
128  if
129  (
130  masterOnly
131  && (UPstream::parRun() && !UPstream::master(communicator))
132  )
133  {
134  // Requested master-only output but is non-master (in parallel)
135  // -> early exit
136  return Snull;
137  }
138 
139 
140  // Use stderr instead of stdout:
141  // - requested via static <redirect> variable
142  // - explicit: with USE_STDERR mask
143  // - inferred: WARNING -> stderr when infoDetailLevel == 0
144  const bool use_stderr =
145  (
146  (redirect == 2)
147  || (severity_ & errorSeverity::USE_STDERR)
148  || (severity_ == errorSeverity::WARNING && Foam::infoDetailLevel == 0)
149  );
150 
151 
152  OSstream* osptr;
153 
154  if (masterOnly)
155  {
156  // Use supplied alternative? Valid for master-only output
157  osptr =
158  (
159  alternative
160  ? alternative
161  : (use_stderr ? &Serr : &Sout)
162  );
163  }
164  else
165  {
166  osptr = (use_stderr ? &Perr : &Pout);
167  }
168 
169  if (!title_.empty())
170  {
171  (*osptr) << title_.c_str();
172  }
173 
174  if (maxErrors_ && (++errorCount_ >= maxErrors_))
175  {
177  << "Too many errors..."
178  << abort(FatalError);
179  }
180 
181  return *osptr;
182  }
183 
184  return Snull;
185 }
186 
187 
189 {
190  if (communicator < 0)
191  {
192  communicator = UPstream::worldComm;
193  }
194 
195  if (UPstream::warnComm >= 0 && communicator != UPstream::warnComm)
196  {
197  Perr<< "** messageStream with comm:" << communicator << endl;
199  }
200 
201  if (communicator == UPstream::worldComm || UPstream::master(communicator))
202  {
203  return this->stream(nullptr, communicator);
204  }
205 
206  return Snull;
207 }
208 
209 
210 std::ostream& Foam::messageStream::stdStream()
211 {
212  // Currently do not need communicator != worldComm
213  return this->stream().stdStream();
214 }
215 
216 
217 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
218 
220 (
221  const int afterVersion,
222  const char* functionName,
223  const char* sourceFileName,
224  const int sourceFileLineNumber
225 )
226 {
227  OSstream& os = this->stream();
228 
229  // No warning for 0 (unversioned) or -ve values (silent versioning).
230  // Also no warning for (version >= foamVersion::api), which
231  // can be used to denote future expiry dates of transition features.
232 
233  if (afterVersion > 0 && afterVersion < foamVersion::api)
234  {
235  const int months =
236  (
237  // YYMM -> months
238  (12 * (foamVersion::api/100) + (foamVersion::api % 100))
239  - (12 * (afterVersion/100) + (afterVersion % 100))
240  );
241 
242  os << nl
243  << ">>> DEPRECATED after version " << afterVersion;
244 
245  if (afterVersion < 1000)
246  {
247  // Predates YYMM versioning (eg, 240 for version 2.4)
248  os << ". This is very old! <<<" << nl;
249  }
250  else
251  {
252  os << ". This is about " << months << " months old. <<<" << nl;
253  }
254  }
255 
256 
257  os << nl;
258  if (functionName) // nullptr check
259  {
260  {
261  os << " From " << functionName << nl;
262  }
263  if (sourceFileName) // nullptr check
264  {
265  os << " in file " << sourceFileName;
266 
267  if (sourceFileLineNumber >= 0)
268  {
269  os << " at line " << sourceFileLineNumber;
270  }
271  os << nl;
272  }
273  }
274  os << " ";
275 
276  return os;
277 }
278 
279 
280 Foam::OSstream& Foam::messageStream::operator()
281 (
282  const std::string& functionName,
283  const char* sourceFileName,
284  const int sourceFileLineNumber
285 )
286 {
287  OSstream& os = this->stream();
288 
289  if (!functionName.empty())
290  {
291  os << nl
292  << " From " << functionName.c_str() << nl;
293  }
294 
295  if (sourceFileName) // nullptr check
296  {
297  os << " in file " << sourceFileName;
298 
299  if (sourceFileLineNumber >= 0)
300  {
301  os << " at line " << sourceFileLineNumber;
302  }
303  os << nl << " ";
304  }
305 
306  return os;
307 }
308 
309 
310 Foam::OSstream& Foam::messageStream::operator()
311 (
312  const char* functionName,
313  const char* sourceFileName,
314  const int sourceFileLineNumber
315 )
316 {
317  OSstream& os = this->stream();
318 
319  if (functionName) // nullptr check
320  {
321  os << nl
322  << " From " << functionName << nl;
323  }
324 
325  if (sourceFileName) // nullptr check
326  {
327  os << " in file " << sourceFileName;
328 
329  if (sourceFileLineNumber >= 0)
330  {
331  os << " at line " << sourceFileLineNumber;
332  }
333  os << nl << " ";
334  }
335 
336  return os;
337 }
338 
339 
340 Foam::OSstream& Foam::messageStream::operator()
341 (
342  const char* functionName,
343  const char* sourceFileName,
344  const int sourceFileLineNumber,
345  const std::string& ioFileName,
346  const label ioStartLineNumber,
347  const label ioEndLineNumber
348 )
349 {
350  OSstream& os = operator()
351  (
352  functionName,
353  sourceFileName,
354  sourceFileLineNumber
355  );
356 
357  os << "Reading \"" << ioFileName.c_str() << '"';
358 
359  if (ioStartLineNumber >= 0)
360  {
361  os << " at line " << ioStartLineNumber;
362 
363  if (ioStartLineNumber < ioEndLineNumber)
364  {
365  os << " to " << ioEndLineNumber;
366  }
367  }
368 
369  os << endl << " ";
370 
371  return os;
372 }
373 
374 
375 Foam::OSstream& Foam::messageStream::operator()
376 (
377  const char* functionName,
378  const char* sourceFileName,
379  const int sourceFileLineNumber,
380  const IOstream& ioStream
381 )
382 {
383  return operator()
384  (
385  functionName,
386  sourceFileName,
387  sourceFileLineNumber,
388  ioStream.relativeName(),
389  ioStream.lineNumber(),
390  -1 // No known endLineNumber
391  );
392 }
393 
394 
395 Foam::OSstream& Foam::messageStream::operator()
396 (
397  const char* functionName,
398  const char* sourceFileName,
399  const int sourceFileLineNumber,
400  const dictionary& dict
401 )
402 {
403  return operator()
404  (
405  functionName,
406  sourceFileName,
407  sourceFileLineNumber,
408  dict.relativeName(),
411  );
412 }
413 
414 
415 // * * * * * * * * * * * * * * * Global Variables * * * * * * * * * * * * * //
416 
418 (
419  // No title
421 );
422 
424 (
425  // No title
427  0,
428  true // use_stderr = true
429 );
430 
432 (
433  "--> FOAM Warning : ",
435 );
436 
438 (
439  "--> FOAM Serious Error : ",
441  100
442 );
443 
444 
445 // ************************************************************************* //
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:600
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:529
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:1586
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:987
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...
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)
OSstream & stream(OSstream *alternative=nullptr, int communicator=-1)
Return OSstream for output operations.
static label warnComm
Debugging: warn for use of any communicator differing from warnComm.
Definition: UPstream.H:992
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...
messageStream(errorSeverity severity, int maxErrors=0, bool use_stderr=false)
Construct untitled with given characteristics.
Definition: messageStream.C:44
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...
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
OSstream & masterStream(int communicator)
Return OSstream for output operations on the master process only, Snull on other processes.
static bool master(const label communicator=worldComm)
True if process corresponds to the master rank in the communicator.
Definition: UPstream.H:1619
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 class for handling character strings derived from std::string.
Definition: string.H:72
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