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(),
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  {
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 // ************************************************************************* //
Foam::messageStream::WARNING
@ WARNING
Definition: messageStream.H:77
Foam::UPstream::warnComm
static label warnComm
Debugging: warn for use of any communicator differing from warnComm.
Definition: UPstream.H:261
Foam::Snull
OFstream Snull
Global predefined null output stream "/dev/null".
Foam::messageStream
Class to handle messaging in a simple, consistent stream-based manner.
Definition: messageStream.H:68
Foam::dictionaryName::name
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:103
Foam::messageStream::errorSeverity
errorSeverity
Severity flags.
Definition: messageStream.H:74
Foam::messageStream::masterStream
OSstream & masterStream(const label communicator)
Convert to OSstream.
Definition: messageStream.C:60
Foam::Warning
messageStream Warning
Foam::UPstream::parRun
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:377
Foam::messageStream::level
static int level
Definition: messageStream.H:97
Foam::IOstream
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:71
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::messageStream::messageStream
messageStream(const string &title, errorSeverity, const int maxErrors=0)
Construct from components.
Definition: messageStream.C:38
Foam::error::printStack
static void printStack(Ostream &)
Helper function to print a stack.
Definition: dummyPrintStack.C:30
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:59
error.H
Foam::Info
messageStream Info
Foam::dictionary::startLineNumber
label startLineNumber() const
Return line number of first token in dictionary.
Definition: dictionary.C:244
Foam::dictionary::endLineNumber
label endLineNumber() const
Return line number of last token in dictionary.
Definition: dictionary.C:257
Foam::OSstream
Generic output stream.
Definition: OSstream.H:51
dict
dictionary dict
Definition: searchingEngine.H:14
Foam::FatalError
error FatalError
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Pstream.H
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:131
Foam::debug::debugSwitch
int debugSwitch(const char *name, const int defaultValue=0)
Lookup debug switch or add default value.
Definition: debug.C:164
Foam::UPstream::master
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:399
Foam::SeriousError
messageStream SeriousError
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:318
Foam::Pout
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
Foam::UPstream::worldComm
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:258
dictionary.H
Foam::messageStream::SERIOUS
@ SERIOUS
Definition: messageStream.H:78
Foam::Sout
OSstream Sout(cout, "Sout")
Definition: IOstreams.H:51
Foam::messageStream::INFO
@ INFO
Definition: messageStream.H:76
lookup
stressControl lookup("compactNormalStress") >> compactNormalStress