bes  Updated for version 3.20.8
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 // Helper function for writing debug log lines
44 std::string get_debug_log_line_prefix();
45 
46 #ifdef NDEBUG
47 #define BESDEBUG( x, y )
48 #else
62 #define BESDEBUG( x, y ) do { if( BESDebug::IsSet( x ) ) *(BESDebug::GetStrm()) << get_debug_log_line_prefix() << "["<< x << "] " << y ; } while( 0 )
63 #endif
64 
65 #ifdef NDEBUG
66 #define BESISDEBUG( x ) (false)
67 #else
88 #define BESISDEBUG( x ) BESDebug::IsSet( x )
89 #endif
90 
91 class BESDebug {
92 private:
93  typedef std::map<std::string, bool> DebugMap;
94 
95  static DebugMap _debug_map;
96  static std::ostream *_debug_strm;
97  static bool _debug_strm_created;
98 
99  typedef DebugMap::iterator _debug_iter;
100 
101 public:
102  typedef DebugMap::const_iterator debug_citer;
103 
104  static const DebugMap &debug_map()
105  {
106  return _debug_map;
107  }
108 
119  static void Set(const std::string &flagName, bool value)
120  {
121  if (flagName == "all" && value) {
122  _debug_iter i = _debug_map.begin();
123  _debug_iter e = _debug_map.end();
124  for (; i != e; i++) {
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  if (a == _debug_map.end()) {
147  _debug_map[flagName] = false;
148  }
149  else {
150  _debug_map[flagName] = true;
151  }
152  }
153  }
154 
160  static bool IsSet(const std::string &flagName)
161  {
162  debug_citer i = _debug_map.find(flagName);
163  if (i != _debug_map.end())
164  return (*i).second;
165  else
166  i = _debug_map.find("all");
167  if (i != _debug_map.end())
168  return (*i).second;
169  else
170  return false;
171  }
172 
179  static std::ostream * GetStrm()
180  {
181  return _debug_strm;
182  }
183 
184  static std::string GetPidStr();
185 
201  static void SetStrm(std::ostream *strm, bool created)
202  {
203  if (_debug_strm_created && _debug_strm) {
204  _debug_strm->flush();
205  delete _debug_strm;
206  _debug_strm = NULL;
207  }
208  else if (_debug_strm) {
209  _debug_strm->flush();
210  }
211  if (!strm) {
212  _debug_strm = &std::cerr;
213  _debug_strm_created = false;
214  }
215  else {
216  _debug_strm = strm;
217  _debug_strm_created = created;
218  }
219  }
220 
221  static void SetUp(const std::string &values);
222  static void Help(std::ostream &strm);
223  static bool IsContextName(const std::string &name);
224  static std::string GetOptionsString();
225 };
226 
227 #endif // I_BESDebug_h
static void SetStrm(std::ostream *strm, bool created)
set the debug output stream to the specified stream
Definition: BESDebug.h:201
static void SetUp(const std::string &values)
Sets up debugging for the bes.
Definition: BESDebug.cc:97
static std::ostream * GetStrm()
return the debug stream
Definition: BESDebug.h:179
static void Register(const std::string &flagName)
register the specified debug flag
Definition: BESDebug.h:141
static bool IsSet(const std::string &flagName)
see if the debug context flagName is set to true
Definition: BESDebug.h:160
static void Help(std::ostream &strm)
Writes help information for so that developers know what can be set for debugging.
Definition: BESDebug.cc:161
static std::string GetOptionsString()
Definition: BESDebug.cc:195
static void Set(const std::string &flagName, bool value)
set the debug context to the specified value
Definition: BESDebug.h:119