Skip to content

Commit

Permalink
add easylog (#163)
Browse files Browse the repository at this point in the history
* add easylog

* add flush method

* ignore cur thd id on win

* support sprintf
* improve ELOGV

* use new log in client

* fix meta string

* fix win; simplify log function

* update server log

* replace log

* remove spdlog

* fix

* namespace

* use memory resource

* fix std::max/std::min for win

* simplify ELOG
  • Loading branch information
qicosmos authored Feb 2, 2023
1 parent dac0ad9 commit c79257f
Show file tree
Hide file tree
Showing 141 changed files with 904 additions and 26,193 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,12 @@ options:
## Dependencies

- [doctest](https://github.com/doctest/doctest)
- [spdlog](https://github.com/gabime/spdlog)
- [asio](https://github.com/chriskohlhoff/asio)
- openssl (optional)
- [async_simple](https://github.com/alibaba/async_simple)
- [iguana](https://github.com/qicosmos/iguana)

Currently, asio and spdlog are put in thirdparty folder.
Currently, asio and frozen are put in thirdparty folder.
doctest is put in tests folder.

# How to generate document
Expand Down
1 change: 0 additions & 1 deletion cmake/dependency.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ option(USE_CONAN "Use conan package manager" OFF)
if(USE_CONAN)
message(STATUS "Use conan package manager")
find_package(asio REQUIRED)
find_package(spdlog REQUIRED)
else()
add_subdirectory(${yaLanTingLibs_SOURCE_DIR}/thirdparty)
endif()
3 changes: 2 additions & 1 deletion cmake/module.cmake
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# module-list
set(yLT_ALL_PROJECTS "struct_pack;struct_pb;struct_json;coro_rpc")
set(yLT_ALL_PROJECTS "struct_pack;struct_pb;struct_json;coro_rpc;easylog")
# separate
option(Build_ylt_struct_pack "Build struct_pack" ON)
option(Build_ylt_struct_pb "Build struct_pb" ON)
option(Build_ylt_struct_json "Build struct_json" ON)
option(Build_ylt_coro_rpc "Build coro_rpc" ON)
option(Build_ylt_easylog "Build easylog" ON)

foreach (proj ${yLT_ALL_PROJECTS})
set(build_proj "Build_ylt_${proj}")
Expand Down
1 change: 0 additions & 1 deletion conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class CoroRpcConan(ConanFile):

def requirements(self):
self.requires("asio/1.23.0")
self.requires("spdlog/1.10.0")

def config_options(self):
if self.settings.os == "Windows":
Expand Down
103 changes: 49 additions & 54 deletions include/coro_rpc/coro_rpc/async_connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <vector>

#include "asio_util/asio_coro_util.hpp"
#include "logging/easylog.hpp"
#include "logging/easylog.h"
#include "router.hpp"
#include "router_impl.hpp"
#include "rpc_protocol.h"
Expand Down Expand Up @@ -87,25 +87,25 @@ class async_connection : public std::enable_shared_from_this<async_connection> {

#ifdef ENABLE_SSL
void async_handshake() {
easylog::info("begin to handshake");
ELOGV(INFO, "begin to handshake");
reset_timer();
auto self = this->shared_from_this();
auto callback = [this, self](const asio::error_code &error) {
cancel_timer();
if (error) {
easylog::error("handshake failed:{}", error.message());
ELOGV(ERROR, "handshake failed: %s", error.message().data());
close();
return;
}
easylog::info("handshake ok");
ELOGV(INFO, "handshake ok");
read_head();
};
ssl_stream_->async_handshake(asio::ssl::stream_base::server, callback);
}
#endif
~async_connection() {
#ifdef UNIT_TEST_INJECT
easylog::info("~async_connection client_id {}", client_id_);
ELOGV(INFO, "~async_connection client_id %d", client_id_);
#endif
cancel_timer();
sync_close();
Expand All @@ -114,7 +114,7 @@ class async_connection : public std::enable_shared_from_this<async_connection> {
void quit() {
cancel_timer();
#ifdef UNIT_TEST_INJECT
easylog::info("quit client_id {}", client_id_);
ELOGV(INFO, "quit client_id %d", client_id_);
#endif
close();
quit_promise_.get_future().wait();
Expand All @@ -123,7 +123,7 @@ class async_connection : public std::enable_shared_from_this<async_connection> {
template <typename R>
void response_msg(const R &ret) {
if (has_closed()) [[unlikely]] {
easylog::info("response_msg failed: connection has been closed");
ELOGV(INFO, "response_msg failed: connection has been closed");
return;
}

Expand All @@ -134,7 +134,7 @@ class async_connection : public std::enable_shared_from_this<async_connection> {

io_context_.post([this, buf = std::move(buf), self = shared_from_this()] {
if (has_closed()) [[unlikely]] {
easylog::info("response_msg failed: connection has been closed");
ELOGV(INFO, "response_msg failed: connection has been closed");
return;
}
write(std::move(buf));
Expand All @@ -155,18 +155,6 @@ class async_connection : public std::enable_shared_from_this<async_connection> {
std::any get_tag() { return tag_; }

private:
void log(std::errc err, const std::string err_prefix = "",
source_location loc = {}) {
#ifdef UNIT_TEST_INJECT
easylog::info(loc, "{} {} client_id {} write_queue size {}", err_prefix,
std::make_error_code(err).message(), client_id_,
write_queue_.size());
#else
easylog::info(loc, "{} {}", err_prefix,
std::make_error_code(err).message());
#endif
}

void read_head() {
async_read_head([this, self = shared_from_this()](asio::error_code ec,
std::size_t length) {
Expand All @@ -175,24 +163,30 @@ class async_connection : public std::enable_shared_from_this<async_connection> {

auto errc = struct_pack::deserialize_to(header_, head_, RPC_HEAD_LEN);
if (errc != struct_pack::errc::ok) [[unlikely]] {
log(std::errc::protocol_error, "bad head");
ELOGV(
ERROR, "%s, %s",
std::make_error_code(std::errc::protocol_error).message().data(),
"deserialize error");
close();
return;
}

#ifdef UNIT_TEST_INJECT
client_id_ = header_.seq_num;
easylog::info("client_id {}", client_id_);
ELOGV(INFO, "client_id %d", client_id_);
#endif

if (header_.magic != magic_number) [[unlikely]] {
easylog::error("bad magic number");
ELOGV(ERROR, "bad magic number");
close();
return;
}

if (header_.length == 0) [[unlikely]] {
log(std::errc::protocol_error, "bad body length");
ELOGV(
ERROR, "%s, %s",
std::make_error_code(std::errc::protocol_error).message().data(),
"bad body length");
close();
return;
}
Expand All @@ -204,7 +198,7 @@ class async_connection : public std::enable_shared_from_this<async_connection> {
read_body(header_.length);
}
else {
log((std::errc)ec.value());
ELOGV(ERROR, "%s, %s", ec.message().data(), "read head error");
close();
}
});
Expand All @@ -220,7 +214,7 @@ class async_connection : public std::enable_shared_from_this<async_connection> {
handle_body(length, self);
}
else {
log((std::errc)ec.value());
ELOGV(ERROR, "%s, %s", ec.message().data(), "read body error");
close(false);
}
});
Expand Down Expand Up @@ -262,38 +256,39 @@ class async_connection : public std::enable_shared_from_this<async_connection> {
auto &msg = write_queue_.front();
#ifdef UNIT_TEST_INJECT
if (g_action == inject_action::force_inject_connection_close_socket) {
easylog::warn(
"inject action: force_inject_connection_close_socket, "
"client_id {}",
client_id_);
ELOGV(WARN,
"inject action: force_inject_connection_close_socket, "
"client_id %d",
client_id_);

asio::error_code ignored_ec;
socket_.shutdown(asio::ip::tcp::socket::shutdown_both, ignored_ec);
socket_.close(ignored_ec);
}
#endif
async_write(asio::buffer(msg),
[this, self = shared_from_this()](asio::error_code ec,
std::size_t length) {
if (ec) {
log((std::errc)ec.value());
close(false);
}
else {
write_queue_.pop_front();

if (!write_queue_.empty()) {
write();
}
else {
if (rsp_err_ != err_ok) [[unlikely]] {
log(rsp_err_);
close(false);
return;
}
}
}
});
async_write(asio::buffer(msg), [this, self = shared_from_this()](
asio::error_code ec,
std::size_t length) {
if (ec) {
ELOGV(ERROR, "%s, %s", ec.message().data(), "async_write error")
close(false);
}
else {
write_queue_.pop_front();

if (!write_queue_.empty()) {
write();
}
else {
if (rsp_err_ != err_ok) [[unlikely]] {
ELOGV(ERROR, "%s, %s",
std::make_error_code(rsp_err_).message().data(), "rsp_err_")
close(false);
return;
}
}
}
});
}

template <typename Handler>
Expand Down Expand Up @@ -395,9 +390,9 @@ class async_connection : public std::enable_shared_from_this<async_connection> {
}

#ifdef UNIT_TEST_INJECT
easylog::info("close timeout client_id {}", client_id_);
ELOGV(INFO, "close timeout client_id %d", client_id_);
#else
easylog::info("close timeout client");
ELOGV(INFO, "close timeout client");
#endif

close(false);
Expand Down
29 changes: 15 additions & 14 deletions include/coro_rpc/coro_rpc/async_rpc_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ class async_rpc_server {
std::unique_lock lock(start_mtx_);
if (flag_ != stat::init) {
if (flag_ == stat::started) {
easylog::info("start again");
ELOGV(INFO, "start again");
}
else if (flag_ == stat::started) {
easylog::info("has stoped");
else if (flag_ == stat::stop) {
ELOGV(INFO, "has stoped");
}
return std::errc::io_error;
}
Expand Down Expand Up @@ -118,10 +118,10 @@ class async_rpc_server {
std::unique_lock lock(start_mtx_);
if (flag_ != stat::init) {
if (flag_ == stat::started) {
easylog::info("start again");
ELOGV(INFO, "start again");
}
else if (flag_ == stat::started) {
easylog::info("has stoped");
else if (flag_ == stat::stop) {
ELOGV(INFO, "has stoped");
}
return std::errc::io_error;
}
Expand Down Expand Up @@ -157,7 +157,7 @@ class async_rpc_server {
}
return;
}
easylog::info("begin to stop async_rpc_server");
ELOGV(INFO, "begin to stop async_rpc_server");
close_acceptor();
if (flag_ == stat::started) {
// notify connection quit all async event
Expand All @@ -172,7 +172,7 @@ class async_rpc_server {
thd_.join();
}

easylog::info("stop async_rpc_server ok");
ELOGV(INFO, "stop async_rpc_server ok");
flag_ = stat::stop;
}

Expand Down Expand Up @@ -272,15 +272,15 @@ class async_rpc_server {
acceptor_.async_accept(
conn->socket(), [this, conn](asio::error_code ec) mutable {
if (ec) {
easylog::info("acceptor error: {}", ec.message());
ELOGV(INFO, "acceptor error: %s", ec.message().data());
if (ec == asio::error::operation_aborted) {
promise_.value().set_value();
return;
}
}
else {
int64_t conn_id = ++conn_id_;
easylog::info("new client conn_id {} coming", conn_id);
ELOGV(INFO, "new client conn_id %d coming", conn_id);
conns_.emplace(conn_id, conn);
conn->start();
}
Expand All @@ -300,7 +300,8 @@ class async_rpc_server {
asio::error_code ec;
acceptor_.bind(endpoint, ec);
if (ec) {
easylog::error("bind port {} error : {}", port_, ec.message());
ELOGV(ERROR, "bind port %d error : %s", port_.load(),
ec.message().data());
acceptor_.cancel(ec);
acceptor_.close(ec);
return std::errc::address_in_use;
Expand All @@ -312,13 +313,13 @@ class async_rpc_server {

auto end_point = acceptor_.local_endpoint(ec);
if (ec) {
easylog::error("get local endpoint port {} error {}", port_,
ec.message());
ELOGV(ERROR, "get local endpoint port %d error %s", port_.load(),
ec.message().data());
return std::errc::address_in_use;
}
port_ = end_point.port();

easylog::info("listen port {} successfully", port_);
ELOGV(INFO, "listen port %d successfully", port_.load());
return std::errc{};
}

Expand Down
Loading

0 comments on commit c79257f

Please sign in to comment.