00001 /* 00002 * HumanStream.cxx 00003 * ers 00004 * 00005 * Created by Matthias Wiesmann on 01.12.04. 00006 * Copyright 2004 CERN. All rights reserved. 00007 * 00008 */ 00009 00010 #include <assert.h> 00011 #include "ers/ers.h" 00012 #include "ers/HumanStream.h" 00013 00014 00015 const char* const ers::HumanStream::KEY = "human" ; 00016 00017 namespace { 00018 ers::Stream *create_stream(const std::string &protocol, const std::string &uri) { 00019 (void) uri ; 00020 if (protocol==ers::HumanStream::KEY) { 00021 return new ers::HumanStream() ; 00022 } 00023 return 0 ; 00024 } // 00025 bool registered = ers::StreamFactory::instance()->register_factory(ers::HumanStream::KEY,create_stream) ; 00026 } // anonymous namespace 00027 00028 00035 std::string ers::HumanStream::to_string(const Issue *issue_ptr) throw() { 00036 try { 00037 HumanStream ers_stream ; 00038 ers_stream.send(issue_ptr) ; 00039 std::string str = ers_stream.to_string(); 00040 return str ; 00041 } catch (std::exception &ex) { 00042 std::string msg = "error while building message " + std::string(ex.what()) ; 00043 return msg; 00044 } // 00045 } // to_string 00046 00047 ers::HumanStream::HumanStream() : ers::Stream() {} // HumanStream 00048 ers::HumanStream::HumanStream(const HumanStream &other) : ers::Stream(other) {} 00049 ers::HumanStream::~HumanStream() {} 00050 00054 std::string ers::HumanStream::to_string() { 00055 return m_out_stream.str(); 00056 } // to_string 00057 00061 void ers::HumanStream::clear() { 00062 m_out_stream.str(""); 00063 } // clear 00064 00065 void ers::HumanStream::print_to(std::ostream& stream) const { 00066 stream << ers::HumanStream::KEY << ':' ; 00067 } // print_to 00068 00069 void ers::HumanStream::send(const Issue *issue_ptr) { 00070 ERS_PRE_CHECK_PTR(issue_ptr); 00071 if (m_out_stream) { 00072 try { 00073 const string_map_type *table = issue_ptr->get_value_table(); 00074 const std::string message_str = issue_ptr->get_value(Issue::MESSAGE_KEY) ; 00075 const std::string severity_str = issue_ptr->get_value(Issue::SEVERITY_KEY) ; 00076 m_out_stream << "issue: " << message_str << "(" << severity_str << ")[" ; 00077 bool first = true ; 00078 for(string_map_type::const_iterator pos = table->begin();pos!=table->end();++pos) { 00079 const std::string &key = pos->first ; 00080 const std::string &value = pos->second ; 00081 if (first) { 00082 first = false ; 00083 } else { 00084 m_out_stream << ", " ; 00085 } // first or not 00086 m_out_stream << key << "=\"" << value << '\"'; 00087 } // for 00088 m_out_stream << "]" ; 00089 } catch (std::ios_base::failure &ex) { 00090 throw ers::Issue(ERS_HERE,ers::error,&ex); 00091 } // catch generic I/O errors 00092 } // if m_out_stream 00093 } // send 00094 00095 00096 00097