messageStream.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration | Website: https://openfoam.org
5  \\ / A nd | Copyright (C) 2011-2021 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 \*---------------------------------------------------------------------------*/
25 
26 #include "error.H"
27 #include "dictionary.H"
28 #include "Pstream.H"
29 
30 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
31 
33 
34 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
35 
37 (
38  const string& title,
39  const errorSeverity sev,
40  const int maxErrors
41 )
42 :
43  title_(title),
44  severity_(sev),
45  maxErrors_(maxErrors),
46  errorCount_(0)
47 {}
48 
49 
50 Foam::OSstream& Foam::messageStream::operator()
51 (
52  const char* functionName,
53  const char* sourceFileName,
54  const int sourceFileLineNumber
55 )
56 {
57  OSstream& os = operator OSstream&();
58 
59  os << endl
60  << " From function " << functionName << endl
61  << " in file " << sourceFileName
62  << " at line " << sourceFileLineNumber << endl
63  << " ";
64 
65  return os;
66 }
67 
68 
69 Foam::OSstream& Foam::messageStream::operator()
70 (
71  const string& functionName,
72  const char* sourceFileName,
73  const int sourceFileLineNumber
74 )
75 {
76  return operator()
77  (
78  functionName.c_str(),
79  sourceFileName,
80  sourceFileLineNumber
81  );
82 }
83 
84 
85 Foam::OSstream& Foam::messageStream::operator()
86 (
87  const char* functionName,
88  const char* sourceFileName,
89  const int sourceFileLineNumber,
90  const string& ioFileName,
91  const label ioStartLineNumber,
92  const label ioEndLineNumber
93 )
94 {
95  OSstream& os = operator OSstream&();
96 
97  os << endl
98  << " From function " << functionName << endl
99  << " in file " << sourceFileName
100  << " at line " << sourceFileLineNumber << endl
101  << " Reading " << ioFileName;
102 
103  if (ioStartLineNumber >= 0 && ioEndLineNumber >= 0)
104  {
105  os << " from line " << ioStartLineNumber
106  << " to line " << ioEndLineNumber;
107  }
108  else if (ioStartLineNumber >= 0)
109  {
110  os << " at line " << ioStartLineNumber;
111  }
112 
113  os << endl << " ";
114 
115  return os;
116 }
117 
118 
119 Foam::OSstream& Foam::messageStream::operator()
120 (
121  const char* functionName,
122  const char* sourceFileName,
123  const int sourceFileLineNumber,
124  const IOstream& ioStream
125 )
126 {
127  return operator()
128  (
129  functionName,
130  sourceFileName,
131  sourceFileLineNumber,
132  ioStream.name(),
133  ioStream.lineNumber(),
134  -1
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 dictionary& dict
145 )
146 {
147  return operator()
148  (
149  functionName,
150  sourceFileName,
151  sourceFileLineNumber,
152  dict.name(),
153  dict.startLineNumber(),
154  dict.endLineNumber()
155  );
156 }
157 
158 
160 {
161  if (communicator != -1)
162  {
163  if (UPstream::warnComm != -1 && communicator != UPstream::warnComm)
164  {
165  Pout<< "** messageStream with comm:" << communicator
166  << endl;
168  }
169  }
170  else
171  {
172  communicator = UPstream::worldComm;
173  }
174 
175  if (level)
176  {
177  const bool master = Pstream::master(communicator);
178 
179  const bool collect = severity_ == INFO || severity_ == WARNING;
180 
181  const bool prefix =
182  (Pstream::parRun() && !collect)
183  || communicator != UPstream::worldComm;
184 
185  OSstream& os = prefix ? Pout : Sout;
186 
187  if (!master && collect)
188  {
189  return Snull;
190  }
191  else
192  {
193  if (title().size())
194  {
195  os << title().c_str();
196  }
197 
198  if (maxErrors_)
199  {
200  errorCount_++;
201 
202  if (errorCount_ >= maxErrors_)
203  {
205  << "Too many errors"
206  << abort(FatalError);
207  }
208  }
209 
210  return os;
211  }
212  }
213 
214  return Snull;
215 }
216 
217 
218 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
219 
221 (
222  "--> FOAM Serious Error : ",
224  100
225 );
226 
228 (
229  "--> FOAM Warning : ",
231 );
232 
234 
235 bool Foam::writeInfoHeader = true;
236 
237 // ************************************************************************* //
static void printStack(Ostream &)
Helper function to print a stack.
Generic output stream.
Definition: OSstream.H:51
dictionary dict
errorSeverity
Severity flags.
Definition: messageStream.H:74
error FatalError
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:156
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
const string & title() const
Return the title of this error type.
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:423
Class to handle messaging in a simple, consistent stream-based manner.
Definition: messageStream.H:68
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:278
OSstream & operator()(const char *functionName, const char *sourceFileName, const int sourceFileLineNumber=0)
Convert to OSstream.
Definition: messageStream.C:51
messageStream(const string &title, const errorSeverity, const int maxErrors=0)
Construct from components.
Definition: messageStream.C:37
errorSeverity severity_
Definition: messageStream.H:88
OFstream Snull
Global predefined null output stream "/dev/null".
messageStream SeriousError
int debugSwitch(const char *name, const int defaultValue=0)
Lookup debug switch or add default value.
Definition: debug.C:211
OSstream Sout(cout, "Sout")
Definition: IOstreams.H:51
static label warnComm
Debugging: warn for use of any communicator differing from warnComm.
Definition: UPstream.H:281
errorManip< error > abort(error &err)
Definition: errorManip.H:131
messageStream Warning
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:399
An IOstream is an abstract base class for all input/output systems; be they streams, files, token lists etc.
Definition: IOstream.H:71
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
messageStream Info