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-2018 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  errorSeverity sev,
40  const int maxErrors
41 )
42 :
43  title_(title),
44  severity_(sev),
45  maxErrors_(maxErrors),
46  errorCount_(0)
47 {}
48 
49 
51 :
52  title_(dict.lookup("title")),
53  severity_(FATAL),
54  maxErrors_(0),
55  errorCount_(0)
56 {}
57 
58 
60 {
61  if (UPstream::warnComm != -1 && communicator != UPstream::warnComm)
62  {
63  Pout<< "** messageStream with comm:" << communicator
64  << endl;
66  }
67 
68  if (communicator == UPstream::worldComm || UPstream::master(communicator))
69  {
70  return operator()();
71  }
72  else
73  {
74  return Snull;
75  }
76 }
77 
78 
79 Foam::OSstream& Foam::messageStream::operator()
80 (
81  const char* functionName,
82  const char* sourceFileName,
83  const int sourceFileLineNumber
84 )
85 {
86  OSstream& os = operator OSstream&();
87 
88  os << endl
89  << " From function " << functionName << endl
90  << " in file " << sourceFileName
91  << " at line " << sourceFileLineNumber << endl
92  << " ";
93 
94  return os;
95 }
96 
97 
98 Foam::OSstream& Foam::messageStream::operator()
99 (
100  const string& functionName,
101  const char* sourceFileName,
102  const int sourceFileLineNumber
103 )
104 {
105  return operator()
106  (
107  functionName.c_str(),
108  sourceFileName,
109  sourceFileLineNumber
110  );
111 }
112 
113 
114 Foam::OSstream& Foam::messageStream::operator()
115 (
116  const char* functionName,
117  const char* sourceFileName,
118  const int sourceFileLineNumber,
119  const string& ioFileName,
120  const label ioStartLineNumber,
121  const label ioEndLineNumber
122 )
123 {
124  OSstream& os = operator OSstream&();
125 
126  os << endl
127  << " From function " << functionName << endl
128  << " in file " << sourceFileName
129  << " at line " << sourceFileLineNumber << endl
130  << " Reading " << ioFileName;
131 
132  if (ioStartLineNumber >= 0 && ioEndLineNumber >= 0)
133  {
134  os << " from line " << ioStartLineNumber
135  << " to line " << ioEndLineNumber;
136  }
137  else if (ioStartLineNumber >= 0)
138  {
139  os << " at line " << ioStartLineNumber;
140  }
141 
142  os << endl << " ";
143 
144  return os;
145 }
146 
147 
148 Foam::OSstream& Foam::messageStream::operator()
149 (
150  const char* functionName,
151  const char* sourceFileName,
152  const int sourceFileLineNumber,
153  const IOstream& ioStream
154 )
155 {
156  return operator()
157  (
158  functionName,
159  sourceFileName,
160  sourceFileLineNumber,
161  ioStream.name(),
162  ioStream.lineNumber(),
163  -1
164  );
165 }
166 
167 
168 Foam::OSstream& Foam::messageStream::operator()
169 (
170  const char* functionName,
171  const char* sourceFileName,
172  const int sourceFileLineNumber,
173  const dictionary& dict
174 )
175 {
176  return operator()
177  (
178  functionName,
179  sourceFileName,
180  sourceFileLineNumber,
181  dict.name(),
182  dict.startLineNumber(),
183  dict.endLineNumber()
184  );
185 }
186 
187 
188 Foam::messageStream::operator Foam::OSstream&()
189 {
190  if (level)
191  {
192  bool collect = (severity_ == INFO || severity_ == WARNING);
193 
194  // Report the error
195  if (!Pstream::master() && collect)
196  {
197  return Snull;
198  }
199  else
200  {
201  if (title().size())
202  {
203  if (Pstream::parRun() && !collect)
204  {
205  Pout<< title().c_str();
206  }
207  else
208  {
209  Sout<< title().c_str();
210  }
211  }
212 
213  if (maxErrors_)
214  {
215  errorCount_++;
216 
217  if (errorCount_ >= maxErrors_)
218  {
220  << "Too many errors"
221  << abort(FatalError);
222  }
223  }
224 
225  if (Pstream::parRun() && !collect)
226  {
227  return Pout;
228  }
229  else
230  {
231  return Sout;
232  }
233  }
234  }
235 
236  return Snull;
237 }
238 
239 
240 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
241 
243 (
244  "--> FOAM Serious Error : ",
246  100
247 );
248 
250 (
251  "--> FOAM Warning : ",
253 );
254 
256 
258 
259 // ************************************************************************* //
static void printStack(Ostream &)
Helper function to print a stack.
Generic output stream.
Definition: OSstream.H:51
dictionary dict
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:59
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:158
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:319
bool writeInfoHeader
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
stressControl lookup("compactNormalStress") >> compactNormalStress
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:178
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
OSstream & masterStream(const label communicator)
Convert to OSstream.
Definition: messageStream.C:59
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(const string &title, errorSeverity, const int maxErrors=0)
Construct from components.
Definition: messageStream.C:37
messageStream Info
OSstream & operator()()
Explicitly convert to OSstream for << operations.