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 
38 // Default is 1 : report to Info
40 
41 
42 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
43 
45 (
46  const string& title,
47  const errorSeverity severity,
48  const int maxErrors
49 )
50 :
51  title_(title),
52  severity_(severity),
53  maxErrors_(maxErrors),
54  errorCount_(0)
55 {}
56 
57 
59 :
60  title_(dict.get<string>("title")),
61  severity_(FATAL),
62  maxErrors_(0),
63  errorCount_(0)
64 {}
65 
66 
67 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
68 
70 {
71  if (UPstream::warnComm != -1 && communicator != UPstream::warnComm)
72  {
73  Pout<< "** messageStream with comm:" << communicator << endl;
75  }
76 
77  if (communicator == UPstream::worldComm || UPstream::master(communicator))
78  {
79  return operator()();
80  }
81 
82  return Snull;
83 }
84 
85 
86 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
87 
88 Foam::OSstream& Foam::messageStream::operator()
89 (
90  const string& functionName
91 )
92 {
93  OSstream& os = operator OSstream&();
94 
95  os << nl
96  << " From " << functionName.c_str() << nl;
97 
98  return os;
99 }
100 
101 
102 Foam::OSstream& Foam::messageStream::operator()
103 (
104  const char* functionName,
105  const char* sourceFileName,
106  const int sourceFileLineNumber
107 )
108 {
109  OSstream& os = operator OSstream&();
110 
111  os << nl
112  << " From " << functionName << nl
113  << " in file " << sourceFileName
114  << " at line " << sourceFileLineNumber << endl
115  << " ";
116 
117  return os;
118 }
119 
120 
121 Foam::OSstream& Foam::messageStream::operator()
122 (
123  const string& functionName,
124  const char* sourceFileName,
125  const int sourceFileLineNumber
126 )
127 {
128  return operator()
129  (
130  functionName.c_str(),
131  sourceFileName,
132  sourceFileLineNumber
133  );
134 }
135 
136 
137 Foam::OSstream& Foam::messageStream::operator()
138 (
139  const char* functionName,
140  const char* sourceFileName,
141  const int sourceFileLineNumber,
142  const string& ioFileName,
143  const label ioStartLineNumber,
144  const label ioEndLineNumber
145 )
146 {
147  OSstream& os = operator OSstream&();
148 
149  os << nl
150  << " From " << functionName << nl
151  << " in file " << sourceFileName
152  << " at line " << sourceFileLineNumber << nl
153  << " Reading " << ioFileName;
154 
155  if (ioStartLineNumber >= 0)
156  {
157  os << " at line " << ioStartLineNumber;
158 
159  if (ioStartLineNumber < ioEndLineNumber)
160  {
161  os << " to " << ioEndLineNumber;
162  }
163  }
164 
165  os << endl << " ";
166 
167  return os;
168 }
169 
170 
171 Foam::OSstream& Foam::messageStream::operator()
172 (
173  const char* functionName,
174  const char* sourceFileName,
175  const int sourceFileLineNumber,
176  const IOstream& ioStream
177 )
178 {
179  return operator()
180  (
181  functionName,
182  sourceFileName,
183  sourceFileLineNumber,
184  ioStream.name(),
185  ioStream.lineNumber(),
186  -1
187  );
188 }
189 
190 
191 Foam::OSstream& Foam::messageStream::operator()
192 (
193  const char* functionName,
194  const char* sourceFileName,
195  const int sourceFileLineNumber,
196  const dictionary& dict
197 )
198 {
199  return operator()
200  (
201  functionName,
202  sourceFileName,
203  sourceFileLineNumber,
204  dict.name(),
207  );
208 }
209 
210 
211 Foam::messageStream::operator Foam::OSstream&()
212 {
213  if (level)
214  {
215  // stderr instead of stdout
216  // - INFO_STDERR
217  // - WARNING when infoDetailLevel == 0
218  const bool useSerr =
219  (
220  (severity_ == INFO_STDERR)
221  || (severity_ == WARNING && Foam::infoDetailLevel == 0)
222  );
223 
224  const bool collect =
225  (
226  severity_ == INFO
227  || severity_ == WARNING
228  || useSerr
229  );
230 
231 
232  // Could add guard with parRun
233  if (collect && !Pstream::master())
234  {
235  return Snull;
236  }
237 
238 
239  OSstream& os =
240  (
241  (collect || !Pstream::parRun())
242  ? (useSerr ? Serr : Sout)
243  : 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:288
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:69
Foam::Warning
messageStream Warning
Foam::UPstream::parRun
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:415
Foam::messageStream::level
static int level
Control the output verbosity of messageStream.
Definition: messageStream.H:107
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:75
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:350
Foam::Serr
OSstream Serr
An Ostream wrapper for std::cerr.
Foam::string
A class for handling character strings derived from std::string.
Definition: string.H:73
Foam::Pout
prefixOSstream Pout
An Ostream wrapper for parallel output to std::cout.
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:137
Foam::infoDetailLevel
int infoDetailLevel
Global for selective suppression of Info output.
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:439
Foam::SeriousError
messageStream SeriousError
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:372
Foam::nl
constexpr char nl
Definition: Ostream.H:385
Foam::UPstream::worldComm
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:285
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:45
Foam::Snull
OFstream Snull
Global predefined null output stream "/dev/null".
Foam::Sout
OSstream Sout
An Ostream wrapper for std::cout.
Foam::messageStream::INFO
General information output.
Definition: messageStream.H:78
Foam::messageStream::INFO_STDERR
Information, but on stderr.
Definition: messageStream.H:82