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-2019 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 char* functionName,
91  const char* sourceFileName,
92  const int sourceFileLineNumber
93 )
94 {
95  OSstream& os = operator OSstream&();
96 
97  os << nl
98  << " From function " << functionName << nl
99  << " in file " << sourceFileName
100  << " at line " << sourceFileLineNumber << endl
101  << " ";
102 
103  return os;
104 }
105 
106 
107 Foam::OSstream& Foam::messageStream::operator()
108 (
109  const string& functionName,
110  const char* sourceFileName,
111  const int sourceFileLineNumber
112 )
113 {
114  return operator()
115  (
116  functionName.c_str(),
117  sourceFileName,
118  sourceFileLineNumber
119  );
120 }
121 
122 
123 Foam::OSstream& Foam::messageStream::operator()
124 (
125  const char* functionName,
126  const char* sourceFileName,
127  const int sourceFileLineNumber,
128  const string& ioFileName,
129  const label ioStartLineNumber,
130  const label ioEndLineNumber
131 )
132 {
133  OSstream& os = operator OSstream&();
134 
135  os << nl
136  << " From function " << functionName << nl
137  << " in file " << sourceFileName
138  << " at line " << sourceFileLineNumber << nl
139  << " Reading " << ioFileName;
140 
141  if (ioStartLineNumber >= 0)
142  {
143  if (ioStartLineNumber < ioEndLineNumber)
144  {
145  os << " from line " << ioStartLineNumber
146  << " to line " << ioEndLineNumber;
147  }
148  else
149  {
150  os << " at line " << ioStartLineNumber;
151  }
152  }
153 
154  os << endl << " ";
155 
156  return os;
157 }
158 
159 
160 Foam::OSstream& Foam::messageStream::operator()
161 (
162  const char* functionName,
163  const char* sourceFileName,
164  const int sourceFileLineNumber,
165  const IOstream& ioStream
166 )
167 {
168  return operator()
169  (
170  functionName,
171  sourceFileName,
172  sourceFileLineNumber,
173  ioStream.name(),
174  ioStream.lineNumber(),
175  -1
176  );
177 }
178 
179 
180 Foam::OSstream& Foam::messageStream::operator()
181 (
182  const char* functionName,
183  const char* sourceFileName,
184  const int sourceFileLineNumber,
185  const dictionary& dict
186 )
187 {
188  return operator()
189  (
190  functionName,
191  sourceFileName,
192  sourceFileLineNumber,
193  dict.name(),
196  );
197 }
198 
199 
200 Foam::messageStream::operator Foam::OSstream&()
201 {
202  if (level)
203  {
204  const bool collect =
205  (
206  severity_ == INFO
207  || severity_ == INFO_STDERR
208  || severity_ == WARNING
209  );
210 
211 
212  // Could add guard with parRun
213  if (collect && !Pstream::master())
214  {
215  return Snull;
216  }
217 
218  OSstream& os =
219  (
220  (collect || !Pstream::parRun())
221  ? ((severity_ == INFO_STDERR) ? Serr : Sout)
222  : Pout
223  );
224 
225 
226  if (!title().empty())
227  {
228  os << title().c_str();
229  }
230 
231  if (maxErrors_ && (++errorCount_ >= maxErrors_))
232  {
234  << "Too many errors"
235  << abort(FatalError);
236  }
237 
238  return os;
239  }
240 
241  return Snull;
242 }
243 
244 
245 // * * * * * * * * * * * * * * * Global Variables * * * * * * * * * * * * * //
246 
248 
250 
252 (
253  "--> FOAM Warning : ",
255 );
256 
258 (
259  "--> FOAM Serious Error : ",
261  100
262 );
263 
264 
265 // ************************************************************************* //
Foam::messageStream::WARNING
Warning of possible problem.
Definition: messageStream.H:80
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:77
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:414
Foam::messageStream::level
static int level
Controls the output verbosity of messageStream.
Definition: messageStream.H:108
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:337
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
Foam::label
intWM_LABEL_SIZE_t label
A label is an int32_t or int64_t as specified by the pre-processor macro WM_LABEL_SIZE.
Definition: label.H:62
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.
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:438
Foam::SeriousError
messageStream SeriousError
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:355
Foam::nl
constexpr char nl
Definition: Ostream.H:372
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:81
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:79
Foam::messageStream::INFO_STDERR
Information, but on stderr.
Definition: messageStream.H:83