bes  Updated for version 3.20.6
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  string err = "Empty debug options";
68  throw BESInternalError(err, __FILE__, __LINE__);
69  }
70  string::size_type comma = 0;
71  comma = values.find(',');
72  if (comma == string::npos) {
73  string err = "Missing comma in debug options: " + values;
74  throw BESInternalError(err, __FILE__, __LINE__);
75  }
76  ostream *strm = 0;
77  bool created = false;
78  string s_strm = values.substr(0, comma);
79  if (s_strm == "cerr") {
80  strm = &cerr;
81  }
82  else if (s_strm == "LOG") {
83  strm = BESLog::TheLog()->get_log_ostream();
84  }
85  else {
86  strm = new ofstream(s_strm.c_str(), ios::out);
87  if (strm && !(*strm)) {
88  delete strm;
89  strm = 0;
90  string err = "Unable to open the debug file: " + s_strm;
91  throw BESInternalError(err, __FILE__, __LINE__);
92  }
93  created = true;
94  }
95 
96  BESDebug::SetStrm(strm, created);
97 
98  string::size_type new_comma = 0;
99  while ((new_comma = values.find(',', comma + 1)) != string::npos) {
100  string flagName = values.substr(comma + 1, new_comma - comma - 1);
101  if (flagName[0] == '-') {
102  string newflag = flagName.substr(1, flagName.length() - 1);
103  BESDebug::Set(newflag, false);
104  }
105  else {
106  BESDebug::Set(flagName, true);
107  }
108  comma = new_comma;
109  }
110  string flagName = values.substr(comma + 1, values.length() - comma - 1);
111  if (flagName[0] == '-') {
112  string newflag = flagName.substr(1, flagName.length() - 1);
113  BESDebug::Set(newflag, false);
114  }
115  else {
116  BESDebug::Set(flagName, true);
117  }
118 }
119 
125 {
126  ostringstream strm;
127  const time_t sctime = time(NULL);
128  const struct tm *sttime = localtime(&sctime);
129  char zone_name[10];
130  strftime(zone_name, sizeof(zone_name), "%Z", sttime);
131  char *b = asctime(sttime);
132  strm << zone_name << " ";
133  for (register int j = 0; b[j] != '\n'; j++)
134  strm << b[j];
135  pid_t thepid = getpid();
136  strm << " id: " << thepid;
137  return strm.str();
138 }
139 
148 void BESDebug::Help(ostream &strm)
149 {
150  strm << "Debug help:" << endl << " Set on the command line with " << "-d \"file_name|cerr,[-]context1,...,[-]context\"" << endl
151  << " context with dash (-) in front will be turned off" << endl << " context of all will turn on debugging for all contexts" << endl << endl
152  << "Possible context(s):" << endl;
153 
154  if (_debug_map.size()) {
155  BESDebug::debug_citer i = _debug_map.begin();
156  BESDebug::debug_citer e = _debug_map.end();
157  for (; i != e; i++) {
158  strm << " " << (*i).first << ": ";
159  if ((*i).second)
160  strm << "on" << endl;
161  else
162  strm << "off" << endl;
163  }
164  }
165  else {
166  strm << " none specified" << endl;
167  }
168 }
169 
170 bool BESDebug::IsContextName(const string &name)
171 {
172  return _debug_map.count(name) > 0;
173 }
174 
183 {
184  ostringstream oss;
185 
186  if (_debug_map.size()) {
187  BESDebug::debug_citer i = _debug_map.begin();
188  BESDebug::debug_citer e = _debug_map.end();
189  for (; i != e; i++) {
190  if (!(*i).second) oss << "-";
191  oss << (*i).first << ",";
192  }
193  string retval = oss.str();
194  return retval.erase(retval.length() - 1);
195  }
196  else {
197  return "";
198  }
199 }
200 
BESDebug::GetPidStr
static std::string GetPidStr()
return the pid as a string
Definition: BESDebug.cc:124
BESDebug::Set
static void Set(const std::string &flagName, bool value)
set the debug context to the specified value
Definition: BESDebug.h:116
BESDebug::SetUp
static void SetUp(const std::string &values)
Sets up debugging for the bes.
Definition: BESDebug.cc:64
BESDebug::GetOptionsString
static std::string GetOptionsString()
Definition: BESDebug.cc:182
BESInternalError
exception thrown if internal error encountered
Definition: BESInternalError.h:43
BESDebug::Help
static void Help(std::ostream &strm)
Writes help information for so that developers know what can be set for debugging.
Definition: BESDebug.cc:148
BESDebug::SetStrm
static void SetStrm(std::ostream *strm, bool created)
set the debug output stream to the specified stream
Definition: BESDebug.h:198