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