45#include "BESInternalError.h"
50ostream *BESDebug::_debug_strm = NULL;
51bool BESDebug::_debug_strm_created =
false;
52map<string, bool> BESDebug::_debug_map;
59string get_debug_log_line_prefix()
63 const time_t sctime = time(NULL);
65 localtime_r(&sctime, &sttime);
67 strftime(zone_name,
sizeof(zone_name),
"%Z", &sttime);
71 strm <<
"[" << zone_name <<
" ";
72 for (
size_t j = 0; b[j] !=
'\n' && j<32; j++)
77 pid_t thepid = getpid();
78 strm <<
"[pid:" << thepid <<
"]";
81 strm <<
"[thread:" << pthread_self() <<
"]";
100 if (values.empty()) {
101 string err =
"Empty debug options";
104 string::size_type comma = 0;
105 comma = values.find(
',');
106 if (comma == string::npos) {
107 string err =
"Missing comma in debug options: " + values;
111 bool created =
false;
112 string s_strm = values.substr(0, comma);
113 if (s_strm ==
"cerr") {
116 else if (s_strm ==
"LOG") {
117 strm = BESLog::TheLog()->get_log_ostream();
120 strm =
new ofstream(s_strm.c_str(), ios::out);
121 if (strm && strm->fail()) {
124 string err =
"Unable to open the debug file: " + s_strm;
132 string::size_type new_comma = 0;
133 while ((new_comma = values.find(
',', comma + 1)) != string::npos) {
134 string flagName = values.substr(comma + 1, new_comma - comma - 1);
135 if (flagName[0] ==
'-') {
136 string newflag = flagName.substr(1, flagName.length() - 1);
144 string flagName = values.substr(comma + 1, values.length() - comma - 1);
145 if (flagName[0] ==
'-') {
146 string newflag = flagName.substr(1, flagName.length() - 1);
164 strm <<
"Debug help:" << endl <<
" Set on the command line with " <<
"-d \"file_name|cerr,[-]context1,...,[-]context\"" << endl
165 <<
" context with dash (-) in front will be turned off" << endl <<
" context of all will turn on debugging for all contexts" << endl << endl
166 <<
"Possible context(s):" << endl;
168 if (_debug_map.size()) {
169 BESDebug::debug_citer i = _debug_map.begin();
170 BESDebug::debug_citer e = _debug_map.end();
171 for (; i != e; i++) {
172 strm <<
" " << (*i).first <<
": ";
174 strm <<
"on" << endl;
176 strm <<
"off" << endl;
180 strm <<
" none specified" << endl;
184bool BESDebug::IsContextName(
const string &name)
186 return _debug_map.count(name) > 0;
200 if (_debug_map.size()) {
201 BESDebug::debug_citer i = _debug_map.begin();
202 BESDebug::debug_citer e = _debug_map.end();
203 for (; i != e; i++) {
204 if (!(*i).second) oss <<
"-";
205 oss << (*i).first <<
",";
207 string retval = oss.str();
208 return retval.erase(retval.length() - 1);
static void SetStrm(std::ostream *strm, bool created)
set the debug output stream to the specified stream
static void SetUp(const std::string &values)
Sets up debugging for the bes.
static void Help(std::ostream &strm)
Writes help information for so that developers know what can be set for debugging.
static std::string GetOptionsString()
static void Set(const std::string &flagName, bool value)
set the debug context to the specified value
exception thrown if internal error encountered