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  ifs,
105  "Cannot open controlDict"
106  );
107  }
108  controlDictPtr_->merge(dictionary(ifs));
109  }
110  }
111 
112  return *controlDictPtr_;
113 }
114 
115 
117 (
118  const char* subDictName,
119  dictionary*& subDictPtr
120 )
121 {
122  if (!subDictPtr)
123  {
124  entry* ePtr = controlDict().lookupEntryPtr
125  (
126  subDictName, false, false
127  );
128 
129  if (!ePtr || !ePtr->isDict())
130  {
131  cerr<< "debug::switchSet(const char*, dictionary*&):\n"
132  << " Cannot find " << subDictName << " in dictionary "
133  << controlDict().name().c_str()
134  << std::endl << std::endl;
135 
136  ::exit(1);
137  }
138 
139  subDictPtr = &ePtr->dict();
140  }
141 
142  return *subDictPtr;
143 }
144 
145 
147 {
148  return switchSet("DebugSwitches", debugSwitchesPtr_);
149 }
150 
151 
153 {
154  return switchSet("InfoSwitches", infoSwitchesPtr_);
155 }
156 
157 
159 {
160  return switchSet("OptimisationSwitches", optimisationSwitchesPtr_);
161 }
162 
163 
164 int Foam::debug::debugSwitch(const char* name, const int defaultValue)
165 {
167  (
168  name, defaultValue, false, false
169  );
170 }
171 
172 
173 int Foam::debug::infoSwitch(const char* name, const int defaultValue)
174 {
176  (
177  name, defaultValue, false, false
178  );
179 }
180 
181 
182 int Foam::debug::optimisationSwitch(const char* name, const int defaultValue)
183 {
185  (
186  name, defaultValue, false, false
187  );
188 }
189 
190 
192 {
194  if (ptr)
195  {
196  ptr->append(obj);
197  }
198  else
199  {
201  (
202  name,
204  (
206  )
207  );
208  }
209 }
210 
211 
213 {
215  if (ptr)
216  {
217  ptr->append(obj);
218  }
219  else
220  {
222  (
223  name,
225  (
227  )
228  );
229  }
230 }
231 
232 
234 (
235  const char* name,
236  simpleRegIOobject* obj
237 )
238 {
240  if (ptr)
241  {
242  ptr->append(obj);
243  }
244  else
245  {
247  (
248  name,
250  (
252  )
253  );
254  }
255 }
256 
257 
259 (
260  const char* name,
261  simpleRegIOobject* obj
262 )
263 {
265  if (ptr)
266  {
267  ptr->append(obj);
268  }
269  else
270  {
272  (
273  name,
275  (
277  )
278  );
279  }
280 }
281 
282 
284 (
285  const char* name,
286  simpleRegIOobject* obj
287 )
288 {
290  (
291  name
292  );
293  if (ptr)
294  {
295  ptr->append(obj);
296  }
297  else
298  {
300  (
301  name,
303  (
305  )
306  );
307  }
308 }
309 
310 
312 {
313  if (!debugObjectsPtr_)
314  {
315  debugObjectsPtr_ = new simpleObjectRegistry(1000);
316  }
317 
318  return *debugObjectsPtr_;
319 }
320 
321 
323 {
324  if (!infoObjectsPtr_)
325  {
326  infoObjectsPtr_ = new simpleObjectRegistry(100);
327  }
328 
329  return *infoObjectsPtr_;
330 }
331 
332 
334 {
335  if (!optimisationObjectsPtr_)
336  {
337  optimisationObjectsPtr_ = new simpleObjectRegistry(100);
338  }
339 
340  return *optimisationObjectsPtr_;
341 }
342 
343 
345 {
346  if (!dimensionSetObjectsPtr_)
347  {
348  dimensionSetObjectsPtr_ = new simpleObjectRegistry(100);
349  }
350 
351  return *dimensionSetObjectsPtr_;
352 }
353 
354 
356 {
357  if (!dimensionedConstantObjectsPtr_)
358  {
359  dimensionedConstantObjectsPtr_ = new simpleObjectRegistry(100);
360  }
361 
362  return *dimensionedConstantObjectsPtr_;
363 }
364 
365 
366 // ************************************************************************* //
Foam::entry
A keyword and a list of tokens is an 'entry'.
Definition: entry.H:65
OSspecific.H
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
Foam::debug::debugSwitches
dictionary & debugSwitches()
The DebugSwitches sub-dictionary in the central controlDict.
Definition: debug.C:146
debug.H
Foam::IFstream
Input from file stream.
Definition: IFstream.H:81
demandDrivenData.H
Template functions to aid in the implementation of demand driven data.
Foam::debug::infoSwitch
int infoSwitch(const char *name, const int defaultValue=0)
Lookup info switch or add default value.
Definition: debug.C:173
Foam::DictionaryBase::lookupPtr
const T * lookupPtr(const word &) const
Find and return an entry if present, otherwise return NULL.
simpleObjectRegistry.H
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
Foam::DictionaryBase::append
void append(const word &, T *)
Add at tail of dictionary.
Definition: DictionaryBase.C:203
Foam::deleteDemandDrivenData
void deleteDemandDrivenData(DataPtr &dataPtr)
Definition: demandDrivenData.H:40
Foam::debug::optimisationSwitch
int optimisationSwitch(const char *name, const int defaultValue=0)
Lookup optimisation switch or add default value.
Definition: debug.C:182
Foam::simpleObjectRegistryEntry
Definition: simpleObjectRegistry.H:48
Foam::debug::optimisationObjects
simpleObjectRegistry & optimisationObjects()
Get access to registered optimisation switch objects.
Definition: debug.C:333
Foam::entry::isDict
virtual bool isDict() const
Return true if this entry is a dictionary.
Definition: entry.H:153
controlDict
runTime controlDict().lookup("adjustTimeStep") >> adjustTimeStep
Foam::List::append
void append(const T &)
Append an element at the end of the list.
Foam::debug::debugObjects
simpleObjectRegistry & debugObjects()
Get access to registered debug switch objects.
Definition: debug.C:311
Foam::debug::dimensionSetObjects
simpleObjectRegistry & dimensionSetObjects()
Get access to registered dimensionSets switch objects.
Definition: debug.C:344
IFstream.H
SafeFatalIOErrorInFunction
#define SafeFatalIOErrorInFunction(ios, msg)
Report an error message using Foam::FatalIOError.
Definition: error.H:345
Foam::dictionary
A list of keyword definitions, which are a keyword followed by any number of values (e....
Definition: dictionary.H:137
Foam::debug::addDimensionedConstantObject
void addDimensionedConstantObject(const char *name, simpleRegIOobject *)
Register DimensionedConstant read/write object.
Definition: debug.C:284
Foam::debug::infoObjects
simpleObjectRegistry & infoObjects()
Get access to registered info switch objects.
Definition: debug.C:322
forAllReverse
#define forAllReverse(list, i)
Reverse loop across all elements in list.
Definition: UList.H:418
Foam::debug::dimensionedConstantObjects
simpleObjectRegistry & dimensionedConstantObjects()
Get access to registered dimensionedConstant switch objects.
Definition: debug.C:355
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::debug::infoSwitches
dictionary & infoSwitches()
The InfoSwitches sub-dictionary in the central controlDict.
Definition: debug.C:152
Foam::entry::dict
virtual const dictionary & dict() const =0
Return dictionary if this entry is a dictionary.
Foam::debug::debugSwitch
int debugSwitch(const char *name, const int defaultValue=0)
Lookup debug switch or add default value.
Definition: debug.C:164
Foam::exit
errorManipArg< error, int > exit(error &err, const int errNo=1)
Definition: errorManip.H:124
Ostream.H
Foam::findEtcFiles
fileNameList findEtcFiles(const fileName &, bool mandatory=false, bool findFirst=false)
Search for files from user/group/shipped directories.
Definition: POSIX.C:271
Foam::debug::addDebugObject
void addDebugObject(const char *name, simpleRegIOobject *obj)
Register debug switch read/write object.
Definition: debug.C:191
Foam::List
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
dictionary.H
Foam::debug::addDimensionSetObject
void addDimensionSetObject(const char *name, simpleRegIOobject *obj)
Register DimensionSets read/write object.
Definition: debug.C:259
Foam::debug::controlDict
dictionary & controlDict()
The central control dictionary.
Definition: debug.C:90
Foam::IOstream::good
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
Foam::debug::optimisationSwitches
dictionary & optimisationSwitches()
The OptimisationSwitches sub-dictionary in the central controlDict.
Definition: debug.C:158
Foam::debug::addInfoObject
void addInfoObject(const char *name, simpleRegIOobject *obj)
Register info switch read/write object.
Definition: debug.C:212
Foam::debug::addOptimisationObject
void addOptimisationObject(const char *name, simpleRegIOobject *obj)
Register optimisation switch read/write object.
Definition: debug.C:234
Foam::simpleRegIOobject
Abstract base class for registered object with I/O. Used in debug symbol registration.
Definition: simpleRegIOobject.H:50
Foam::debug::switchSet
dictionary & switchSet(const char *subDictName, dictionary *&subDictPtr)
Internal function to lookup a sub-dictionary from controlDict.
Definition: debug.C:117
Foam::name
word name(const complex &)
Return a string representation of a complex.
Definition: complex.C:47
Foam::dictionary::lookupOrAddDefault
T lookupOrAddDefault(const word &, const T &, bool recursive=false, bool patternMatch=true)
Find and return a T, if not found return the given.
Definition: dictionaryTemplates.C:63
Foam::simpleObjectRegistry
Object registry for simpleRegIOobject. Maintains ordering.
Definition: simpleObjectRegistry.H:66