Skip to content
Snippets Groups Projects
Commit 49a0cb08 authored by g0dil's avatar g0dil
Browse files

Utils: Add hexdumpQuote() utility

Utils: Optimize ExceptionMixin implementation
Utils/Console: Add error logging to UDPServer
parent c747ff7e
No related branches found
No related tags found
No related merge requests found
...@@ -165,7 +165,7 @@ prefix_ void senf::scheduler::hiresTimers() ...@@ -165,7 +165,7 @@ prefix_ void senf::scheduler::hiresTimers()
prefix_ senf::log::time_type senf::scheduler::LogTimeSource::operator()() prefix_ senf::log::time_type senf::scheduler::LogTimeSource::operator()()
const const
{ {
return now(); return senf::scheduler::now();
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
......
...@@ -45,7 +45,7 @@ prefix_ senf::console::UDPServer::UDPServer(senf::INet4SocketAddress const & add ...@@ -45,7 +45,7 @@ prefix_ senf::console::UDPServer::UDPServer(senf::INet4SocketAddress const & add
{ {
if (address.address().multicast()) if (address.address().multicast())
handle_.facet<senf::INet4MulticastSocketProtocol>().mcAddMembership(address.address()); handle_.facet<senf::INet4MulticastSocketProtocol>().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) prefix_ senf::console::UDPServer::UDPServer(senf::INet6SocketAddress const & address)
...@@ -58,7 +58,7 @@ prefix_ senf::console::UDPServer::UDPServer(senf::INet6SocketAddress const & add ...@@ -58,7 +58,7 @@ prefix_ senf::console::UDPServer::UDPServer(senf::INet6SocketAddress const & add
{ {
if (address.address().multicast()) if (address.address().multicast())
handle_.facet<senf::INet6MulticastSocketProtocol>().mcAddMembership(address.address()); handle_.facet<senf::INet6MulticastSocketProtocol>().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) prefix_ senf::console::UDPServer & senf::console::UDPServer::replies(bool enable)
...@@ -123,12 +123,14 @@ prefix_ void senf::console::UDPServer::handleInput(int events) ...@@ -123,12 +123,14 @@ prefix_ void senf::console::UDPServer::handleInput(int events)
catch (Executor::ExitException &) { catch (Executor::ExitException &) {
// Ignored // 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) { catch (std::exception & ex) {
std::string msg (ex.what()); stream << ex.what() << '\n';
std::string::size_type i (msg.find("-- \n")); SENF_LOG((senf::log::IMPORTANT)("Error: " << ex.what()));
if (i != std::string::npos)
msg = msg.substr(i+4);
stream << msg << std::endl;
} }
if (replies_ && (emptyReplies_ || ! stream.str().empty())) { if (replies_ && (emptyReplies_ || ! stream.str().empty())) {
if (target_) if (target_)
......
...@@ -47,7 +47,9 @@ prefix_ void senf::ExceptionMixin::addBacktrace() ...@@ -47,7 +47,9 @@ prefix_ void senf::ExceptionMixin::addBacktrace()
std::stringstream ss; std::stringstream ss;
ss << "\nException at\n"; ss << "\nException at\n";
formatBacktrace(ss, entries, nEntries); formatBacktrace(ss, entries, nEntries);
ss << "-- \n" << message_; ss << "-- \n";
excLen_ = ss.str().size();
ss << what_;
what_ = ss.str(); what_ = ss.str();
} }
#endif #endif
......
...@@ -31,23 +31,35 @@ ...@@ -31,23 +31,35 @@
// senf::ExceptionMixin // senf::ExceptionMixin
prefix_ senf::ExceptionMixin::ExceptionMixin(std::string const & description) prefix_ senf::ExceptionMixin::ExceptionMixin(std::string const & description)
: what_(description), : what_(description)
message_(description)
{ {
#ifdef SENF_DEBUG #ifdef SENF_DEBUG
addBacktrace(); addBacktrace();
#endif #endif
} }
prefix_ std::string const & senf::ExceptionMixin::message() prefix_ std::string senf::ExceptionMixin::message()
const const
{ {
#ifdef SENF_DEBUG
return what_.substr(excLen_);
#else
return message_; 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) prefix_ void senf::ExceptionMixin::append(std::string text)
{ {
message_ += text;
what_ += text; what_ += text;
} }
......
...@@ -140,7 +140,8 @@ namespace senf { ...@@ -140,7 +140,8 @@ namespace senf {
class ExceptionMixin class ExceptionMixin
{ {
public: 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 void append(std::string text); ///< Extend exception description
/**< Adds \a text to the description text. */ /**< Adds \a text to the description text. */
...@@ -152,12 +153,13 @@ namespace senf { ...@@ -152,12 +153,13 @@ namespace senf {
string. This should probably be a string constant string. This should probably be a string constant
describing the exception for most derived describing the exception for most derived
exceptions. */ exceptions. */
std::string what_; std::string what_;
private: private:
#ifdef SENF_DEBUG #ifdef SENF_DEBUG
void addBacktrace(); void addBacktrace();
std::string::size_type excLen_;
#endif #endif
std::string message_;
}; };
/** \brief Extensible exception base-class /** \brief Extensible exception base-class
......
...@@ -40,16 +40,17 @@ namespace detail { ...@@ -40,16 +40,17 @@ namespace detail {
: public senf::singleton<TimeSourceManager> : public senf::singleton<TimeSourceManager>
{ {
public: public:
TimeSourceManager();
using senf::singleton<TimeSourceManager>::instance; using senf::singleton<TimeSourceManager>::instance;
time_type now(); time_type now();
void timeSource(std::auto_ptr<TimeSource> source); void timeSource(std::auto_ptr<TimeSource> source);
private: private:
TimeSourceManager();
boost::scoped_ptr<TimeSource> timeSource_; boost::scoped_ptr<TimeSource> timeSource_;
friend class senf::singleton<TimeSourceManager>;
}; };
}}} }}}
......
...@@ -40,6 +40,15 @@ prefix_ void senf::hexdump(Iterator i, Iterator i_end, std::ostream & stream, ...@@ -40,6 +40,15 @@ prefix_ void senf::hexdump(Iterator i, Iterator i_end, std::ostream & stream,
dumper(*i); dumper(*i);
} }
template <class Iterator>
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//////////////////////////////////////// ///////////////////////////////cc.e////////////////////////////////////////
#undef prefix_ #undef prefix_
//#include "hexdump.mpp" //#include "hexdump.mpp"
......
...@@ -34,10 +34,13 @@ ...@@ -34,10 +34,13 @@
namespace senf { 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 <class Iterator> template <class Iterator>
void hexdump(Iterator i, Iterator i_end, std::ostream & stream, unsigned block_size=16); void hexdump(Iterator i, Iterator i_end, std::ostream & stream, unsigned block_size=16);
/** \brief Return quoted iterator range */
template <class Iterator>
std::string hexdumpQuote(Iterator i, Iterator i_end);
} }
///////////////////////////////hh.e//////////////////////////////////////// ///////////////////////////////hh.e////////////////////////////////////////
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment