bes  Updated for version 3.20.10
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 #include <mutex>
43 
44 // Helper function for writing debug log lines
45 std::string get_debug_log_line_prefix();
46 
47 static std::mutex bes_debug_log_mutex;
48 
49 #ifdef NDEBUG
50 #define BESDEBUG( x, y )
51 #else
65 #if 0
66 #define BESDEBUG( x, y ) do { std::unique_lock<std::mutex> lck (bes_debug_log_mutex); if( BESDebug::IsSet( x ) ) { *(BESDebug::GetStrm()) << get_debug_log_line_prefix() << "["<< x << "] " << y; } } while( 0 )
67 #else
68 #define BESDEBUG( x, y ) do { if( BESDebug::IsSet( x ) ) *(BESDebug::GetStrm()) << get_debug_log_line_prefix() << "["<< x << "] " << y ; } while( 0 )
69 #endif
70 
71 #endif // NDEBUG
72 
73 #ifdef NDEBUG
74 #define BESISDEBUG( x ) (false)
75 #else
96 #define BESISDEBUG( x ) BESDebug::IsSet( x )
97 #endif
98 
99 class BESDebug {
100 private:
101  typedef std::map<std::string, bool> DebugMap;
102 
103  static DebugMap _debug_map;
104  static std::ostream *_debug_strm;
105  static bool _debug_strm_created;
106 
107  typedef DebugMap::iterator _debug_iter;
108 
109 public:
110  typedef DebugMap::const_iterator debug_citer;
111 
112  static const DebugMap &debug_map()
113  {
114  return _debug_map;
115  }
116 
127  static void Set(const std::string &flagName, bool value)
128  {
129  if (flagName == "all" && value) {
130  _debug_iter i = _debug_map.begin();
131  _debug_iter e = _debug_map.end();
132  for (; i != e; i++) {
133  (*i).second = true;
134  }
135  }
136  _debug_map[flagName] = value;
137  }
138 
149  static void Register(const std::string &flagName)
150  {
151  debug_citer a = _debug_map.find("all");
152  debug_citer i = _debug_map.find(flagName);
153  if (i == _debug_map.end()) {
154  if (a == _debug_map.end()) {
155  _debug_map[flagName] = false;
156  }
157  else {
158  _debug_map[flagName] = true;
159  }
160  }
161  }
162 
168  static bool IsSet(const std::string &flagName)
169  {
170  debug_citer i = _debug_map.find(flagName);
171  if (i != _debug_map.end())
172  return (*i).second;
173  else
174  i = _debug_map.find("all");
175  if (i != _debug_map.end())
176  return (*i).second;
177  else
178  return false;
179  }
180 
187  static std::ostream * GetStrm()
188  {
189  return _debug_strm;
190  }
191 
192  static std::string GetPidStr();
193 
209  static void SetStrm(std::ostream *strm, bool created)
210  {
211  if (_debug_strm_created && _debug_strm) {
212  _debug_strm->flush();
213  delete _debug_strm;
214  _debug_strm = NULL;
215  }
216  else if (_debug_strm) {
217  _debug_strm->flush();
218  }
219  if (!strm) {
220  _debug_strm = &std::cerr;
221  _debug_strm_created = false;
222  }
223  else {
224  _debug_strm = strm;
225  _debug_strm_created = created;
226  }
227  }
228 
229  static void SetUp(const std::string &values);
230  static void Help(std::ostream &strm);
231  static bool IsContextName(const std::string &name);
232  static std::string GetOptionsString();
233 };
234 
235 #endif // I_BESDebug_h
static void SetStrm(std::ostream *strm, bool created)
set the debug output stream to the specified stream
Definition: BESDebug.h:209
static void SetUp(const std::string &values)
Sets up debugging for the bes.
Definition: BESDebug.cc:98
static std::ostream * GetStrm()
return the debug stream
Definition: BESDebug.h:187
static void Register(const std::string &flagName)
register the specified debug flag
Definition: BESDebug.h:149
static bool IsSet(const std::string &flagName)
see if the debug context flagName is set to true
Definition: BESDebug.h:168
static void Help(std::ostream &strm)
Writes help information for so that developers know what can be set for debugging.
Definition: BESDebug.cc:162
static std::string GetOptionsString()
Definition: BESDebug.cc:196
static void Set(const std::string &flagName, bool value)
set the debug context to the specified value
Definition: BESDebug.h:127