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