bes  Updated for version 3.17.4
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 using std::cerr ;
44 using std::endl ;
45 using std::ostream ;
46 using std::map ;
47 using std::string ;
48 
49 #include "BESUtil.h"
50 
51 #ifdef NDEBUG
52 #define BESDEBUG( x, y )
53 #else
54 
67 #define BESDEBUG( x, y ) do { if( BESDebug::IsSet( x ) ) *(BESDebug::GetStrm()) << "[" << BESDebug::GetPidStr() << "]["<< x << "] " << y ; } while( 0 )
68 #endif
69 
90 #define BESISDEBUG( x ) BESDebug::IsSet( x )
91 
92 class BESDebug
93 {
94 private:
95  typedef map<string,bool> DebugMap;
96 
97  static DebugMap _debug_map ;
98  static ostream *_debug_strm ;
99  static bool _debug_strm_created ;
100 
101  typedef DebugMap::iterator _debug_iter ;
102 
103 public:
104  typedef DebugMap::const_iterator debug_citer ;
105 
106  static const DebugMap &debug_map()
107  {
108  return _debug_map;
109  }
110 
121  static void Set(const string &flagName, bool value)
122  {
123  if (flagName == "all" && value)
124  {
125  _debug_iter i = _debug_map.begin();
126  _debug_iter e = _debug_map.end();
127  for (; i != e; i++)
128  {
129  (*i).second = true;
130  }
131  }
132  _debug_map[flagName] = value;
133  }
134 
145  static void Register(const string &flagName)
146  {
147  debug_citer a = _debug_map.find("all");
148  debug_citer i = _debug_map.find(flagName);
149  if (i == _debug_map.end())
150  {
151  if (a == _debug_map.end())
152  {
153  _debug_map[flagName] = false;
154  }
155  else
156  {
157  _debug_map[flagName] = true;
158  }
159  }
160  }
161 
167  static bool IsSet(const string &flagName)
168  {
169  debug_citer i = _debug_map.find(flagName);
170  if (i != _debug_map.end())
171  return (*i).second;
172  else
173  i = _debug_map.find("all");
174  if (i != _debug_map.end())
175  return (*i).second;
176  else
177  return false;
178  }
179 
186  static ostream * GetStrm()
187  {
188  return _debug_strm;
189  }
190 
191  static string GetPidStr();
192 
208  static void SetStrm(ostream *strm, bool created)
209  {
210  if (_debug_strm_created && _debug_strm)
211  {
212  _debug_strm->flush();
213  delete _debug_strm;
214  _debug_strm = NULL;
215  }
216  else if (_debug_strm)
217  {
218  _debug_strm->flush();
219  }
220  if (!strm)
221  {
222  _debug_strm = &cerr;
223  _debug_strm_created = false;
224  }
225  else
226  {
227  _debug_strm = strm;
228  _debug_strm_created = created;
229  }
230  }
231 
232  static void SetUp( const string &values ) ;
233  static void Help( ostream &strm ) ;
234  static bool IsContextName( const string &name ) ;
235  static string GetOptionsString() ;
236 } ;
237 
238 #endif // I_BESDebug_h
239 
240 /*
241 int
242 main( int argc, char **argv )
243 {
244  int some_number = 1 ;
245  BESDEBUG( "something", "Shouldn't be seeing this part 1: "
246  << some_number++ << endl ) ;
247  BESDebug::Set( "something", false ) ;
248  BESDEBUG( "something", "Shouldn't be seeing this part 2: "
249  << some_number++ << endl ) ;
250  BESDebug::Set( "something", true ) ;
251  BESDEBUG( "something", "Should be seeing this: "
252  << some_number++ << endl ) ;
253 
254  return 0 ;
255 }
256 */
257 
static void SetUp(const string &values)
Sets up debugging for the bes.
Definition: BESDebug.cc:68
static void Set(const string &flagName, bool value)
set the debug context to the specified value
Definition: BESDebug.h:121
static string GetPidStr()
return the pid as a string
Definition: BESDebug.cc:139
static void Help(ostream &strm)
Writes help information for so that developers know what can be set for debugging.
Definition: BESDebug.cc:163
static ostream * GetStrm()
return the debug stream
Definition: BESDebug.h:186
static string GetOptionsString()
Definition: BESDebug.cc:200
static bool IsSet(const string &flagName)
see if the debug context flagName is set to true
Definition: BESDebug.h:167
static void SetStrm(ostream *strm, bool created)
set the debug output stream to the specified stream
Definition: BESDebug.h:208
static void Register(const string &flagName)
register the specified debug flag
Definition: BESDebug.h:145