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