X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Utils%2FConsole%2FServer.cc;h=a1bad60e45e27a1a2b53e43b9e1e77e93fb57951;hb=d5a72d0b3f6fee56dba6de1c54cafb448ebe3457;hp=d3261331bc6c3b71f05067576bf0921eb58c641b;hpb=368058ef8f5eb65ea7e210351af25f49ddd0f342;p=senf.git diff --git a/Utils/Console/Server.cc b/Utils/Console/Server.cc index d326133..a1bad60 100644 --- a/Utils/Console/Server.cc +++ b/Utils/Console/Server.cc @@ -27,6 +27,7 @@ #include "Server.ih" // Custom includes +#include #include #include #include @@ -36,6 +37,9 @@ #include "../../Utils/membind.hh" #include "../../Utils/Logger/SenfLog.hh" #include "LineEditor.hh" +#include "ScopedDirectory.hh" +#include "Sysdir.hh" +#include "ParsedCommand.hh" //#include "Server.mpp" #define prefix_ @@ -93,7 +97,8 @@ prefix_ senf::console::Server::Server(ServerHandle handle) : handle_ (handle), event_ ("senf::console::Server", senf::membind(&Server::newClient, this), handle_, scheduler::FdEvent::EV_READ), - root_ (senf::console::root().thisptr()), mode_ (Automatic) + root_ (senf::console::root().thisptr()), mode_ (Automatic), + name_ (::program_invocation_short_name) {} prefix_ void senf::console::Server::newClient(int event) @@ -101,12 +106,20 @@ prefix_ void senf::console::Server::newClient(int event) ServerHandle::ClientHandle client (handle_.accept()); boost::intrusive_ptr p (new Client(*this, client)); clients_.insert( p ); - SENF_LOG(( "Registered new client " << p.get() )); + SENF_LOG(( "Registered new client " << client.peer() )); } prefix_ void senf::console::Server::removeClient(Client & client) { - SENF_LOG(( "Disposing client " << & client )); + SENF_LOG_BLOCK(({ + log << "Disposing client "; + try { + log << client.handle().peer(); + } + catch (senf::SystemException ex) { + log << "(unknown)"; + } + })); // THIS DELETES THE CLIENT INSTANCE !! clients_.erase(boost::intrusive_ptr(&client)); } @@ -175,6 +188,12 @@ prefix_ void senf::console::detail::DumbClientReader::v_write(std::string const handle().write(data); } +prefix_ unsigned senf::console::detail::DumbClientReader::v_width() + const +{ + return 80; +} + /////////////////////////////////////////////////////////////////////////// // senf::console::detail::NoninteractiveClientReader @@ -197,6 +216,12 @@ prefix_ void senf::console::detail::NoninteractiveClientReader::v_write(std::str handle().write(data); } +prefix_ unsigned senf::console::detail::NoninteractiveClientReader::v_width() + const +{ + return 80; +} + prefix_ void senf::console::detail::NoninteractiveClientReader::newData(int event) { @@ -219,8 +244,9 @@ senf::console::detail::NoninteractiveClientReader::newData(int event) // senf::console::Client prefix_ senf::console::Client::Client(Server & server, ClientHandle handle) - : out_t(boost::ref(*this)), senf::log::IOStreamTarget(out_t::member), server_ (server), - handle_ (handle), + : out_t(boost::ref(*this)), + senf::log::IOStreamTarget("client-" + senf::str(handle.peer()), out_t::member), + server_ (server), handle_ (handle), readevent_ ("senf::console::Client::interactive_check", boost::bind(&Client::setNoninteractive,this), handle, scheduler::FdEvent::EV_READ, false), @@ -266,11 +292,6 @@ prefix_ void senf::console::Client::setNoninteractive() prefix_ std::string::size_type senf::console::Client::handleInput(std::string data, bool incremental) { - if (data.empty() && ! incremental) - data = lastCommand_; - else - lastCommand_ = data; - std::string::size_type n (data.size()); try { @@ -284,14 +305,21 @@ prefix_ std::string::size_type senf::console::Client::handleInput(std::string da _1 )); } catch (Executor::ExitException &) { - // This generates an EOF condition on the Handle. This EOF condition is expected - // to be handled gracefully by the ClientReader. We cannot call stop() here, since we - // are called from the client reader callback and that will continue executing even if we - // call stop here ... + // This generates an EOF condition on the Handle. This EOF condition is expected to be + // handled gracefully by the ClientReader. We cannot call stop() here, since we are called + // from the client reader callback and that will continue executing after stop() has been + // called. stop() however will delete *this instance ... BANG ... handle_.facet().shutdown(senf::TCPSocketProtocol::ShutRD); } catch (std::exception & ex) { - stream() << ex.what() << std::endl; + std::string msg (ex.what()); + std::string::size_type i (msg.find("-- \n")); + if (i != std::string::npos) { + backtrace_ = msg.substr(0,i); + msg = msg.substr(i+4); + } else + backtrace_.clear(); + stream() << msg << std::endl; } catch (...) { stream() << "unidentified error (unknown exception thrown)" << std::endl; @@ -310,33 +338,37 @@ prefix_ void senf::console::Client::v_write(senf::log::time_type timestamp, reader_->enablePrompt(); } -prefix_ std::ostream & senf::console::operator<<(std::ostream & os, Client const & client) +prefix_ unsigned senf::console::Client::getWidth(std::ostream & os, unsigned defaultWidth, + unsigned minWidth) { - typedef ClientSocketHandle< MakeSocketPolicy< - INet4AddressingPolicy,ConnectedCommunicationPolicy>::policy > V4Socket; - typedef ClientSocketHandle< MakeSocketPolicy< - INet6AddressingPolicy,ConnectedCommunicationPolicy>::policy > V6Socket; - - try { - if (check_socket_cast(client.handle())) - os << dynamic_socket_cast(client.handle()).peer(); - else if (check_socket_cast(client.handle())) - os << dynamic_socket_cast(client.handle()).peer(); - else - os << static_cast(&client); - } - catch (SystemException &) { - os << "0.0.0.0:0"; + unsigned rv (defaultWidth); + try { + rv = get(os).width(); } - - return os; + catch (std::bad_cast &) {} + return rv < minWidth ? defaultWidth : rv; } -prefix_ std::ostream & senf::console::operator<<(std::ostream & os, Client * client) +/////////////////////////////////////////////////////////////////////////// +// senf::console::Client::SysBacktrace + +prefix_ senf::console::Client::SysBacktrace::SysBacktrace() { - return os << *client; + sysdir().node().add("backtrace", &SysBacktrace::backtrace) + .doc("Display the backtrace of the last error / exception in this console"); } +prefix_ void senf::console::Client::SysBacktrace::backtrace(std::ostream & os) +{ + Client & client (Client::get(os)); + if (client.backtrace().empty()) + os << "(no backtrace)\n"; + else + os << client.backtrace(); +} + +senf::console::Client::SysBacktrace senf::console::Client::SysBacktrace::instance_; + ///////////////////////////////cc.e//////////////////////////////////////// #undef prefix_ //#include "Server.mpp"