From: g0dil Date: Tue, 7 Jul 2009 10:59:36 +0000 (+0000) Subject: Utils: Add hexdumpQuote() utility X-Git-Url: http://g0dil.de/git?a=commitdiff_plain;h=b3c7235d21911975e082ab5e1565a0add93eb85e;p=senf.git Utils: Add hexdumpQuote() utility Utils: Optimize ExceptionMixin implementation Utils/Console: Add error logging to UDPServer git-svn-id: https://svn.berlios.de/svnroot/repos/senf/trunk@1257 270642c3-0616-0410-b53a-bc976706d245 --- diff --git a/Scheduler/Scheduler.cc b/Scheduler/Scheduler.cc index 471b743..1f94643 100644 --- a/Scheduler/Scheduler.cc +++ b/Scheduler/Scheduler.cc @@ -165,7 +165,7 @@ prefix_ void senf::scheduler::hiresTimers() prefix_ senf::log::time_type senf::scheduler::LogTimeSource::operator()() const { - return now(); + return senf::scheduler::now(); } /////////////////////////////////////////////////////////////////////////// diff --git a/Utils/Console/UDPServer.cc b/Utils/Console/UDPServer.cc index 312a03d..8a0c33b 100644 --- a/Utils/Console/UDPServer.cc +++ b/Utils/Console/UDPServer.cc @@ -45,7 +45,7 @@ prefix_ senf::console::UDPServer::UDPServer(senf::INet4SocketAddress const & add { if (address.address().multicast()) handle_.facet().mcAddMembership(address.address()); - SENF_LOG(("UDP Console server started at " << address)); + SENF_LOG(("UDP Console server started at " << address )); } prefix_ senf::console::UDPServer::UDPServer(senf::INet6SocketAddress const & address) @@ -58,7 +58,7 @@ prefix_ senf::console::UDPServer::UDPServer(senf::INet6SocketAddress const & add { if (address.address().multicast()) handle_.facet().mcAddMembership(address.address()); - SENF_LOG(("UDP Console server started at " << address)); + SENF_LOG(("UDP Console server started at " << address )); } prefix_ senf::console::UDPServer & senf::console::UDPServer::replies(bool enable) @@ -123,12 +123,14 @@ prefix_ void senf::console::UDPServer::handleInput(int events) catch (Executor::ExitException &) { // Ignored } + catch (ExceptionMixin & ex) { + stream << ex.message() << '\n'; + SENF_LOG((senf::log::IMPORTANT)("Error: " << ex.message())); + SENF_LOG((senf::log::NOTICE)(ex.backtrace())); + } catch (std::exception & ex) { - std::string msg (ex.what()); - std::string::size_type i (msg.find("-- \n")); - if (i != std::string::npos) - msg = msg.substr(i+4); - stream << msg << std::endl; + stream << ex.what() << '\n'; + SENF_LOG((senf::log::IMPORTANT)("Error: " << ex.what())); } if (replies_ && (emptyReplies_ || ! stream.str().empty())) { if (target_) diff --git a/Utils/Exception.cc b/Utils/Exception.cc index 737214d..ee1ec18 100644 --- a/Utils/Exception.cc +++ b/Utils/Exception.cc @@ -47,7 +47,9 @@ prefix_ void senf::ExceptionMixin::addBacktrace() std::stringstream ss; ss << "\nException at\n"; formatBacktrace(ss, entries, nEntries); - ss << "-- \n" << message_; + ss << "-- \n"; + excLen_ = ss.str().size(); + ss << what_; what_ = ss.str(); } #endif diff --git a/Utils/Exception.cci b/Utils/Exception.cci index 3366fe2..852852a 100644 --- a/Utils/Exception.cci +++ b/Utils/Exception.cci @@ -31,23 +31,35 @@ // senf::ExceptionMixin prefix_ senf::ExceptionMixin::ExceptionMixin(std::string const & description) - : what_(description), - message_(description) + : what_(description) { #ifdef SENF_DEBUG addBacktrace(); #endif } -prefix_ std::string const & senf::ExceptionMixin::message() +prefix_ std::string senf::ExceptionMixin::message() const { +#ifdef SENF_DEBUG + return what_.substr(excLen_); +#else return message_; +#endif +} + +prefix_ std::string senf::ExceptionMixin::backtrace() + const +{ +#ifdef SENF_DEBUG + return what_.substr(0,excLen_-4); +#else + return ""; +#endif } prefix_ void senf::ExceptionMixin::append(std::string text) { - message_ += text; what_ += text; } diff --git a/Utils/Exception.hh b/Utils/Exception.hh index a91b157..c6f3257 100644 --- a/Utils/Exception.hh +++ b/Utils/Exception.hh @@ -140,7 +140,8 @@ namespace senf { class ExceptionMixin { public: - std::string const & message() const; ///< get exception description + std::string message() const; ///< get exception description + std::string backtrace() const; ///< Return backtrace (if available) void append(std::string text); ///< Extend exception description /**< Adds \a text to the description text. */ @@ -152,12 +153,13 @@ namespace senf { string. This should probably be a string constant describing the exception for most derived exceptions. */ + std::string what_; private: #ifdef SENF_DEBUG void addBacktrace(); + std::string::size_type excLen_; #endif - std::string message_; }; /** \brief Extensible exception base-class diff --git a/Utils/Logger/TimeSource.ih b/Utils/Logger/TimeSource.ih index 9d02972..d1a73b1 100644 --- a/Utils/Logger/TimeSource.ih +++ b/Utils/Logger/TimeSource.ih @@ -40,16 +40,17 @@ namespace detail { : public senf::singleton { public: - TimeSourceManager(); - using senf::singleton::instance; time_type now(); void timeSource(std::auto_ptr source); private: + TimeSourceManager(); boost::scoped_ptr timeSource_; + + friend class senf::singleton; }; }}} diff --git a/Utils/hexdump.ct b/Utils/hexdump.ct index 76abf99..277c671 100644 --- a/Utils/hexdump.ct +++ b/Utils/hexdump.ct @@ -40,6 +40,15 @@ prefix_ void senf::hexdump(Iterator i, Iterator i_end, std::ostream & stream, dumper(*i); } +template +prefix_ std::string senf::hexdumpQuote(Iterator i, Iterator i_end) +{ + std::string s; + for (; i != i_end; ++i) + s.push_back(*i >= ' ' && *i <= 126 ? *i : '.'); + return s; +} + ///////////////////////////////cc.e//////////////////////////////////////// #undef prefix_ //#include "hexdump.mpp" diff --git a/Utils/hexdump.hh b/Utils/hexdump.hh index f5a60e6..f43f505 100644 --- a/Utils/hexdump.hh +++ b/Utils/hexdump.hh @@ -34,10 +34,13 @@ namespace senf { - /** \brief Write range [ i, i_end ) to output stream in hexadecimal format - */ + /** \brief Write range [ i, i_end ) to output stream in hexadecimal format */ template void hexdump(Iterator i, Iterator i_end, std::ostream & stream, unsigned block_size=16); + + /** \brief Return quoted iterator range */ + template + std::string hexdumpQuote(Iterator i, Iterator i_end); } ///////////////////////////////hh.e////////////////////////////////////////