bes  Updated for version 3.20.6
BESDebug.h
1 // BESDebug.h
2 
3 // This file is part of bes, A C++ back-end server implementation framework
4 // for the OPeNDAP Data Access Protocol.
5 
6 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
7 // Author: Patrick West <pwest@ucar.edu> and Jose Garcia <jgarcia@ucar.edu>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 //
23 // You can contact University Corporation for Atmospheric Research at
24 // 3080 Center Green Drive, Boulder, CO 80301
25 
26 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
27 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
28 //
29 // Authors:
30 // pwest Patrick West <pwest@ucar.edu>
31 // jgarcia Jose Garcia <jgarcia@ucar.edu>
32 
36 #ifndef I_BESDebug_h
37 #define I_BESDebug_h 1
38 
39 #include <iostream>
40 #include <map>
41 #include <string>
42 
43 #ifdef NDEBUG
44 #define BESDEBUG( x, y )
45 #else
46 
59 #define BESDEBUG( x, y ) do { if( BESDebug::IsSet( x ) ) *(BESDebug::GetStrm()) << "[" << BESDebug::GetPidStr() << "]["<< x << "] " << y ; } while( 0 )
60 #endif
61 
62 #ifdef NDEBUG
63 #define BESISDEBUG( x ) (false)
64 #else
65 
85 #define BESISDEBUG( x ) BESDebug::IsSet( x )
86 #endif
87 
88 class BESDebug {
89 private:
90  typedef std::map<std::string, bool> DebugMap;
91 
92  static DebugMap _debug_map;
93  static std::ostream *_debug_strm;
94  static bool _debug_strm_created;
95 
96  typedef DebugMap::iterator _debug_iter;
97 
98 public:
99  typedef DebugMap::const_iterator debug_citer;
100 
101  static const DebugMap &debug_map()
102  {
103  return _debug_map;
104  }
105 
116  static void Set(const std::string &flagName, bool value)
117  {
118  if (flagName == "all" && value) {
119  _debug_iter i = _debug_map.begin();
120  _debug_iter e = _debug_map.end();
121  for (; i != e; i++) {
122  (*i).second = true;
123  }
124  }
125  _debug_map[flagName] = value;
126  }
127 
138  static void Register(const std::string &flagName)
139  {
140  debug_citer a = _debug_map.find("all");
141  debug_citer i = _debug_map.find(flagName);
142  if (i == _debug_map.end()) {
143  if (a == _debug_map.end()) {
144  _debug_map[flagName] = false;
145  }
146  else {
147  _debug_map[flagName] = true;
148  }
149  }
150  }
151 
157  static bool IsSet(const std::string &flagName)
158  {
159  debug_citer i = _debug_map.find(flagName);
160  if (i != _debug_map.end())
161  return (*i).second;
162  else
163  i = _debug_map.find("all");
164  if (i != _debug_map.end())
165  return (*i).second;
166  else
167  return false;
168  }
169 
176  static std::ostream * GetStrm()
177  {
178  return _debug_strm;
179  }
180 
181  static std::string GetPidStr();
182 
198  static void SetStrm(std::ostream *strm, bool created)
199  {
200  if (_debug_strm_created && _debug_strm) {
201  _debug_strm->flush();
202  delete _debug_strm;
203  _debug_strm = NULL;
204  }
205  else if (_debug_strm) {
206  _debug_strm->flush();
207  }
208  if (!strm) {
209  _debug_strm = &std::cerr;
210  _debug_strm_created = false;
211  }
212  else {
213  _debug_strm = strm;
214  _debug_strm_created = created;
215  }
216  }
217 
218  static void SetUp(const std::string &values);
219  static void Help(std::ostream &strm);
220  static bool IsContextName(const std::string &name);
221  static std::string GetOptionsString();
222 };
223 
224 #endif // I_BESDebug_h
BESDebug::GetPidStr
static std::string GetPidStr()
return the pid as a string
Definition: BESDebug.cc:124
BESDebug
Definition: BESDebug.h:88
BESDebug::Set
static void Set(const std::string &flagName, bool value)
set the debug context to the specified value
Definition: BESDebug.h:116
BESDebug::SetUp
static void SetUp(const std::string &values)
Sets up debugging for the bes.
Definition: BESDebug.cc:64
BESDebug::GetOptionsString
static std::string GetOptionsString()
Definition: BESDebug.cc:182
BESDebug::IsSet
static bool IsSet(const std::string &flagName)
see if the debug context flagName is set to true
Definition: BESDebug.h:157
BESDebug::Register
static void Register(const std::string &flagName)
register the specified debug flag
Definition: BESDebug.h:138
BESDebug::Help
static void Help(std::ostream &strm)
Writes help information for so that developers know what can be set for debugging.
Definition: BESDebug.cc:148
BESDebug::SetStrm
static void SetStrm(std::ostream *strm, bool created)
set the debug output stream to the specified stream
Definition: BESDebug.h:198
BESDebug::GetStrm
static std::ostream * GetStrm()
return the debug stream
Definition: BESDebug.h:176