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 -------------------------------------------------------------------------------
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 \*---------------------------------------------------------------------------*/
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  )
83  || !UPstream::parRun()
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 
165 Foam::OSstream& Foam::messageStream::operator()
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 
182 Foam::OSstream& Foam::messageStream::operator()
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 
201 Foam::OSstream& Foam::messageStream::operator()
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 
217 Foam::OSstream& Foam::messageStream::operator()
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 
251 Foam::OSstream& Foam::messageStream::operator()
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 
271 Foam::OSstream& Foam::messageStream::operator()
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,
284  dict.relativeName(),
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 // ************************************************************************* //
Foam::messageStream::WARNING
Warning of possible problem.
Definition: messageStream.H:83
Foam::UPstream::warnComm
static label warnComm
Debugging: warn for use of any communicator differing from warnComm.
Definition: UPstream.H:296
Foam::error::printStack
static void printStack(Ostream &os)
Helper function to print a stack.
Definition: dummyPrintStack.C:36
Foam::messageStream
Handle output messages in a simple, consistent stream-based manner.
Definition: messageStream.H:73
Foam::messageStream::errorSeverity
errorSeverity
Message type, error severity flags.
Definition: messageStream.H:78
Foam::messageStream::masterStream
OSstream & masterStream(const label communicator)
Definition: messageStream.C:140
Foam::Warning
messageStream Warning
Foam::messageStream::level
static int level
The output level (verbosity) of messages.
Definition: messageStream.H:112
Foam::messageStream::redirect
static int redirect
The output redirection of messages.
Definition: messageStream.H:117
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:79
Foam::UPstream::master
static bool master(const label communicator=worldComm)
Am I the master process.
Definition: UPstream.H:457
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:369
Foam::Serr
OSstream Serr
OSstream wrapped stderr (std::cerr)
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:76
Foam::Pout
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Foam::InfoErr
messageStream InfoErr
Information stream (stderr output on master, null elsewhere)
Foam::debug::infoSwitch
int infoSwitch(const char *name, const int deflt=0)
Lookup info switch or add default value.
Definition: debug.C:231
error.H
Foam::messageStream::stream
OSstream & stream(OSstream *alternative=nullptr)
Definition: messageStream.C:71
Foam::Info
messageStream Info
Information stream (stdout output on master, null elsewhere)
Foam::dictionary::relativeName
fileName relativeName(const bool caseTag=false) const
The dictionary name relative to the case.
Definition: dictionary.C:186
Foam::dictionary::startLineNumber
label startLineNumber() const
Return line number of first token in dictionary.
Definition: dictionary.C:205
Foam::dictionary::endLineNumber
label endLineNumber() const
Return line number of last token in dictionary.
Definition: dictionary.C:216
Foam::OSstream
Generic output stream using a standard (STL) stream.
Definition: OSstream.H:54
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by a number of values (eg,...
Definition: dictionary.H:123
os
OBJstream os(runTime.globalPath()/outputName)
Pstream.H
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
Foam::infoDetailLevel
int infoDetailLevel
Global for selective suppression of Info output.
Foam::Perr
prefixOSstream Perr
OSstream wrapped stderr (std::cerr) with parallel prefix.
Foam::SeriousError
messageStream SeriousError
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::nl
constexpr char nl
Definition: Ostream.H:404
Foam::UPstream::worldComm
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:293
Foam::UPstream::parRun
static bool & parRun() noexcept
Test if this a parallel run.
Definition: UPstream.H:433
dictionary.H
Foam::messageStream::SERIOUS
A serious problem - eg, data corruption.
Definition: messageStream.H:86
Foam::PtrListOps::get
List< ReturnType > get(const UPtrList< T > &list, const AccessOp &aop)
Foam::messageStream::messageStream
messageStream(const string &title, const errorSeverity severity, const int maxErrors=0)
Construct from components.
Definition: messageStream.C:47
Foam::Snull
OFstream Snull
Global predefined null output stream "/dev/null".
Foam::messageStream::stdStream
std::ostream & stdStream()
Return std::ostream for output operations.
Definition: messageStream.C:157
Foam::Sout
OSstream Sout
OSstream wrapped stdout (std::cout)
Foam::messageStream::INFO
General information output (stdout)
Definition: messageStream.H:81
Foam::messageStream::INFO_STDERR
General information output (stderr)
Definition: messageStream.H:82