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-2017 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 #include "IOobject.H"
37 #include "HashSet.H"
38 
39 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
40 
41 namespace Foam
42 {
43 namespace debug
44 {
45 
47 //- Skip documentation : local scope only
48 
49 dictionary* controlDictPtr_(nullptr);
50 dictionary* debugSwitchesPtr_(nullptr);
51 dictionary* infoSwitchesPtr_(nullptr);
52 dictionary* optimisationSwitchesPtr_(nullptr);
53 
54 // Debug switch read and write callback tables.
55 simpleObjectRegistry* debugObjectsPtr_(nullptr);
56 simpleObjectRegistry* infoObjectsPtr_(nullptr);
57 simpleObjectRegistry* optimisationObjectsPtr_(nullptr);
58 simpleObjectRegistry* dimensionSetObjectsPtr_(nullptr);
59 simpleObjectRegistry* dimensionedConstantObjectsPtr_(nullptr);
60 
61 
62 // To ensure controlDictPtr_ is deleted at the end of the run
63 class deleteControlDictPtr
64 {
65 public:
66 
67  deleteControlDictPtr()
68  {}
69 
70  ~deleteControlDictPtr()
71  {
72  deleteDemandDrivenData(debugObjectsPtr_);
73  deleteDemandDrivenData(infoObjectsPtr_);
74  deleteDemandDrivenData(optimisationObjectsPtr_);
75  deleteDemandDrivenData(dimensionSetObjectsPtr_);
76  deleteDemandDrivenData(dimensionedConstantObjectsPtr_);
77 
78  debugSwitchesPtr_ = nullptr;
79  infoSwitchesPtr_ = nullptr;
80  optimisationSwitchesPtr_ = nullptr;
81  deleteDemandDrivenData(controlDictPtr_);
82  }
83 };
84 
85 deleteControlDictPtr deleteControlDictPtr_;
87 
88 
89 } // End namespace debug
90 } // End namespace Foam
91 
92 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
93 
95 {
96  if (!controlDictPtr_)
97  {
98  fileNameList controlDictFiles = findEtcFiles("controlDict", true);
99  controlDictPtr_ = new dictionary();
100  forAllReverse(controlDictFiles, cdfi)
101  {
102  IFstream ifs(controlDictFiles[cdfi]);
103 
104  if (!ifs.good())
105  {
107  (
108  ifs,
109  "Cannot open controlDict"
110  );
111  }
112  controlDictPtr_->merge(dictionary(ifs));
113  }
114  }
115 
116  return *controlDictPtr_;
117 }
118 
119 
121 (
122  const char* subDictName,
123  dictionary*& subDictPtr
124 )
125 {
126  if (!subDictPtr)
127  {
129  (
130  subDictName, false, false
131  );
132 
133  if (!ePtr || !ePtr->isDict())
134  {
135  cerr<< "debug::switchSet(const char*, dictionary*&):\n"
136  << " Cannot find " << subDictName << " in dictionary "
137  << controlDict().name().c_str()
138  << std::endl << std::endl;
139 
140  ::exit(1);
141  }
142 
143  subDictPtr = &ePtr->dict();
144  }
145 
146  return *subDictPtr;
147 }
148 
149 
151 {
152  return switchSet("DebugSwitches", debugSwitchesPtr_);
153 }
154 
155 
157 {
158  return switchSet("InfoSwitches", infoSwitchesPtr_);
159 }
160 
161 
163 {
164  return switchSet("OptimisationSwitches", optimisationSwitchesPtr_);
165 }
166 
167 
168 int Foam::debug::debugSwitch(const char* name, const int defaultValue)
169 {
171  (
172  name, defaultValue, false, false
173  );
174 }
175 
176 
177 int Foam::debug::infoSwitch(const char* name, const int defaultValue)
178 {
180  (
181  name, defaultValue, false, false
182  );
183 }
184 
185 
186 int Foam::debug::optimisationSwitch(const char* name, const int defaultValue)
187 {
189  (
190  name, defaultValue, false, false
191  );
192 }
193 
194 
196 (
197  const char* name,
198  const float defaultValue
199 )
200 {
202  (
203  name, defaultValue, false, false
204  );
205 }
206 
207 
208 void Foam::debug::addDebugObject(const char* name, simpleRegIOobject* obj)
209 {
211  if (ptr)
212  {
213  ptr->append(obj);
214  }
215  else
216  {
218  (
219  name,
221  (
223  )
224  );
225  }
226 }
227 
228 
229 void Foam::debug::addInfoObject(const char* name, simpleRegIOobject* obj)
230 {
232  if (ptr)
233  {
234  ptr->append(obj);
235  }
236  else
237  {
239  (
240  name,
242  (
244  )
245  );
246  }
247 }
248 
249 
251 (
252  const char* name,
253  simpleRegIOobject* obj
254 )
255 {
257  if (ptr)
258  {
259  ptr->append(obj);
260  }
261  else
262  {
264  (
265  name,
267  (
269  )
270  );
271  }
272 }
273 
274 
276 (
277  const char* name,
278  simpleRegIOobject* obj
279 )
280 {
282  if (ptr)
283  {
284  ptr->append(obj);
285  }
286  else
287  {
289  (
290  name,
292  (
294  )
295  );
296  }
297 }
298 
299 
301 (
302  const char* name,
303  simpleRegIOobject* obj
304 )
305 {
307  (
308  name
309  );
310  if (ptr)
311  {
312  ptr->append(obj);
313  }
314  else
315  {
317  (
318  name,
320  (
322  )
323  );
324  }
325 }
326 
327 
329 {
330  if (!debugObjectsPtr_)
331  {
332  debugObjectsPtr_ = new simpleObjectRegistry(1000);
333  }
334 
335  return *debugObjectsPtr_;
336 }
337 
338 
340 {
341  if (!infoObjectsPtr_)
342  {
343  infoObjectsPtr_ = new simpleObjectRegistry(100);
344  }
345 
346  return *infoObjectsPtr_;
347 }
348 
349 
351 {
352  if (!optimisationObjectsPtr_)
353  {
354  optimisationObjectsPtr_ = new simpleObjectRegistry(100);
355  }
356 
357  return *optimisationObjectsPtr_;
358 }
359 
360 
362 {
363  if (!dimensionSetObjectsPtr_)
364  {
365  dimensionSetObjectsPtr_ = new simpleObjectRegistry(100);
366  }
367 
368  return *dimensionSetObjectsPtr_;
369 }
370 
371 
373 {
374  if (!dimensionedConstantObjectsPtr_)
375  {
376  dimensionedConstantObjectsPtr_ = new simpleObjectRegistry(100);
377  }
378 
379  return *dimensionedConstantObjectsPtr_;
380 }
381 
382 
383 namespace Foam
384 {
385 
386 void listSwitches
387 (
388  const wordList& debugSwitches,
389  const wordList& infoSwitches,
390  const wordList& optSwitches,
391  const bool unset
392 )
393 {
394  if (unset)
395  {
396  fileNameList controlDictFiles = findEtcFiles("controlDict", true);
398  forAllReverse(controlDictFiles, cdfi)
399  {
400  controlDict.merge(dictionary(IFstream(controlDictFiles[cdfi])()));
401  }
402 
403  wordHashSet controlDictDebug
404  (
405  controlDict.subDict("DebugSwitches").sortedToc()
406  );
407 
408  wordHashSet controlDictInfo
409  (
410  controlDict.subDict("InfoSwitches").sortedToc()
411  );
412 
413  wordHashSet controlDictOpt
414  (
415  controlDict.subDict("OptimisationSwitches").sortedToc()
416  );
417 
418 
420 
421  wordHashSet hashset;
422  hashset = debugSwitches;
423  hashset -= controlDictDebug;
424  Info<< "Unset DebugSwitches" << hashset.sortedToc() << endl;
425 
426  hashset = infoSwitches;
427  hashset -= controlDictInfo;
428  Info<< "Unset InfoSwitches" << hashset.sortedToc() << endl;
429 
430  hashset = optSwitches;
431  hashset -= controlDictOpt;
432  Info<< "Unset OptimisationSwitches" << hashset.sortedToc() << endl;
433  }
434  else
435  {
437  Info<< "DebugSwitches" << debugSwitches << endl;
438  Info<< "InfoSwitches" << infoSwitches << endl;
439  Info<< "OptimisationSwitches" << optSwitches << endl;
440  }
441 }
442 
443 }
444 
445 
446 void Foam::debug::listSwitches(const bool unset)
447 {
449  (
450  debug::debugSwitches().sortedToc(),
451  debug::infoSwitches().sortedToc(),
452  debug::optimisationSwitches().sortedToc(),
453  unset
454  );
455 }
456 
457 
459 {
461  (
462  debug::debugObjects().sortedToc(),
463  debug::infoObjects().sortedToc(),
464  debug::optimisationObjects().sortedToc(),
465  unset
466  );
467 }
468 
469 
470 // ************************************************************************* //
Abstract base class for registered object with I/O. Used in debug symbol registration.
A HashTable with keys but without contents.
Definition: HashSet.H:59
void addInfoObject(const char *name, simpleRegIOobject *obj)
Register info switch read/write object.
Definition: debug.C:229
const entry * lookupEntryPtr(const word &, bool recursive, bool patternMatch) const
Find and return an entry data stream pointer if present.
Definition: dictionary.C:470
float floatOptimisationSwitch(const char *name, const float defaultValue=0)
Lookup optimisation switch or add default value.
Definition: debug.C:196
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:121
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.
static Stream & writeDivider(Stream &os)
Write the standard file section divider.
Definition: IOobjectI.H:98
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:253
#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:186
virtual const dictionary & dict() const =0
Return dictionary if this entry is a dictionary.
const T * lookupPtr(const word &) const
Find and return an entry if present, otherwise return nullptr.
void append(const word &, T *)
Add at tail of dictionary.
simpleObjectRegistry & debugObjects()
Get access to registered debug switch objects.
Definition: debug.C:328
bool good() const
Return true if next operation might succeed.
Definition: IOstream.H:333
int infoSwitch(const char *name, const int defaultValue=0)
Lookup info switch or add default value.
Definition: debug.C:177
simpleObjectRegistry & optimisationObjects()
Get access to registered optimisation switch objects.
Definition: debug.C:350
fileNameList findEtcFiles(const fileName &, bool mandatory=false, bool findFirst=false)
Search for files from user/group/shipped directories.
Definition: etcFiles.C:119
const dictionary & subDict(const word &) const
Find and return a sub-dictionary.
Definition: dictionary.C:692
void addDimensionedConstantObject(const char *name, simpleRegIOobject *)
Register DimensionedConstant read/write object.
Definition: debug.C:301
const fileName & name() const
Return the dictionary name.
Definition: dictionary.H:103
Functions to search &#39;etc&#39; directories for configuration files etc.
dictionary & infoSwitches()
The InfoSwitches sub-dictionary in the central controlDict.
Definition: debug.C:156
void append(const T &)
Append an element at the end of the list.
Definition: ListI.H:184
int debugSwitch(const char *name, const int defaultValue=0)
Lookup debug switch or add default value.
Definition: debug.C:168
void listSwitches(const bool unset)
List debug switches.
Definition: debug.C:446
virtual bool isDict() const
Return true if this entry is a dictionary.
Definition: entry.H:156
void listRegisteredSwitches(const bool unset)
List registered debug switches.
Definition: debug.C:458
simpleObjectRegistry & infoObjects()
Get access to registered info switch objects.
Definition: debug.C:339
Input from file stream.
Definition: IFstream.H:81
simpleObjectRegistry & dimensionSetObjects()
Get access to registered dimensionSets switch objects.
Definition: debug.C:361
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:372
List< Key > sortedToc() const
Return the table of contents as a sorted list.
Definition: HashTable.C:217
messageStream Info
bool merge(const dictionary &)
Merge entries from the given dictionary.
Definition: dictionary.C:1090
dictionary & optimisationSwitches()
The OptimisationSwitches sub-dictionary in the central controlDict.
Definition: debug.C:162
void addDimensionSetObject(const char *name, simpleRegIOobject *obj)
Register DimensionSets read/write object.
Definition: debug.C:276
void addOptimisationObject(const char *name, simpleRegIOobject *obj)
Register optimisation switch read/write object.
Definition: debug.C:251
dictionary & debugSwitches()
The DebugSwitches sub-dictionary in the central controlDict.
Definition: debug.C:150
void addDebugObject(const char *name, simpleRegIOobject *obj)
Register debug switch read/write object.
Definition: debug.C:208
dictionary & controlDict()
The central control dictionary.
Definition: debug.C:94
void deleteDemandDrivenData(DataPtr &dataPtr)
wordList sortedToc() const
Return the sorted table of contents.
Definition: dictionary.C:790
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