bes  Updated for version 3.19.1
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 {
90 private:
91  typedef std::map<std::string,bool> DebugMap;
92 
93  static DebugMap _debug_map ;
94  static std::ostream *_debug_strm ;
95  static bool _debug_strm_created ;
96 
97  typedef DebugMap::iterator _debug_iter ;
98 
99 public:
100  typedef DebugMap::const_iterator debug_citer ;
101 
102  static const DebugMap &debug_map()
103  {
104  return _debug_map;
105  }
106 
117  static void Set(const std::string &flagName, bool value)
118  {
119  if (flagName == "all" && value)
120  {
121  _debug_iter i = _debug_map.begin();
122  _debug_iter e = _debug_map.end();
123  for (; i != e; i++)
124  {
125  (*i).second = true;
126  }
127  }
128  _debug_map[flagName] = value;
129  }
130 
141  static void Register(const std::string &flagName)
142  {
143  debug_citer a = _debug_map.find("all");
144  debug_citer i = _debug_map.find(flagName);
145  if (i == _debug_map.end())
146  {
147  if (a == _debug_map.end())
148  {
149  _debug_map[flagName] = false;
150  }
151  else
152  {
153  _debug_map[flagName] = true;
154  }
155  }
156  }
157 
163  static bool IsSet(const std::string &flagName)
164  {
165  debug_citer i = _debug_map.find(flagName);
166  if (i != _debug_map.end())
167  return (*i).second;
168  else
169  i = _debug_map.find("all");
170  if (i != _debug_map.end())
171  return (*i).second;
172  else
173  return false;
174  }
175 
182  static std::ostream * GetStrm()
183  {
184  return _debug_strm;
185  }
186 
187  static std::string GetPidStr();
188 
204  static void SetStrm(std::ostream *strm, bool created)
205  {
206  if (_debug_strm_created && _debug_strm)
207  {
208  _debug_strm->flush();
209  delete _debug_strm;
210  _debug_strm = NULL;
211  }
212  else if (_debug_strm)
213  {
214  _debug_strm->flush();
215  }
216  if (!strm)
217  {
218  _debug_strm = &std::cerr;
219  _debug_strm_created = false;
220  }
221  else
222  {
223  _debug_strm = strm;
224  _debug_strm_created = created;
225  }
226  }
227 
228  static void SetUp( const std::string &values ) ;
229  static void Help( std::ostream &strm ) ;
230  static bool IsContextName( const std::string &name ) ;
231  static std::string GetOptionsString() ;
232 } ;
233 
234 #endif // I_BESDebug_h
static std::ostream * GetStrm()
return the debug stream
Definition: BESDebug.h:182
static void Register(const std::string &flagName)
register the specified debug flag
Definition: BESDebug.h:141
static std::string GetPidStr()
return the pid as a string
Definition: BESDebug.cc:135
static void SetUp(const std::string &values)
Sets up debugging for the bes.
Definition: BESDebug.cc:64
static void Set(const std::string &flagName, bool value)
set the debug context to the specified value
Definition: BESDebug.h:117
static std::string GetOptionsString()
Definition: BESDebug.cc:196
static void Help(std::ostream &strm)
Writes help information for so that developers know what can be set for debugging.
Definition: BESDebug.cc:159
static void SetStrm(std::ostream *strm, bool created)
set the debug output stream to the specified stream
Definition: BESDebug.h:204
static bool IsSet(const std::string &flagName)
see if the debug context flagName is set to true
Definition: BESDebug.h:163