debug.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 Description
25  Class for handling debugging switches.
26 
27 \*---------------------------------------------------------------------------*/
28 
29 #include "debug.H"
30 #include "dictionary.H"
31 #include "IFstream.H"
32 #include "OSspecific.H"
33 #include "Ostream.H"
34 #include "demandDrivenData.H"
35 #include "simpleObjectRegistry.H"
36 
37 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
38 
39 namespace Foam
40 {
41 namespace debug
42 {
43 
45 dictionary* controlDictPtr_(NULL);
46 dictionary* debugSwitchesPtr_(NULL);
47 dictionary* infoSwitchesPtr_(NULL);
48 dictionary* optimisationSwitchesPtr_(NULL);
49 
50 // Debug switch read and write callback tables.
51 simpleObjectRegistry* debugObjectsPtr_(NULL);
52 simpleObjectRegistry* infoObjectsPtr_(NULL);
53 simpleObjectRegistry* optimisationObjectsPtr_(NULL);
54 simpleObjectRegistry* dimensionSetObjectsPtr_(NULL);
55 simpleObjectRegistry* dimensionedConstantObjectsPtr_(NULL);
56 
57 
58 // to ensure controlDictPtr_ is deleted at the end of the run
59 class deleteControlDictPtr
60 {
61 public:
62 
63  deleteControlDictPtr()
64  {}
65 
66  ~deleteControlDictPtr()
67  {
68  deleteDemandDrivenData(debugObjectsPtr_);
69  deleteDemandDrivenData(infoObjectsPtr_);
70  deleteDemandDrivenData(optimisationObjectsPtr_);
71  deleteDemandDrivenData(dimensionSetObjectsPtr_);
72  deleteDemandDrivenData(dimensionedConstantObjectsPtr_);
73 
74  debugSwitchesPtr_ = NULL;
75  infoSwitchesPtr_ = NULL;
76  optimisationSwitchesPtr_ = NULL;
77  deleteDemandDrivenData(controlDictPtr_);
78  }
79 };
80 
81 deleteControlDictPtr deleteControlDictPtr_;
83 
84 
85 } // End namespace debug
86 } // End namespace Foam
87 
88 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
89 
91 {
92  if (!controlDictPtr_)
93  {
94  fileNameList controlDictFiles = findEtcFiles("controlDict", true);
95  controlDictPtr_ = new dictionary();
96  forAllReverse(controlDictFiles, cdfi)
97  {
98  IFstream ifs(controlDictFiles[cdfi]);
99 
100  if (!ifs.good())
101  {
103  (
104  "debug::controlDict()",
105  ifs,
106  "Cannot open controlDict"
107  );
108  }
109  controlDictPtr_->merge(dictionary(ifs));
110  }
111  }
112 
113  return *controlDictPtr_;
114 }
115 
116 
118 (
119  const char* subDictName,
120  dictionary*& subDictPtr
121 )
122 {
123  if (!subDictPtr)
124  {
126  (
127  subDictName, false, false
128  );
129 
130  if (!ePtr || !ePtr->isDict())
131  {
132  cerr<< "debug::switchSet(const char*, dictionary*&):\n"
133  << " Cannot find " << subDictName << " in dictionary "
134  << controlDict().name().c_str()
135  << std::endl << std::endl;
136 
137  ::exit(1);
138  }
139 
140  subDictPtr = &ePtr->dict();
141  }
142 
143  return *subDictPtr;
144 }
145 
146 
148 {
149  return switchSet("DebugSwitches", debugSwitchesPtr_);
150 }
151 
152 
154 {
155  return switchSet("InfoSwitches", infoSwitchesPtr_);
156 }
157 
158 
160 {
161  return switchSet("OptimisationSwitches", optimisationSwitchesPtr_);
162 }
163 
164 
165 int Foam::debug::debugSwitch(const char* name, const int defaultValue)
166 {
168  (
169  name, defaultValue, false, false
170  );
171 }
172 
173 
174 int Foam::debug::infoSwitch(const char* name, const int defaultValue)
175 {
177  (
178  name, defaultValue, false, false
179  );
180 }
181 
182 
183 int Foam::debug::optimisationSwitch(const char* name, const int defaultValue)
184 {
186  (
187  name, defaultValue, false, false
188  );
189 }
190 
191 
192 void Foam::debug::addDebugObject(const char* name, simpleRegIOobject* obj)
193 {
195  if (ptr)
196  {
197  ptr->append(obj);
198  }
199  else
200  {
202  (
203  name,
205  (
207  )
208  );
209  }
210 }
211 
212 
213 void Foam::debug::addInfoObject(const char* name, simpleRegIOobject* obj)
214 {
216  if (ptr)
217  {
218  ptr->append(obj);
219  }
220  else
221  {
223  (
224  name,
226  (
228  )
229  );
230  }
231 }
232 
233 
235 (
236  const char* name,
237  simpleRegIOobject* obj
238 )
239 {
241  if (ptr)
242  {
243  ptr->append(obj);
244  }
245  else
246  {
248  (
249  name,
251  (
253  )
254  );
255  }
256 }
257 
258 
260 (
261  const char* name,
262  simpleRegIOobject* obj
263 )
264 {
266  if (ptr)
267  {
268  ptr->append(obj);
269  }
270  else
271  {
273  (
274  name,
276  (
278  )
279  );
280  }
281 }
282 
283 
285 (
286  const char* name,
287  simpleRegIOobject* obj
288 )
289 {
291  (
292  name
293  );
294  if (ptr)
295  {
296  ptr->append(obj);
297  }
298  else
299  {
301  (
302  name,
304  (
306  )
307  );
308  }
309 }
310 
311 
313 {
314  if (!debugObjectsPtr_)
315  {
316  debugObjectsPtr_ = new simpleObjectRegistry(1000);
317  }
318 
319  return *debugObjectsPtr_;
320 }
321 
322 
324 {
325  if (!infoObjectsPtr_)
326  {
327  infoObjectsPtr_ = new simpleObjectRegistry(100);
328  }
329 
330  return *infoObjectsPtr_;
331 }
332 
333 
335 {
336  if (!optimisationObjectsPtr_)
337  {
338  optimisationObjectsPtr_ = new simpleObjectRegistry(100);
339  }
340 
341  return *optimisationObjectsPtr_;
342 }
343 
344 
346 {
347  if (!dimensionSetObjectsPtr_)
348  {
349  dimensionSetObjectsPtr_ = new simpleObjectRegistry(100);
350  }
351 
352  return *dimensionSetObjectsPtr_;
353 }
354 
355 
357 {
358  if (!dimensionedConstantObjectsPtr_)
359  {
360  dimensionedConstantObjectsPtr_ = new simpleObjectRegistry(100);
361  }
362 
363  return *dimensionedConstantObjectsPtr_;
364 }
365 
366 
367 // ************************************************************************* //
int debugSwitch(const char *name, const int defaultValue=0)
Lookup debug switch or add default value.
Definition: debug.C:165
virtual bool isDict() const
Return true if this entry is a dictionary.
Definition: entry.H:153
int infoSwitch(const char *name, const int defaultValue=0)
Lookup info switch or add default value.
Definition: debug.C:174
void append(const word &, T *)
Add at tail of dictionary.
fileNameList findEtcFiles(const fileName &, bool mandatory=false, bool findFirst=false)
Search for files from user/group/shipped directories.
Definition: POSIX.C:272
dictionary & debugSwitches()
The DebugSwitches sub-dictionary in the central controlDict.
Definition: debug.C:147
virtual const dictionary & dict() const =0
Return dictionary if this entry is a dictionary.
void deleteDemandDrivenData(DataPtr &dataPtr)
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
dictionary & controlDict()
The central control dictionary.
Definition: debug.C:90
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
T lookupOrAddDefault(const word &, const T &, bool recursive=false, bool patternMatch=true)
Find and return a T, if not found return the given.
simpleObjectRegistry & infoObjects()
Get access to registered info switch objects.
Definition: debug.C:323
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:103
A list of keyword definitions, which are a keyword followed by any number of values (e...
Definition: dictionary.H:137
Input from file stream.
Definition: IFstream.H:81
Namespace for OpenFOAM.
const entry * lookupEntryPtr(const word &, bool recursive, bool patternMatch) const
Find and return an entry data stream pointer if present.
Definition: dictionary.C:345
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
dictionary & switchSet(const char *subDictName, dictionary *&subDictPtr)
Internal function to lookup a sub-dictionary from controlDict.
Definition: debug.C:118
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
#define SafeFatalIOErrorIn(functionName, ios, msg)
Report an error message using Foam::FatalIOError.
Definition: error.H:338
dictionary & optimisationSwitches()
The OptimisationSwitches sub-dictionary in the central controlDict.
Definition: debug.C:159
Template functions to aid in the implementation of demand driven data.
void addOptimisationObject(const char *name, simpleRegIOobject *obj)
Register optimisation switch read/write object.
Definition: debug.C:235
void addDimensionSetObject(const char *name, simpleRegIOobject *obj)
Register DimensionSets read/write object.
Definition: debug.C:260
const T * lookupPtr(const word &) const
Find and return an entry if present, otherwise return NULL.
dictionary & infoSwitches()
The InfoSwitches sub-dictionary in the central controlDict.
Definition: debug.C:153
Abstract base class for registered object with I/O. Used in debug symbol registration.
simpleObjectRegistry & debugObjects()
Get access to registered debug switch objects.
Definition: debug.C:312
A keyword and a list of tokens is an &#39;entry&#39;.
Definition: entry.H:65
#define forAllReverse(list, i)
Definition: UList.H:424
void addInfoObject(const char *name, simpleRegIOobject *obj)
Register info switch read/write object.
Definition: debug.C:213
simpleObjectRegistry & optimisationObjects()
Get access to registered optimisation switch objects.
Definition: debug.C:334
simpleObjectRegistry & dimensionSetObjects()
Get access to registered dimensionSets switch objects.
Definition: debug.C:345
void append(const T &)
Append an element at the end of the list.
Definition: ListI.H:97
Object registry for simpleRegIOobject. Maintains ordering.
int optimisationSwitch(const char *name, const int defaultValue=0)
Lookup optimisation switch or add default value.
Definition: debug.C:183
void addDebugObject(const char *name, simpleRegIOobject *obj)
Register debug switch read/write object.
Definition: debug.C:192
void addDimensionedConstantObject(const char *name, simpleRegIOobject *)
Register DimensionedConstant read/write object.
Definition: debug.C:285
simpleObjectRegistry & dimensionedConstantObjects()
Get access to registered dimensionedConstant switch objects.
Definition: debug.C:356