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-2025 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 ioLineNumber
92 )
93 {
94  OSstream& os = operator OSstream&();
95 
96  os << endl
97  << " From function " << functionName << endl
98  << " in file " << sourceFileName
99  << " at line " << sourceFileLineNumber << endl
100  << " Reading " << ioFileName;
101 
102  if (ioLineNumber >= 0)
103  {
104  os << " at line " << ioLineNumber;
105  }
106 
107  os << endl << " ";
108 
109  return os;
110 }
111 
112 
113 Foam::OSstream& Foam::messageStream::operator()
114 (
115  const char* functionName,
116  const char* sourceFileName,
117  const int sourceFileLineNumber,
118  const IOstream& ioStream
119 )
120 {
121  return operator()
122  (
123  functionName,
124  sourceFileName,
125  sourceFileLineNumber,
126  ioStream.name(),
127  ioStream.lineNumber()
128  );
129 }
130 
131 
132 Foam::OSstream& Foam::messageStream::operator()
133 (
134  const char* functionName,
135  const char* sourceFileName,
136  const int sourceFileLineNumber,
137  const dictionary& dict
138 )
139 {
140  return operator()
141  (
142  functionName,
143  sourceFileName,
144  sourceFileLineNumber,
145  dict.name(),
147  );
148 }
149 
150 
152 {
153  if (communicator != -1)
154  {
155  if (UPstream::warnComm != -1 && communicator != UPstream::warnComm)
156  {
157  Pout<< "** messageStream with comm:" << communicator
158  << endl;
160  }
161  }
162  else
163  {
164  communicator = UPstream::worldComm;
165  }
166 
167  if (messageStream::level >= 1)
168  {
169  const bool master = Pstream::master(communicator);
170 
171  const bool collect = severity_ == INFO || severity_ == WARNING;
172 
173  const bool prefix =
174  (Pstream::parRun() && !collect)
175  || communicator != UPstream::worldComm;
176 
177  OSstream& os = prefix ? Pout : Sout;
178 
179  if (!master && collect)
180  {
181  return Snull;
182  }
183  else
184  {
185  if (title().size())
186  {
187  os << title().c_str();
188  }
189 
190  if (maxErrors_)
191  {
192  errorCount_++;
193 
194  if (errorCount_ >= maxErrors_)
195  {
197  << "Too many errors"
198  << abort(FatalError);
199  }
200  }
201 
202  return os;
203  }
204  }
205 
206  return Snull;
207 }
208 
209 
210 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
211 
213 (
214  "--> FOAM Serious Error : ",
216  100
217 );
218 
220 (
221  "--> FOAM Warning : ",
223 );
224 
226 
228 
229 // ************************************************************************* //
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:111
A list of keywords followed by any number of values (e.g. words and numbers) or sub-dictionaries.
Definition: dictionary.H:162
virtual label endLineNumber() const
Return line number of last token in dictionary.
Definition: dictionary.C:485
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:334
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:258
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