BESDebug.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "config.h"
00034
00035 #include <time.h>
00036
00037 #include <fstream>
00038 #include <iostream>
00039 #include <sstream>
00040
00041 using std::ofstream ;
00042 using std::ios ;
00043 using std::cout ;
00044 using std::endl ;
00045 using std::ostringstream ;
00046
00047 #include "BESDebug.h"
00048 #include "BESInternalError.h"
00049
00050 ostream *BESDebug::_debug_strm = NULL ;
00051 bool BESDebug::_debug_strm_created = false ;
00052 map<string,bool> BESDebug::_debug_map ;
00053
00066 void
00067 BESDebug::SetUp( const string &values )
00068 {
00069 if( values.empty() )
00070 {
00071 string err = "Empty debug options" ;
00072 throw BESInternalError( err, __FILE__, __LINE__ ) ;
00073 }
00074 string::size_type comma = 0 ;
00075 comma = values.find( ',' ) ;
00076 if( comma == string::npos )
00077 {
00078 string err = "Missing comma in debug options: " + values ;
00079 throw BESInternalError( err, __FILE__, __LINE__ ) ;
00080 }
00081 ostream *strm = 0 ;
00082 bool created = false ;
00083 string s_strm = values.substr( 0, comma ) ;
00084 if( s_strm == "cerr" )
00085 {
00086 strm = &cerr ;
00087 }
00088 else
00089 {
00090 strm = new ofstream( s_strm.c_str(), ios::out ) ;
00091 if( strm && !(*strm) )
00092 {
00093 delete strm ;
00094 strm = 0 ;
00095 string err = "Unable to open the debug file: " + s_strm ;
00096 throw BESInternalError( err, __FILE__, __LINE__ ) ;
00097 }
00098 created = true ;
00099 }
00100
00101 BESDebug::SetStrm( strm, created ) ;
00102
00103 string::size_type new_comma = 0 ;
00104 while( ( new_comma = values.find( ',', comma+1 ) ) != string::npos )
00105 {
00106 string flagName = values.substr( comma+1, new_comma-comma-1 ) ;
00107 if( flagName[0] == '-' )
00108 {
00109 string newflag = flagName.substr( 1, flagName.length() - 1 ) ;
00110 BESDebug::Set( newflag, false ) ;
00111 }
00112 else
00113 {
00114 BESDebug::Set( flagName, true ) ;
00115 }
00116 comma = new_comma ;
00117 }
00118 string flagName = values.substr( comma+1, values.length()-comma-1 ) ;
00119 if( flagName[0] == '-' )
00120 {
00121 string newflag = flagName.substr( 1, flagName.length() - 1 ) ;
00122 BESDebug::Set( newflag, false ) ;
00123 }
00124 else
00125 {
00126 BESDebug::Set( flagName, true ) ;
00127 }
00128 }
00129
00134 string
00135 BESDebug::GetPidStr()
00136 {
00137 ostringstream strm ;
00138 const time_t sctime = time( NULL ) ;
00139 const struct tm *sttime = localtime( &sctime ) ;
00140 char zone_name[10] ;
00141 strftime( zone_name, sizeof( zone_name ), "%Z", sttime ) ;
00142 char *b = asctime( sttime ) ;
00143 strm << zone_name << " " ;
00144 for( register int j = 0; b[j] != '\n'; j++ )
00145 strm << b[j] ;
00146 pid_t thepid = getpid() ;
00147 strm << " id: " << thepid ;
00148 return strm.str() ;
00149 }
00150
00159 void
00160 BESDebug::Help( ostream &strm )
00161 {
00162 strm << "Debug help:" << endl
00163 << " Set on the command line with "
00164 << "-d \"file_name|cerr,[-]context1,...,[-]contextn\"" << endl
00165 << " context with dash (-) in front will be turned off" << endl
00166 << " context of all will turn on debugging for all context" << endl
00167 << endl
00168 << "Possible context:" << endl ;
00169
00170 if( _debug_map.size() )
00171 {
00172 BESDebug::_debug_citer i = _debug_map.begin() ;
00173 BESDebug::_debug_citer e = _debug_map.end() ;
00174 for( ; i != e; i++ )
00175 {
00176 strm << " " << (*i).first << ": " ;
00177 if( (*i).second )
00178 strm << "on" << endl ;
00179 else
00180 strm << "off" << endl ;
00181 }
00182 }
00183 else
00184 {
00185 strm << " none specified" << endl ;
00186 }
00187 }
00188