Put echo peer logging on toggle, default to off

This commit is contained in:
Vinnie Falco
2016-05-04 11:06:02 -04:00
parent 4bbf393e38
commit aea2f939d7
2 changed files with 30 additions and 21 deletions

View File

@@ -43,6 +43,7 @@ public:
using socket_type = boost::asio::ip::tcp::socket; using socket_type = boost::asio::ip::tcp::socket;
private: private:
bool log_ = false;
boost::asio::io_service ios_; boost::asio::io_service ios_;
socket_type sock_; socket_type sock_;
boost::asio::ip::tcp::acceptor acceptor_; boost::asio::ip::tcp::acceptor acceptor_;
@@ -72,7 +73,7 @@ public:
} }
else else
{ {
Peer{std::move(sock_), ep}; Peer{log_, std::move(sock_), ep};
} }
thread_.reserve(threads); thread_.reserve(threads);
for(std::size_t i = 0; i < threads; ++i) for(std::size_t i = 0; i < threads; ++i)
@@ -100,6 +101,7 @@ private:
{ {
struct data struct data
{ {
bool log;
int state = 0; int state = 0;
boost::optional<endpoint_type> ep; boost::optional<endpoint_type> ep;
websocket::stream<socket_type> ws; websocket::stream<socket_type> ws;
@@ -107,8 +109,9 @@ private:
beast::streambuf sb; beast::streambuf sb;
int id; int id;
data(socket_type&& sock_) data(bool log_, socket_type&& sock_)
: ws(std::move(sock_)) : log(log_)
, ws(std::move(sock_))
, id([] , id([]
{ {
static int n = 0; static int n = 0;
@@ -117,9 +120,10 @@ private:
{ {
} }
data(socket_type&& sock_, data(bool log_, socket_type&& sock_,
endpoint_type const& ep_) endpoint_type const& ep_)
: ep(ep_) : log(log_)
, ep(ep_)
, ws(std::move(sock_)) , ws(std::move(sock_))
, id([] , id([]
{ {
@@ -157,8 +161,8 @@ private:
template<class... Args> template<class... Args>
explicit explicit
Peer(socket_type&& sock, Args&&... args) Peer(bool log, socket_type&& sock, Args&&... args)
: d_(std::make_shared<data>( : d_(std::make_shared<data>(log,
std::forward<socket_type>(sock), std::forward<socket_type>(sock),
std::forward<Args>(args)...)) std::forward<Args>(args)...))
{ {
@@ -186,7 +190,7 @@ private:
void operator()(error_code ec) void operator()(error_code ec)
{ {
auto& d = *d_; auto& d = *d_;
switch(d_->state) switch(d.state)
{ {
// did accept // did accept
case 0: case 0:
@@ -232,17 +236,22 @@ private:
void void
fail(error_code ec, std::string what) fail(error_code ec, std::string what)
{ {
if(ec != websocket::error::closed) auto& d = *d_;
std::cerr << "#" << d_->id << " " << if(d.log)
what << ": " << ec.message() << std::endl; {
if(ec != websocket::error::closed)
std::cerr << "#" << d_->id << " " <<
what << ": " << ec.message() << std::endl;
}
} }
}; };
void void
fail(error_code ec, std::string what) fail(error_code ec, std::string what)
{ {
std::cerr << if(log_)
what << ": " << ec.message() << std::endl; std::cerr << what << ": " <<
ec.message() << std::endl;
} }
void void
@@ -265,7 +274,7 @@ private:
acceptor_.async_accept(sock_, acceptor_.async_accept(sock_,
std::bind(&async_echo_peer::on_accept, this, std::bind(&async_echo_peer::on_accept, this,
beast::asio::placeholders::error)); beast::asio::placeholders::error));
Peer{std::move(sock)}; Peer{false, std::move(sock)};
} }
}; };

View File

@@ -42,6 +42,7 @@ public:
using socket_type = boost::asio::ip::tcp::socket; using socket_type = boost::asio::ip::tcp::socket;
private: private:
bool log_ = false;
boost::asio::io_service ios_; boost::asio::io_service ios_;
socket_type sock_; socket_type sock_;
boost::asio::ip::tcp::acceptor acceptor_; boost::asio::ip::tcp::acceptor acceptor_;
@@ -83,23 +84,22 @@ public:
} }
private: private:
static
void void
fail(error_code ec, std::string what) fail(error_code ec, std::string what)
{ {
std::cerr << if(log_)
what << ": " << ec.message() << std::endl; std::cerr <<
what << ": " << ec.message() << std::endl;
} }
static
void void
fail(int id, error_code ec, std::string what) fail(int id, error_code ec, std::string what)
{ {
std::cerr << "#" << std::to_string(id) << " " << if(log_)
what << ": " << ec.message() << std::endl; std::cerr << "#" << std::to_string(id) << " " <<
what << ": " << ec.message() << std::endl;
} }
static
void void
maybe_throw(error_code ec, std::string what) maybe_throw(error_code ec, std::string what)
{ {