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 <fstream>
00034 #include <iostream>
00035
00036 using std::ofstream ;
00037 using std::ios ;
00038 using std::cout ;
00039 using std::endl ;
00040
00041 #include "BESDebug.h"
00042 #include "BESInternalError.h"
00043
00044 ostream *BESDebug::_debug_strm = NULL ;
00045 bool BESDebug::_debug_strm_created = false ;
00046 string BESDebug::_pid_str = "" ;
00047 map<string,bool> BESDebug::_debug_map ;
00048
00061 void
00062 BESDebug::SetUp( const string &values )
00063 {
00064 if( values.empty() )
00065 {
00066 string err = "Empty debug options" ;
00067 throw BESInternalError( err, __FILE__, __LINE__ ) ;
00068 }
00069 string::size_type comma = 0 ;
00070 comma = values.find( ',' ) ;
00071 if( comma == string::npos )
00072 {
00073 string err = "Missing comma in debug options: " + values ;
00074 throw BESInternalError( err, __FILE__, __LINE__ ) ;
00075 }
00076 ostream *strm = 0 ;
00077 bool created = false ;
00078 string s_strm = values.substr( 0, comma ) ;
00079 if( s_strm == "cerr" )
00080 {
00081 strm = &cerr ;
00082 }
00083 else
00084 {
00085 strm = new ofstream( s_strm.c_str(), ios::out ) ;
00086 if( strm && !(*strm) )
00087 {
00088 delete strm ;
00089 strm = 0 ;
00090 string err = "Unable to open the debug file: " + s_strm ;
00091 throw BESInternalError( err, __FILE__, __LINE__ ) ;
00092 }
00093 created = true ;
00094 }
00095
00096 BESDebug::SetStrm( strm, created ) ;
00097
00098 string::size_type new_comma = 0 ;
00099 while( ( new_comma = values.find( ',', comma+1 ) ) != string::npos )
00100 {
00101 string flagName = values.substr( comma+1, new_comma-comma-1 ) ;
00102 if( flagName[0] == '-' )
00103 {
00104 string newflag = flagName.substr( 1, flagName.length() - 1 ) ;
00105 BESDebug::Set( newflag, false ) ;
00106 }
00107 else
00108 {
00109 BESDebug::Set( flagName, true ) ;
00110 }
00111 comma = new_comma ;
00112 }
00113 string flagName = values.substr( comma+1, values.length()-comma-1 ) ;
00114 if( flagName[0] == '-' )
00115 {
00116 string newflag = flagName.substr( 1, flagName.length() - 1 ) ;
00117 BESDebug::Set( newflag, false ) ;
00118 }
00119 else
00120 {
00121 BESDebug::Set( flagName, true ) ;
00122 }
00123 }
00124
00132 void
00133 BESDebug::Help( ostream &strm )
00134 {
00135 strm << "Debug help:" << endl
00136 << endl
00137 << "Set on the command line with "
00138 << "-d \"file_name|cerr,[-]context1,[-]context2,...,[-]contextn\"" << endl
00139 << " context with dash (-) in front will be turned off" << endl
00140 << endl
00141 << "Possible context:" << endl ;
00142
00143 if( _debug_map.size() )
00144 {
00145 BESDebug::_debug_citer i = _debug_map.begin() ;
00146 BESDebug::_debug_citer e = _debug_map.end() ;
00147 for( ; i != e; i++ )
00148 {
00149 strm << " " << (*i).first << ": " ;
00150 if( (*i).second )
00151 strm << "on" << endl ;
00152 else
00153 strm << "off" << endl ;
00154 }
00155 }
00156 else
00157 {
00158 strm << " none specified" << endl ;
00159 }
00160 }
00161