bes  Updated for version 3.20.8
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 <pthread.h>
43 
44 #include "BESDebug.h"
45 #include "BESInternalError.h"
46 #include "BESLog.h"
47 
48 using namespace std;
49 
50 ostream *BESDebug::_debug_strm = NULL;
51 bool BESDebug::_debug_strm_created = false;
52 map<string, bool> BESDebug::_debug_map;
53 
54 
59 string get_debug_log_line_prefix()
60 {
61  ostringstream strm;
62  // Time Field
63  const time_t sctime = time(NULL);
64  const struct tm *sttime = localtime(&sctime);
65  char zone_name[10];
66  strftime(zone_name, sizeof(zone_name), "%Z", sttime);
67 
68  char b[32]; // The linux man-page for asctime_r() says "at least 26 bytes".
69  asctime_r(sttime,b);
70  strm << "[" << zone_name << " ";
71  for (size_t j = 0; b[j] != '\n' && j<32; j++)
72  strm << b[j];
73  strm << "]";
74 
75  // PID field
76  pid_t thepid = getpid();
77  strm << "[pid:" << thepid <<"]";
78 
79  // Thread field
80  strm << "[thread:" << pthread_self() <<"]";
81  return strm.str();
82 }
83 
84 
97 void BESDebug::SetUp(const string &values)
98 {
99  if (values.empty()) {
100  string err = "Empty debug options";
101  throw BESInternalError(err, __FILE__, __LINE__);
102  }
103  string::size_type comma = 0;
104  comma = values.find(',');
105  if (comma == string::npos) {
106  string err = "Missing comma in debug options: " + values;
107  throw BESInternalError(err, __FILE__, __LINE__);
108  }
109  ostream *strm = 0;
110  bool created = false;
111  string s_strm = values.substr(0, comma);
112  if (s_strm == "cerr") {
113  strm = &cerr;
114  }
115  else if (s_strm == "LOG") {
116  strm = BESLog::TheLog()->get_log_ostream();
117  }
118  else {
119  strm = new ofstream(s_strm.c_str(), ios::out);
120  if (strm && strm->fail()) {
121  delete strm;
122  strm = 0;
123  string err = "Unable to open the debug file: " + s_strm;
124  throw BESInternalError(err, __FILE__, __LINE__);
125  }
126  created = true;
127  }
128 
129  BESDebug::SetStrm(strm, created);
130 
131  string::size_type new_comma = 0;
132  while ((new_comma = values.find(',', comma + 1)) != string::npos) {
133  string flagName = values.substr(comma + 1, new_comma - comma - 1);
134  if (flagName[0] == '-') {
135  string newflag = flagName.substr(1, flagName.length() - 1);
136  BESDebug::Set(newflag, false);
137  }
138  else {
139  BESDebug::Set(flagName, true);
140  }
141  comma = new_comma;
142  }
143  string flagName = values.substr(comma + 1, values.length() - comma - 1);
144  if (flagName[0] == '-') {
145  string newflag = flagName.substr(1, flagName.length() - 1);
146  BESDebug::Set(newflag, false);
147  }
148  else {
149  BESDebug::Set(flagName, true);
150  }
151 }
152 
161 void BESDebug::Help(ostream &strm)
162 {
163  strm << "Debug help:" << endl << " Set on the command line with " << "-d \"file_name|cerr,[-]context1,...,[-]context\"" << endl
164  << " context with dash (-) in front will be turned off" << endl << " context of all will turn on debugging for all contexts" << endl << endl
165  << "Possible context(s):" << endl;
166 
167  if (_debug_map.size()) {
168  BESDebug::debug_citer i = _debug_map.begin();
169  BESDebug::debug_citer e = _debug_map.end();
170  for (; i != e; i++) {
171  strm << " " << (*i).first << ": ";
172  if ((*i).second)
173  strm << "on" << endl;
174  else
175  strm << "off" << endl;
176  }
177  }
178  else {
179  strm << " none specified" << endl;
180  }
181 }
182 
183 bool BESDebug::IsContextName(const string &name)
184 {
185  return _debug_map.count(name) > 0;
186 }
187 
196 {
197  ostringstream oss;
198 
199  if (_debug_map.size()) {
200  BESDebug::debug_citer i = _debug_map.begin();
201  BESDebug::debug_citer e = _debug_map.end();
202  for (; i != e; i++) {
203  if (!(*i).second) oss << "-";
204  oss << (*i).first << ",";
205  }
206  string retval = oss.str();
207  return retval.erase(retval.length() - 1);
208  }
209  else {
210  return "";
211  }
212 }
213 
static void SetStrm(std::ostream *strm, bool created)
set the debug output stream to the specified stream
Definition: BESDebug.h:201
static void SetUp(const std::string &values)
Sets up debugging for the bes.
Definition: BESDebug.cc:97
static void Help(std::ostream &strm)
Writes help information for so that developers know what can be set for debugging.
Definition: BESDebug.cc:161
static std::string GetOptionsString()
Definition: BESDebug.cc:195
static void Set(const std::string &flagName, bool value)
set the debug context to the specified value
Definition: BESDebug.h:119
exception thrown if internal error encountered