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(),
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 
236 
237 // ************************************************************************* //
An IOstream is an abstract base class for all input/output systems; be they streams,...
Definition: IOstream.H:72
Generic output stream.
Definition: OSstream.H:54
static label warnComm
Debugging: warn for use of any communicator differing from warnComm.
Definition: UPstream.H:281
static bool master(const label communicator=0)
Am I the master process.
Definition: UPstream.H:423
static label worldComm
Default communicator (all processors)
Definition: UPstream.H:278
static bool & parRun()
Is this a parallel run?
Definition: UPstream.H:399
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:109
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:160
label endLineNumber() const
Return line number of last token in dictionary.
Definition: dictionary.C:612
label startLineNumber() const
Return line number of first token in dictionary.
Definition: dictionary.C:599
static void printStack(Ostream &)
Helper function to print a stack.
A functionName is a word starting with '#'.
Definition: functionName.H:60
Class to handle messaging in a simple, consistent stream-based manner.
Definition: messageStream.H:69
errorSeverity
Severity flags.
Definition: messageStream.H:75
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
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:306
int debugSwitch(const char *name, const int defaultValue=0)
Lookup debug switch or add default value.
Definition: debug.C:211
OFstream Snull
Global predefined null output stream "/dev/null".
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
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
bool writeInfoHeader
errorManip< error > abort(error &err)
Definition: errorManip.H:131
messageStream Info
messageStream SeriousError
prefixOSstream Pout(cout, "Pout")
Definition: IOstreams.H:53
error FatalError
prefixOSstream Sout(cout, "Sout")
Definition: IOstreams.H:51
messageStream Warning
dictionary dict