bes Updated for version 3.21.1
The Backend Server (BES) is the lower two tiers of the Hyrax data server
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
35
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
45std::string get_debug_log_line_prefix();
46
47static std::mutex bes_debug_log_mutex;
48
50#ifdef NDEBUG
51#define BESDEBUG( x, y )
52#else
66#define BESDEBUG( x, y ) do { if( BESDebug::IsSet( x ) ) *(BESDebug::GetStrm()) << get_debug_log_line_prefix() << "["<< x << "] " << y ; } while( 0 )
67#endif // NDEBUG
68
69#ifdef NDEBUG
70#define BESISDEBUG( x ) (false)
71#else
92#define BESISDEBUG( x ) BESDebug::IsSet( x )
93#endif
94
95class BESDebug {
96private:
97 // The time to make 10000000 calls to IsSet with a std::map was 3 763 862 us and
98 // to make the same calls to IsSet with a std::unordered_map was 2 675 492 us
99 // jhrg 4/12/23
100 // typedef std::unordered_map<std::string, bool> DebugMap;
101 using DebugMap = std::map<std::string, bool>;
102
103 static DebugMap _debug_map;
104 static std::ostream *_debug_strm;
105 static bool _debug_strm_created;
106
107public:
108 static const DebugMap &debug_map()
109 {
110 return _debug_map;
111 }
112
113 // Moved to the .cc file to avoid <algorithm> in a header file. jhrg 4/14/23
114 static void Set(const std::string &flagName, bool value);
115
126 static void Register(const std::string &flagName)
127 {
128 auto i = _debug_map.find(flagName);
129 if (i == _debug_map.end()) {
130 auto a = _debug_map.find("all");
131 if (a == _debug_map.end()) {
132 _debug_map[flagName] = false;
133 }
134 else {
135 _debug_map[flagName] = true;
136 }
137 }
138 }
139
145 static bool IsSet(const std::string &flagName)
146 {
147 auto i = _debug_map.find(flagName);
148 if (i != _debug_map.end())
149 return (*i).second;
150 else
151 i = _debug_map.find("all");
152
153 if (i != _debug_map.end())
154 return (*i).second;
155 else
156 return false;
157 }
158
165 static std::ostream * GetStrm()
166 {
167 return _debug_strm;
168 }
169
185 static void SetStrm(std::ostream *strm, bool created)
186 {
187 if (_debug_strm_created && _debug_strm) {
188 _debug_strm->flush();
189 delete _debug_strm;
190 _debug_strm = nullptr;
191 }
192 else if (_debug_strm) {
193 _debug_strm->flush();
194 }
195 if (!strm) {
196 _debug_strm = &std::cerr;
197 _debug_strm_created = false;
198 }
199 else {
200 _debug_strm = strm;
201 _debug_strm_created = created;
202 }
203 }
204
205 static void SetUp(const std::string &values);
206 static void Help(std::ostream &strm);
207 static bool IsContextName(const std::string &name);
208 static std::string GetOptionsString();
209};
210
211#endif // I_BESDebug_h
static void SetStrm(std::ostream *strm, bool created)
set the debug output stream to the specified stream
Definition BESDebug.h:185
static void SetUp(const std::string &values)
Sets up debugging for the bes.
Definition BESDebug.cc:91
static void Register(const std::string &flagName)
register the specified debug flag
Definition BESDebug.h:126
static bool IsSet(const std::string &flagName)
see if the debug context flagName is set to true
Definition BESDebug.h:145
static void Help(std::ostream &strm)
Writes help information for so that developers know what can be set for debugging.
Definition BESDebug.cc:173
static std::ostream * GetStrm()
return the debug stream
Definition BESDebug.h:165
static std::string GetOptionsString()
Definition BESDebug.cc:205
static void Set(const std::string &flagName, bool value)
set the debug context to the specified value
Definition BESDebug.cc:157