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 <sstream>
00034 #include <iostream>
00035 #include <fstream>
00036
00037 using std::ostringstream ;
00038 using std::ifstream ;
00039
00040 #include "BESInfo.h"
00041 #include "TheBESKeys.h"
00042 #include "BESHandlerException.h"
00043
00044 #define BES_INFO_FILE_BUFFER_SIZE 4096
00045
00051 BESInfo::BESInfo( )
00052 : _strm( 0 ),
00053 _buffered( true )
00054 {
00055 _strm = new ostringstream ;
00056 }
00057
00066 BESInfo::BESInfo( const string &key )
00067 : _strm( 0 ),
00068 _buffered( true )
00069 {
00070 bool found = false ;
00071 string b = TheBESKeys::TheKeys()->get_key( key, found ) ;
00072 if( b == "true" || b == "True" || b == "TRUE" ||
00073 b == "yes" || b == "Yes" || b == "YES" )
00074 {
00075 _strm = new ostringstream ;
00076 _buffered = true ;
00077 }
00078 else
00079 {
00080 _buffered = false ;
00081 }
00082 }
00083
00084 BESInfo::~BESInfo()
00085 {
00086 if( _buffered && _strm ) delete _strm ;
00087 }
00088
00089 void
00090 BESInfo::begin_response( const string &response_name )
00091 {
00092 _response_started = true ;
00093 _response_name = response_name ;
00094 }
00095
00096 void
00097 BESInfo::end_response( )
00098 {
00099 _response_started = false ;
00100 if( _tags.size() )
00101 {
00102 string s = "Not all tags were ended in info response" ;
00103 throw BESHandlerException( s, __FILE__, __LINE__ ) ;
00104 }
00105 }
00106
00107 void
00108 BESInfo::begin_tag( const string &tag_name,
00109 map<string,string> *attrs )
00110 {
00111 _tags.push( tag_name ) ;
00112 }
00113
00114 void
00115 BESInfo::end_tag( const string &tag_name )
00116 {
00117 if( _tags.size() == 0 || _tags.top() != tag_name )
00118 {
00119 string s = (string)"tag " + tag_name
00120 + " already ended or not started" ;
00121 throw BESHandlerException( s, __FILE__, __LINE__ ) ;
00122 }
00123 else
00124 {
00125 _tags.pop() ;
00126 }
00127 }
00128
00134 void
00135 BESInfo::add_data( const string &s )
00136 {
00137 if( !_buffered )
00138 {
00139 fprintf( stdout, "%s", s.c_str() ) ;
00140 }
00141 else
00142 {
00143 (*_strm) << s ;
00144 }
00145 }
00146
00162 void
00163 BESInfo::add_data_from_file( const string &key, const string &name )
00164 {
00165 bool found = false ;
00166 string file = TheBESKeys::TheKeys()->get_key( key, found ) ;
00167 if( found == false )
00168 {
00169 add_data( name + " file key " + key + " not found, information not available\n" ) ;
00170 }
00171 else
00172 {
00173 ifstream ifs( file.c_str() ) ;
00174 if( !ifs )
00175 {
00176 add_data( name + " file " + file + " not found, information not available\n" ) ;
00177 }
00178 else
00179 {
00180 char line[BES_INFO_FILE_BUFFER_SIZE] ;
00181 while( !ifs.eof() )
00182 {
00183 ifs.getline( line, BES_INFO_FILE_BUFFER_SIZE ) ;
00184 if( !ifs.eof() )
00185 {
00186 add_data( line ) ;
00187 add_data( "\n" ) ;
00188 }
00189 }
00190 ifs.close() ;
00191 }
00192 }
00193 }
00194
00204 void
00205 BESInfo::add_exception( BESException &e )
00206 {
00207 begin_tag( "BESException" ) ;
00208 add_tag( "Type", e.get_context() ) ;
00209 add_tag( "Message", e.get_message() ) ;
00210 begin_tag( "Location" ) ;
00211 add_tag( "File", e.get_file() ) ;
00212 ostringstream sline ;
00213 sline << e.get_line() ;
00214 add_tag( "Line", sline.str() ) ;
00215 end_tag( "Location" ) ;
00216 end_tag( "BESException" ) ;
00217 }
00218
00227 void
00228 BESInfo::print(FILE *out)
00229 {
00230 if( _buffered )
00231 {
00232 fprintf( out, "%s", ((ostringstream *)_strm)->str().c_str() ) ;
00233 }
00234 }
00235
00243 void
00244 BESInfo::dump( ostream &strm ) const
00245 {
00246 strm << BESIndent::LMarg << "BESInfo::dump - ("
00247 << (void *)this << ")" << endl ;
00248 BESIndent::Indent() ;
00249 strm << BESIndent::LMarg << "response name: " << _response_name << endl ;
00250 strm << BESIndent::LMarg << "is it buffered? " << _buffered << endl ;
00251 strm << BESIndent::LMarg << "has response been started? " << _response_started << endl ;
00252 if( _tags.size() )
00253 {
00254 strm << BESIndent::LMarg << "tags:" << endl ;
00255 BESIndent::Indent() ;
00256 stack<string> temp_tags = _tags ;
00257 while( !temp_tags.empty() )
00258 {
00259 string tag = temp_tags.top() ;
00260 strm << BESIndent::LMarg << tag << endl ;
00261 temp_tags.pop() ;
00262 }
00263 BESIndent::UnIndent() ;
00264 }
00265 else
00266 {
00267 strm << BESIndent::LMarg << "tags: empty" << endl ;
00268 }
00269 BESIndent::UnIndent() ;
00270 }
00271