initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / OpenFOAM / global / debug / debug.C
blob82ec7d7eaf13f226563e0170e44c8e45261b3980
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 Description
26     Class for handling debugging switches.
28 \*---------------------------------------------------------------------------*/
30 #include "debug.H"
31 #include "dictionary.H"
32 #include "IFstream.H"
33 #include "OSspecific.H"
35 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 namespace Foam
40 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
42 namespace debug
45 dictionary* controlDictPtr_(NULL);
46 dictionary* debugSwitchesPtr_(NULL);
47 dictionary* infoSwitchesPtr_(NULL);
48 dictionary* optimisationSwitchesPtr_(NULL);
50 //- Class to ensure controlDictPtr_ is deleted at the end of the run
51 //  @cond ignore documentation for this class
52 class deleteControlDictPtr
54 public:
56     deleteControlDictPtr()
57     {}
59     ~deleteControlDictPtr()
60     {
61         if (controlDictPtr_)
62         {
63             delete controlDictPtr_;
64         }
65     }
67 //! @endcond
69 deleteControlDictPtr deleteControlDictPtr_;
72 dictionary& switchSet(const char* switchSetName, dictionary* switchSetPtr)
74     if (!switchSetPtr)
75     {
76         if (!controlDict().found(switchSetName))
77         {
78             cerr<< "debug::switchSet(const char*, dictionary*): " << std::endl
79                 << "    Cannot find " <<  switchSetName
80             << " in dictionary " << controlDictPtr_->name().c_str()
81             << std::endl << std::endl;
83             ::exit(1);
84         }
86         switchSetPtr =
87             const_cast<dictionary*>(&(controlDict().subDict(switchSetName)));
88     }
90     return *switchSetPtr;
94 int debugSwitch
96     dictionary& switchSet,
97     const char* switchName,
98     const int defaultValue
101     if (switchSet.found(switchName))
102     {
103         return readInt(switchSet.lookup(switchName));
104     }
105     else
106     {
107         switchSet.add(switchName, defaultValue);
108         return defaultValue;
109     }
115 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
117 dictionary& debug::controlDict()
119     if (!controlDictPtr_)
120     {
121         fileName controlDictFileName(dotFoam("controlDict"));
123         IFstream dictFile(controlDictFileName);
125         if (!dictFile.good())
126         {
127             cerr<< "debug::controlDict(): "
128                 << "Cannot open essential file " << controlDictFileName.c_str()
129                 << std::endl << std::endl;
130             ::exit(1);
131         }
133         controlDictPtr_ = new dictionary(dictFile);
134     }
136     return *controlDictPtr_;
140 dictionary& debug::debugSwitches()
142     return switchSet("DebugSwitches", debugSwitchesPtr_);
146 int debug::debugSwitch(const char* switchName, const int defaultValue)
148     return debugSwitch
149     (
150         debugSwitches(),
151         switchName,
152         defaultValue
153     );
157 dictionary& debug::infoSwitches()
159     return switchSet("InfoSwitches", infoSwitchesPtr_);
163 int debug::infoSwitch(const char* switchName, const int defaultValue)
165     return debugSwitch
166     (
167         infoSwitches(),
168         switchName,
169         defaultValue
170     );
174 dictionary& debug::optimisationSwitches()
176     return switchSet("OptimisationSwitches", optimisationSwitchesPtr_);
180 int debug::optimisationSwitch(const char* switchName, const int defaultValue)
182     return debugSwitch
183     (
184         optimisationSwitches(),
185         switchName,
186         defaultValue
187     );
191 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
193 } // End namespace Foam
195 // ************************************************************************* //