bes  Updated for version 3.19.1
BESDebug.cc
1 // BESDebug.cc
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 
33 #include "config.h"
34 
35 #include <time.h>
36 #include <unistd.h>
37 
38 #include <fstream>
39 #include <iostream>
40 #include <sstream>
41 
42 #include "BESDebug.h"
43 #include "BESInternalError.h"
44 #include "BESLog.h"
45 
46 using namespace std;
47 
48 ostream *BESDebug::_debug_strm = NULL;
49 bool BESDebug::_debug_strm_created = false;
50 map<string, bool> BESDebug::_debug_map;
51 
64 void BESDebug::SetUp(const string &values)
65 {
66  if (values.empty())
67  {
68  string err = "Empty debug options";
69  throw BESInternalError(err, __FILE__, __LINE__);
70  }
71  string::size_type comma = 0;
72  comma = values.find(',');
73  if (comma == string::npos)
74  {
75  string err = "Missing comma in debug options: " + values;
76  throw BESInternalError(err, __FILE__, __LINE__);
77  }
78  ostream *strm = 0;
79  bool created = false;
80  string s_strm = values.substr(0, comma);
81  if (s_strm == "cerr")
82  {
83  strm = &cerr;
84  }
85  else if (s_strm == "LOG")
86  {
87  strm = BESLog::TheLog()->get_log_ostream();
88  }
89  else
90  {
91  strm = new ofstream(s_strm.c_str(), ios::out);
92  if (strm && !(*strm))
93  {
94  delete strm;
95  strm = 0;
96  string err = "Unable to open the debug file: " + s_strm;
97  throw BESInternalError(err, __FILE__, __LINE__);
98  }
99  created = true;
100  }
101 
102  BESDebug::SetStrm(strm, created);
103 
104  string::size_type new_comma = 0;
105  while ((new_comma = values.find(',', comma + 1)) != string::npos)
106  {
107  string flagName = values.substr(comma + 1, new_comma - comma - 1);
108  if (flagName[0] == '-')
109  {
110  string newflag = flagName.substr(1, flagName.length() - 1);
111  BESDebug::Set(newflag, false);
112  }
113  else
114  {
115  BESDebug::Set(flagName, true);
116  }
117  comma = new_comma;
118  }
119  string flagName = values.substr(comma + 1, values.length() - comma - 1);
120  if (flagName[0] == '-')
121  {
122  string newflag = flagName.substr(1, flagName.length() - 1);
123  BESDebug::Set(newflag, false);
124  }
125  else
126  {
127  BESDebug::Set(flagName, true);
128  }
129 }
130 
136 {
137  ostringstream strm;
138  const time_t sctime = time(NULL);
139  const struct tm *sttime = localtime(&sctime);
140  char zone_name[10];
141  strftime(zone_name, sizeof(zone_name), "%Z", sttime);
142  char *b = asctime(sttime);
143  strm << zone_name << " ";
144  for (register int j = 0; b[j] != '\n'; j++)
145  strm << b[j];
146  pid_t thepid = getpid();
147  strm << " id: " << thepid;
148  return strm.str();
149 }
150 
159 void BESDebug::Help(ostream &strm)
160 {
161  strm << "Debug help:" << endl << " Set on the command line with " << "-d \"file_name|cerr,[-]context1,...,[-]context\"" << endl
162  << " context with dash (-) in front will be turned off" << endl << " context of all will turn on debugging for all contexts" << endl << endl
163  << "Possible context(s):" << endl;
164 
165  if (_debug_map.size())
166  {
167  BESDebug::debug_citer i = _debug_map.begin();
168  BESDebug::debug_citer e = _debug_map.end();
169  for (; i != e; i++)
170  {
171  strm << " " << (*i).first << ": ";
172  if ((*i).second)
173  strm << "on" << endl;
174  else
175  strm << "off" << endl;
176  }
177  }
178  else
179  {
180  strm << " none specified" << endl;
181  }
182 }
183 
184 bool BESDebug::IsContextName(const string &name)
185 {
186  return _debug_map.count(name) > 0;
187 }
188 
197 {
198  ostringstream oss("");
199 
200  if (_debug_map.size())
201  {
202  BESDebug::debug_citer i = _debug_map.begin();
203  BESDebug::debug_citer e = _debug_map.end();
204  for (; i != e; i++)
205  {
206  if (!(*i).second)
207  oss << "-";
208  oss << (*i).first << ",";
209  }
210  string retval = oss.str();
211  return retval.erase(retval.length()-1);
212  }
213  else
214  {
215  return "";
216  }
217 }
218 
exception thrown if inernal error encountered
static std::string GetPidStr()
return the pid as a string
Definition: BESDebug.cc:135
STL namespace.
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