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