00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <iostream>
00011 #include <cstdlib>
00012
00013 #include "ers/StreamFactory.h"
00014 #include "ers/DefaultStream.h"
00015 #include "ers/ers.h"
00016
00017
00022 const char* ers::StreamFactory::DEFAULT_STREAMS[] = {
00023 "null:",
00024 "default:", "default:", "default:", "default:",
00025 "default:", "default:", "default:",
00026 "default:verbose", "default:verbose",
00027 "null:" } ;
00028
00029
00033 ers::StreamFactory *ers::StreamFactory::s_instance = 0 ;
00034
00035
00036
00037
00038
00043 ers::StreamFactory::StreamFactory() {
00044 for(int i= static_cast<int> (severity_none);i< static_cast<int> (severity_max);i++) {
00045 severity_t s = static_cast<severity_t> (i) ;
00046 m_streams[s]=0 ;
00047 }
00048 }
00049
00055 ers::StreamFactory::StreamFactory(const StreamFactory &other) {
00056 (void) other ;
00057 ERS_NOT_IMPLEMENTED();
00058 }
00059
00064 ers::StreamFactory::~StreamFactory() {
00065 for(int i= static_cast<int> (severity_none);i< static_cast<int> (severity_max);i++) {
00066 severity_t s = static_cast<severity_t> (i) ;
00067 if(m_streams[s]) {
00068 delete(m_streams[s]);
00069 m_streams[s]=0 ;
00070 }
00071 }
00072 }
00073
00074
00080 ers::StreamFactory* ers::StreamFactory::instance() {
00081 if (0==s_instance) {
00082 s_instance = new StreamFactory();
00083 }
00084 return s_instance ;
00085 }
00086
00089 void ers::StreamFactory::print_registered() {
00090 const StreamFactory *factory = instance();
00091 std::cerr << *factory ;
00092 }
00093
00094
00095
00096
00097
00104 ers::Stream* ers::StreamFactory::get_default_stream(severity_t s) {
00105 return instance()->get_stream(s) ;
00106 }
00107
00119 const char* ers::StreamFactory::key_for_severity(severity_t s) {
00120 char key_buffer[64] ;
00121 snprintf(key_buffer,sizeof(key_buffer),"ERS_%s",ers::Core::to_string(s)) ;
00122 char *c = &(key_buffer[0]);
00123 while(*c) {
00124 *c = toupper(*c) ;
00125 c++ ;
00126 }
00127 const char *env = ::getenv(key_buffer) ;
00128 if (env!=0) return env ;
00129 const char* static_key = DEFAULT_STREAMS[s] ;
00130 if (static_key) return static_key ;
00131 return ers::DefaultStream::KEY ;
00132 }
00133
00143 ers::Stream *ers::StreamFactory::create_stream(const std::string &key) const {
00144 std::string protocol ;
00145 std::string uri ;
00146
00147 std::string::size_type colon = key.find(':') ;
00148 if (colon==std::string::npos) {
00149 protocol = key ;
00150 } else {
00151 protocol = key.substr(0,colon) ;
00152 std::string::size_type uri_start = colon+1 ;
00153 if (uri_start<key.size()) {
00154 uri = key.substr(uri_start) ;
00155 }
00156 }
00157 for(stream_factory_collection::const_iterator pos=m_factories.begin();pos!=m_factories.end();++pos) {
00158 create_stream_callback callback = pos->second;
00159 Stream *s = callback(protocol,uri);
00160 if (s!=0) return s ;
00161 }
00162 fprintf(stderr,"Warning, could not find stream for key protocol=\"%s\" uri=\"%s\" (%s)\n",protocol.c_str(),uri.c_str(),key.c_str());
00163 return new DefaultStream();
00164 }
00165
00176 ers::Stream *ers::StreamFactory::create_stream(severity_t s) {
00177 const char* key = key_for_severity(s);
00178 return create_stream(key);
00179 }
00180
00181
00186 void ers::StreamFactory::fatal(Issue *issue_ptr) {
00187 ERS_PRE_CHECK_PTR(issue_ptr);
00188 issue_ptr->severity(ers::fatal);
00189 dispatch(issue_ptr,false);
00190 }
00191
00196 void ers::StreamFactory::error(Issue *issue_ptr) {
00197 ERS_PRE_CHECK_PTR(issue_ptr);
00198 issue_ptr->severity(ers::error);
00199 dispatch(issue_ptr,false);
00200 }
00201
00206 void ers::StreamFactory::warning(Issue *issue_ptr) {
00207 ERS_PRE_CHECK_PTR(issue_ptr);
00208 issue_ptr->severity(ers::warning);
00209 dispatch(issue_ptr,false);
00210 }
00211
00218 void ers::StreamFactory::debug(Issue *issue_ptr, severity_t s) {
00219 ERS_PRE_CHECK_PTR(issue_ptr);
00220 ERS_PRECONDITION(s<ers::information,"severity_t is not debug : %s (%d) %d",ers::Core::to_string(s),s,ers::information);
00221 issue_ptr->severity(s) ;
00222 dispatch(issue_ptr,false);
00223 }
00224
00231 void ers::StreamFactory::debug(const Context &c, const std::string &message, severity_t s) {
00232 LogIssue log_issue(c,s,message);
00233 debug(&log_issue,s);
00234 }
00235
00241 void ers::StreamFactory::warning(const Context &c, const std::string &message) {
00242 LogIssue log_issue(c,ers::warning,message);
00243 warning(&log_issue);
00244 }
00245
00246
00254 void ers::StreamFactory::dispatch(Issue *issue_ptr, bool throw_error) {
00255 ERS_PRE_CHECK_PTR(issue_ptr);
00256 if (throw_error && issue_ptr->is_error()) { throw *issue_ptr ; }
00257 const severity_t s = issue_ptr->severity() ;
00258 Stream *stream_ptr = instance()->get_stream(s) ;
00259 ERS_CHECK_PTR(stream_ptr);
00260 stream_ptr->send(issue_ptr);
00261 }
00262
00263
00264 void ers::StreamFactory::dispatch(Issue &issue_ref, bool throw_error) {
00265 dispatch(&issue_ref,throw_error);
00266 }
00267
00268
00269 void ers::StreamFactory::set_stream(severity_t s, const std::string &key) {
00270 instance()->set(s,key.c_str()) ;
00271 }
00272
00273
00274
00275
00276
00277
00283 ers::Stream * ers::StreamFactory::get_stream(severity_t s) {
00284 if (m_streams[s]==0) {
00285 m_streams[s]=create_stream(s) ;
00286 }
00287 return m_streams[s] ;
00288 }
00289
00290
00299 void ers::StreamFactory::set(severity_t severity, Stream *s) {
00300 ERS_PRE_CHECK_PTR(s);
00301 ERS_PRECONDITION(severity_none < severity && severity < severity_max,"illegal severity_t %d",(int) severity);
00302 if (m_streams[severity]) {
00303 delete m_streams[severity] ;
00304 }
00305 m_streams[severity] = s ;
00306 }
00307
00316 void ers::StreamFactory::set(severity_t severity, const char* key) {
00317 ERS_PRE_CHECK_PTR(key);
00318 Stream *s = create_stream(key);
00319 set(severity,s);
00320 }
00321
00326 ers::Stream *ers::StreamFactory::fatal() { return get_stream(ers::fatal) ; }
00327
00332 ers::Stream* ers::StreamFactory::error() { return get_stream(ers::error) ; }
00333
00338 ers::Stream* ers::StreamFactory::warning() { return get_stream(ers::warning); }
00339
00340
00347 ers::Stream* ers::StreamFactory::debug(severity_t s) {
00348 ERS_PRECONDITION(s<ers::information && s>ers::severity_none,"severity_t is not debug : %s (%d)",ers::Core::to_string(s),s);
00349 return get_stream(s) ;
00350 }
00351
00352
00353
00354
00367 bool ers::StreamFactory::register_factory(const std::string &name, create_stream_callback callback) {
00368
00369 m_factories[name] = callback ;
00370 return true ;
00371 }
00372
00378 void ers::StreamFactory::write_to(std::ostream& stream) const {
00379 stream << "Stream factory - registered streams\n" ;
00380 stream << "-----------------------------------\n" ;
00381 int i = 0 ;
00382 for(stream_factory_collection::const_iterator pos=m_factories.begin();pos!=m_factories.end();++pos) {
00383 std::string name = pos->first;
00384 stream << i << ")\t" << name << std::endl;
00385 i++ ;
00386 }
00387 stream << "-----------------------------------\n" ;
00388 }
00389
00390
00398 std::ostream& ers::operator<<(std::ostream& stream, const ers::StreamFactory& factory) {
00399 factory.write_to(stream);
00400 return stream ;
00401 }
00402
00403
00404
00405