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