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-2020 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 (UPstream::warnComm != -1 && communicator != UPstream::warnComm)
74  {
75  Pout<< "** messageStream with comm:" << communicator << endl;
77  }
78 
79  if (communicator == UPstream::worldComm || UPstream::master(communicator))
80  {
81  return operator()();
82  }
83 
84  return Snull;
85 }
86 
87 
88 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
89 
90 Foam::OSstream& Foam::messageStream::operator()
91 (
92  const string& functionName
93 )
94 {
95  OSstream& os = operator OSstream&();
96 
97  os << nl
98  << " From " << functionName.c_str() << nl;
99 
100  return os;
101 }
102 
103 
104 Foam::OSstream& Foam::messageStream::operator()
105 (
106  const char* functionName,
107  const char* sourceFileName,
108  const int sourceFileLineNumber
109 )
110 {
111  OSstream& os = operator OSstream&();
112 
113  os << nl
114  << " From " << functionName << nl
115  << " in file " << sourceFileName
116  << " at line " << sourceFileLineNumber << endl
117  << " ";
118 
119  return os;
120 }
121 
122 
123 Foam::OSstream& Foam::messageStream::operator()
124 (
125  const string& functionName,
126  const char* sourceFileName,
127  const int sourceFileLineNumber
128 )
129 {
130  return operator()
131  (
132  functionName.c_str(),
133  sourceFileName,
134  sourceFileLineNumber
135  );
136 }
137 
138 
139 Foam::OSstream& Foam::messageStream::operator()
140 (
141  const char* functionName,
142  const char* sourceFileName,
143  const int sourceFileLineNumber,
144  const string& ioFileName,
145  const label ioStartLineNumber,
146  const label ioEndLineNumber
147 )
148 {
149  OSstream& os = operator OSstream&();
150 
151  os << nl
152  << " From " << functionName << nl
153  << " in file " << sourceFileName
154  << " at line " << sourceFileLineNumber << nl
155  << " Reading " << ioFileName;
156 
157  if (ioStartLineNumber >= 0)
158  {
159  os << " at line " << ioStartLineNumber;
160 
161  if (ioStartLineNumber < ioEndLineNumber)
162  {
163  os << " to " << ioEndLineNumber;
164  }
165  }
166 
167  os << endl << " ";
168 
169  return os;
170 }
171 
172 
173 Foam::OSstream& Foam::messageStream::operator()
174 (
175  const char* functionName,
176  const char* sourceFileName,
177  const int sourceFileLineNumber,
178  const IOstream& ioStream
179 )
180 {
181  return operator()
182  (
183  functionName,
184  sourceFileName,
185  sourceFileLineNumber,
186  ioStream.name(),
187  ioStream.lineNumber(),
188  -1
189  );
190 }
191 
192 
193 Foam::OSstream& Foam::messageStream::operator()
194 (
195  const char* functionName,
196  const char* sourceFileName,
197  const int sourceFileLineNumber,
198  const dictionary& dict
199 )
200 {
201  return operator()
202  (
203  functionName,
204  sourceFileName,
205  sourceFileLineNumber,
206  dict.name(),
209  );
210 }
211 
212 
213 Foam::messageStream::operator Foam::OSstream&()
214 {
215  if (level)
216  {
217  const bool collect =
218  (
219  severity_ == INFO
220  || severity_ == WARNING
221  || severity_ == INFO_STDERR
222  );
223 
224  // Could add guard with parRun
225  if (collect && !Pstream::master())
226  {
227  return Snull;
228  }
229 
230  // Use stderr instead of stdout
231  // - INFO_STDERR
232  // - WARNING when infoDetailLevel == 0
233  const bool useStderr =
234  (
235  (severity_ == INFO_STDERR)
236  || (severity_ == WARNING && Foam::infoDetailLevel == 0)
237  );
238 
239  OSstream& os =
240  (
241  (collect || !Pstream::parRun())
242  ? (useStderr ? Serr : Sout)
243  : (useStderr ? Perr : Pout)
244  );
245 
246 
247  if (!title().empty())
248  {
249  os << title().c_str();
250  }
251 
252  if (maxErrors_ && (++errorCount_ >= maxErrors_))
253  {
255  << "Too many errors"
256  << abort(FatalError);
257  }
258 
259  return os;
260  }
261 
262  return Snull;
263 }
264 
265 
266 // * * * * * * * * * * * * * * * Global Variables * * * * * * * * * * * * * //
267 
269 
271 
273 (
274  "--> FOAM Warning : ",
276 );
277 
279 (
280  "--> FOAM Serious Error : ",
282  100
283 );
284 
285 
286 // ************************************************************************* //
Foam::messageStream::WARNING
Warning of possible problem.
Definition: messageStream.H:79
Foam::UPstream::warnComm
static label warnComm
Debugging: warn for use of any communicator differing from warnComm.
Definition: UPstream.H:298
Foam::error::printStack
static void printStack(Ostream &os)
Helper function to print a stack.
Definition: dummyPrintStack.C:36
Foam::messageStream
Class to handle messaging in a simple, consistent stream-based manner.
Definition: messageStream.H:71
Foam::debug::debugSwitch
int debugSwitch(const char *name, const int deflt=0)
Lookup debug switch or add default value.
Definition: debug.C:225
Foam::messageStream::errorSeverity
errorSeverity
Message type, or error severity flags.
Definition: messageStream.H:76
Foam::messageStream::masterStream
OSstream & masterStream(const label communicator)
Convert to OSstream.
Definition: messageStream.C:71
Foam::Warning
messageStream Warning
Foam::UPstream::parRun
static bool & parRun()
Test if this a parallel run, or allow modify access.
Definition: UPstream.H:434
Foam::messageStream::level
static int level
The output level (verbosity) of messages.
Definition: messageStream.H:107
Foam::messageStream::redirect
static int redirect
The output redirection of messages.
Definition: messageStream.H:112
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:75
Foam::UPstream::master
static bool master(const label communicator=worldComm)
Am I the master process.
Definition: UPstream.H:458
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::Serr
OSstream Serr
OSstream wrapped stderr (std::cerr)
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:73
Foam::Pout
prefixOSstream Pout
OSstream wrapped stdout (std::cout) with parallel prefix.
Foam::InfoErr
messageStream InfoErr
Information stream (uses stderr - output is on the master only)
Foam::dictionary::name
const fileName & name() const
The dictionary name.
Definition: dictionary.H:446
error.H
Foam::Info
messageStream Info
Information stream (uses stdout - output is on the master only)
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:121
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:381
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::UPstream::worldComm
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:295
dictionary.H
Foam::messageStream::SERIOUS
A serious problem - eg, data corruption.
Definition: messageStream.H:80
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::Sout
OSstream Sout
OSstream wrapped stdout (std::cout)
Foam::messageStream::INFO
General information output.
Definition: messageStream.H:78
Foam::messageStream::INFO_STDERR
Information, but on stderr.
Definition: messageStream.H:82