From a30083c01a93283be90ea9104c0e6e2e183f4195 Mon Sep 17 00:00:00 2001 From: Simon Ninon Date: Sun, 11 Dec 2016 23:18:45 +0100 Subject: [PATCH] make __TACOPIE_THROW calls __TACOPIE_LOG --- includes/tacopie/error.hpp | 5 ++- sources/network/io_service.cpp | 6 ++-- sources/network/tcp_client.cpp | 18 ++++------ sources/network/tcp_server.cpp | 6 ++-- sources/network/tcp_socket.cpp | 66 ++++++++++++---------------------- 5 files changed, 36 insertions(+), 65 deletions(-) diff --git a/includes/tacopie/error.hpp b/includes/tacopie/error.hpp index 63cb526..3d09d3b 100644 --- a/includes/tacopie/error.hpp +++ b/includes/tacopie/error.hpp @@ -30,4 +30,7 @@ class tacopie_error : public std::runtime_error { } //! tacopie //! macro for convenience -#define __TACOPIE_THROW(what) throw tacopie::tacopie_error((what), __FILE__, __LINE__); +#define __TACOPIE_THROW(level, what) { \ + __TACOPIE_LOG(level, (what)); \ + throw tacopie::tacopie_error((what), __FILE__, __LINE__); \ + } diff --git a/sources/network/io_service.cpp b/sources/network/io_service.cpp index 2195bc7..da7885a 100644 --- a/sources/network/io_service.cpp +++ b/sources/network/io_service.cpp @@ -43,10 +43,8 @@ io_service::io_service(void) { __TACOPIE_LOG(debug, "create io_service"); - if (pipe(m_notif_pipe_fds) == -1) { - __TACOPIE_LOG(error, "pipe() failure"); - __TACOPIE_THROW("io_service::io_service: pipe() failure"); - } + if (pipe(m_notif_pipe_fds) == -1) + { __TACOPIE_THROW(error, "pipe() failure"); } //! Start worker after everything has been initialized m_poll_worker = std::thread(std::bind(&io_service::poll, this)); diff --git a/sources/network/tcp_client.cpp b/sources/network/tcp_client.cpp index 53b84c5..4a074d8 100644 --- a/sources/network/tcp_client.cpp +++ b/sources/network/tcp_client.cpp @@ -40,10 +40,8 @@ tcp_client::tcp_client(const tcp_socket& socket) void tcp_client::connect(const std::string& host, std::uint32_t port) { - if (is_connected()) { - __TACOPIE_LOG(warn, "tcp_client is already connected"); - __TACOPIE_THROW("tcp_client::connect: tcp_client is already connected"); - } + if (is_connected()) + { __TACOPIE_THROW(warn, "tcp_client is already connected"); } m_socket.connect(host, port); m_io_service->track(m_socket); @@ -185,10 +183,8 @@ tcp_client::process_write(write_result& result) { void tcp_client::async_read(const read_request& request) { - if (not is_connected()) { - __TACOPIE_LOG(warn, "tcp_client is not connected"); - __TACOPIE_THROW("tcp_client::async_read: tcp_client is disconnected"); - } + if (not is_connected()) + { __TACOPIE_THROW(warn, "tcp_client is disconnected"); } std::lock_guard lock(m_read_requests_mtx); @@ -200,10 +196,8 @@ tcp_client::async_read(const read_request& request) { void tcp_client::async_write(const write_request& request) { - if (not is_connected()) { - __TACOPIE_LOG(warn, "tcp_client is not connected"); - __TACOPIE_THROW("tcp_client::async_write: tcp_client is disconnected"); - } + if (not is_connected()) + { __TACOPIE_THROW(warn, "tcp_client is disconnected"); } std::lock_guard lock(m_write_requests_mtx); diff --git a/sources/network/tcp_server.cpp b/sources/network/tcp_server.cpp index b737922..8fa1e71 100644 --- a/sources/network/tcp_server.cpp +++ b/sources/network/tcp_server.cpp @@ -27,10 +27,8 @@ tcp_server::~tcp_server(void) { void tcp_server::start(const std::string& host, std::uint32_t port, const on_new_connection_callback_t& callback) { - if (is_running()) { - __TACOPIE_LOG(warn, "tcp_server is already running"); - __TACOPIE_THROW("tcp_server::start: tcp_server is already running"); - } + if (is_running()) + { __TACOPIE_THROW(warn, "tcp_server is already running"); } m_socket.bind(host, port); m_socket.listen(__TACOPIE_CONNECTION_QUEUE_SIZE); diff --git a/sources/network/tcp_socket.cpp b/sources/network/tcp_socket.cpp index 9d09447..edffa95 100644 --- a/sources/network/tcp_socket.cpp +++ b/sources/network/tcp_socket.cpp @@ -47,15 +47,11 @@ tcp_socket::recv(std::size_t size_to_read) { ssize_t rd_size = ::recv(m_fd, const_cast(data.data()), size_to_read, 0); - if (rd_size == -1) { - __TACOPIE_LOG(error, "create recv() failure"); - __TACOPIE_THROW("tcp_socket::recv: recv() failure"); - } + if (rd_size == -1) + { __TACOPIE_THROW(error, "recv() failure"); } - if (rd_size == 0) { - __TACOPIE_LOG(warn, "socket closed by remote host"); - __TACOPIE_THROW("tcp_socket::recv: nothing to read, socket has been closed by remote host"); - } + if (rd_size == 0) + { __TACOPIE_THROW(warn, "nothing to read, socket has been closed by remote host"); } return data; } @@ -67,10 +63,8 @@ tcp_socket::send(const std::vector& data, std::size_t size_to_write) { ssize_t wr_size = ::send(m_fd, data.data(), size_to_write, 0); - if (wr_size == -1) { - __TACOPIE_LOG(error, "send failure"); - __TACOPIE_THROW("tcp_socket::send: send() failure"); - } + if (wr_size == -1) + { __TACOPIE_THROW(error, "send() failure"); } return wr_size; } @@ -82,10 +76,8 @@ tcp_socket::connect(const std::string& host, std::uint32_t port) { struct hostent* addr = gethostbyname(host.c_str()); - if (not addr) { - __TACOPIE_LOG(error, "gethostbyname() failure"); - __TACOPIE_THROW("tcp_socket::bind: gethostbyname() failure"); - } + if (not addr) + { __TACOPIE_THROW(error, "gethostbyname() failure"); } struct sockaddr_in server_addr; std::memset(&server_addr, 0, sizeof(server_addr)); @@ -93,10 +85,8 @@ tcp_socket::connect(const std::string& host, std::uint32_t port) { server_addr.sin_port = htons(port); server_addr.sin_family = AF_INET; - if (::connect(m_fd, (const struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) { - __TACOPIE_LOG(error, "connect() failure"); - __TACOPIE_THROW("tcp_socket::bind: connect() failure"); - } + if (::connect(m_fd, (const struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) + { __TACOPIE_THROW(error, "connect() failure"); } } //! @@ -110,10 +100,8 @@ tcp_socket::bind(const std::string& host, std::uint32_t port) { struct hostent* addr = gethostbyname(host.c_str()); - if (not addr) { - __TACOPIE_LOG(error, "gethostbyname() failure"); - __TACOPIE_THROW("tcp_socket::bind: gethostbyname() failure"); - } + if (not addr) + { __TACOPIE_THROW(error, "gethostbyname() failure"); } struct sockaddr_in server_addr; std::memset(&server_addr, 0, sizeof(server_addr)); @@ -121,10 +109,8 @@ tcp_socket::bind(const std::string& host, std::uint32_t port) { server_addr.sin_port = htons(port); server_addr.sin_family = AF_INET; - if (::bind(m_fd, (const struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) { - __TACOPIE_LOG(error, "bind() failure"); - __TACOPIE_THROW("tcp_socket::bind: bind() failure"); - } + if (::bind(m_fd, (const struct sockaddr *)&server_addr, sizeof(server_addr)) == -1) + { __TACOPIE_THROW(error, "bind() failure"); } } void @@ -132,10 +118,8 @@ tcp_socket::listen(std::size_t max_connection_queue) { create_socket_if_necessary(); check_or_set_type(type::SERVER); - if (::listen(m_fd, max_connection_queue) == -1) { - __TACOPIE_LOG(debug, "listen() failure"); - __TACOPIE_THROW("tcp_socket::listen: listen() failure"); - } + if (::listen(m_fd, max_connection_queue) == -1) + { __TACOPIE_THROW(debug, "listen() failure"); } } tcp_socket @@ -148,10 +132,8 @@ tcp_socket::accept(void) { fd_t client_fd = ::accept(m_fd, (struct sockaddr *)&client_info, &client_info_struct_size); - if (client_fd == -1) { - __TACOPIE_LOG(error, "accept failure"); - __TACOPIE_THROW("tcp_socket::accept: accept() failure"); - } + if (client_fd == -1) + { __TACOPIE_THROW(error, "accept() failure"); } //! TODO: init with real client addr return { client_fd, "", client_info.sin_port, type::CLIENT }; @@ -185,10 +167,8 @@ tcp_socket::create_socket_if_necessary(void) { m_fd = socket(AF_INET, SOCK_STREAM, 0); m_type = type::UNKNOWN; - if (m_fd == -1) { - __TACOPIE_LOG(error, "socket() failure"); - __TACOPIE_THROW("tcp_socket::create_socket_if_necessary: socket() failure"); - } + if (m_fd == -1) + { __TACOPIE_THROW(error, "tcp_socket::create_socket_if_necessary: socket() failure"); } } //! @@ -198,10 +178,8 @@ tcp_socket::create_socket_if_necessary(void) { void tcp_socket::check_or_set_type(type t) { - if (m_type != type::UNKNOWN and m_type != t) { - __TACOPIE_LOG(error, "trying to perform invalid operation on socket"); - __TACOPIE_THROW("tcp_socket::check_or_set_type: trying to perform invalid operation on socket"); - } + if (m_type != type::UNKNOWN and m_type != t) + { __TACOPIE_THROW(error, "trying to perform invalid operation on socket"); } m_type = t; }