From ff6da2c6145238f31e7f014cd7b8cb74c376dd1d Mon Sep 17 00:00:00 2001 From: "Zezheng.Li" Date: Thu, 20 Jul 2023 16:35:45 +0800 Subject: [PATCH] [coro_http] [easylog] use easylog instead of std::cout --- include/ylt/coro_http/coro_http_client.hpp | 8 +++ include/ylt/easylog.hpp | 11 ++-- include/ylt/easylog/record.hpp | 4 +- .../cinatra/cinatra_log_wrapper.hpp | 56 +++++++++++++++++ .../thirdparty/cinatra/coro_http_client.hpp | 61 +++++++------------ src/coro_http/examples/channel.cpp | 11 ++-- 6 files changed, 99 insertions(+), 52 deletions(-) create mode 100644 include/ylt/thirdparty/cinatra/cinatra_log_wrapper.hpp diff --git a/include/ylt/coro_http/coro_http_client.hpp b/include/ylt/coro_http/coro_http_client.hpp index 5cb86967e4..ac30324d8b 100644 --- a/include/ylt/coro_http/coro_http_client.hpp +++ b/include/ylt/coro_http/coro_http_client.hpp @@ -17,6 +17,14 @@ #ifdef YLT_ENABLE_SSL #define CINATRA_ENABLE_SSL #endif +#include +#define CINATRA_LOG_ENDL +#define CINATRA_LOG_ERROR ELOG_ERROR +#define CINATRA_LOG_WARNING ELOG_WARN +#define CINATRA_LOG_INFO ELOG_INFO +#define CINATRA_LOG_DEBUG ELOG_DEBUG +#define CINATRA_LOG_TRACE ELOG_TRACE + #include namespace coro_http { diff --git a/include/ylt/easylog.hpp b/include/ylt/easylog.hpp index 5d1f819f88..e3d4316a29 100644 --- a/include/ylt/easylog.hpp +++ b/include/ylt/easylog.hpp @@ -138,7 +138,8 @@ inline void add_appender(std::function fn) { GET_STRING(__FILE__, __LINE__)) \ .ref() -#define ELOG(severity, ...) ELOG_IMPL(Severity::severity, __VA_ARGS__, 0) +#define ELOG(severity, ...) \ + ELOG_IMPL(easylog::Severity::severity, __VA_ARGS__, 0) #define ELOGV_IMPL(severity, Id, fmt, ...) \ if (!easylog::logger::instance().check_severity(severity)) { \ @@ -149,19 +150,19 @@ inline void add_appender(std::function fn) { easylog::record_t(std::chrono::system_clock::now(), severity, \ GET_STRING(__FILE__, __LINE__)) \ .sprintf(fmt, __VA_ARGS__); \ - if (severity == Severity::CRITICAL) { \ + if (severity == easylog::Severity::CRITICAL) { \ easylog::flush(); \ std::exit(EXIT_FAILURE); \ } \ } #define ELOGV(severity, ...) \ - ELOGV_IMPL(Severity::severity, 0, __VA_ARGS__, "\n") + ELOGV_IMPL(easylog::Severity::severity, 0, __VA_ARGS__, "\n") #define MELOGV(severity, Id, ...) \ - ELOGV_IMPL(Severity::severity, Id, __VA_ARGS__, "\n") + ELOGV_IMPL(easylog::Severity::severity, Id, __VA_ARGS__, "\n") -#define ELOG_TRACE ELOG(INFO) +#define ELOG_TRACE ELOG(TRACE) #define ELOG_DEBUG ELOG(DEBUG) #define ELOG_INFO ELOG(INFO) #define ELOG_WARN ELOG(WARN) diff --git a/include/ylt/easylog/record.hpp b/include/ylt/easylog/record.hpp index ed477c3654..4c4f2be930 100644 --- a/include/ylt/easylog/record.hpp +++ b/include/ylt/easylog/record.hpp @@ -50,6 +50,8 @@ enum { ERROR = 0 }; #endif #endif +namespace easylog { + enum class Severity { NONE = 0, TRACE = 1, @@ -60,8 +62,6 @@ enum class Severity { CRITICAL = 6, }; -namespace easylog { - inline std::string_view severity_str(Severity severity) { switch (severity) { case Severity::TRACE: diff --git a/include/ylt/thirdparty/cinatra/cinatra_log_wrapper.hpp b/include/ylt/thirdparty/cinatra/cinatra_log_wrapper.hpp new file mode 100644 index 0000000000..f3d01e2041 --- /dev/null +++ b/include/ylt/thirdparty/cinatra/cinatra_log_wrapper.hpp @@ -0,0 +1,56 @@ +namespace cinatra { +struct null_logger_t { + template + const null_logger_t& operator<<(T&&) const { + return *this; + } +}; +} // namespace cinatra + +constexpr inline cinatra::null_logger_t NULL_LOGGER; + +#ifdef CINATRA_LOG_ENDL +#else +#define CINATRA_LOG_ENDL << std::endl +#endif + +#ifdef CINATRA_LOG_ERROR +#else +#define CINATRA_LOG_ERROR std::cerr +#endif + +#ifdef CINATRA_LOG_WARNING +#else +#ifndef NDEBUG +#define CINATRA_LOG_WARNING std::cerr +#else +#define CINATRA_LOG_WARNING NULL_LOGGER +#endif +#endif + +#ifdef CINATRA_LOG_INFO +#else +#ifndef NDEBUG +#define CINATRA_LOG_INFO std::cout +#else +#define CINATRA_LOG_INFO NULL_LOGGER +#endif +#endif + +#ifdef CINATRA_LOG_DEBUG +#else +#ifndef NDEBUG +#define CINATRA_LOG_DEBUG std::cout +#else +#define CINATRA_LOG_DEBUG NULL_LOGGER +#endif +#endif + +#ifdef CINATRA_LOG_TRACE +#else +#ifndef NDEBUG +#define CINATRA_LOG_TRACE std::cout +#else +#define CINATRA_LOG_TRACE NULL_LOGGER +#endif +#endif diff --git a/include/ylt/thirdparty/cinatra/coro_http_client.hpp b/include/ylt/thirdparty/cinatra/coro_http_client.hpp index 5aabcf6fab..20a66dd5c8 100644 --- a/include/ylt/thirdparty/cinatra/coro_http_client.hpp +++ b/include/ylt/thirdparty/cinatra/coro_http_client.hpp @@ -20,6 +20,7 @@ #include "async_simple/Unit.h" #include "async_simple/coro/FutureAwaiter.h" #include "async_simple/coro/Lazy.h" +#include "cinatra_log_wrapper.hpp" #include "http_parser.hpp" #include "response_cv.hpp" #include "uri.hpp" @@ -270,7 +271,7 @@ class coro_http_client { use_ssl_ = true; ssl_init_ret_ = true; } catch (std::exception &e) { - std::cout << "init ssl failed: " << e.what() << "\n"; + CINATRA_LOG_ERROR << "init ssl failed: " << e.what() CINATRA_LOG_ENDL; } return ssl_init_ret_; } @@ -346,9 +347,7 @@ class coro_http_client { resp_data data{}; auto [r, u] = handle_uri(data, uri); if (!r) { -#ifndef NDEBUG - std::cout << "url error"; -#endif + CINATRA_LOG_WARNING << "url error:" CINATRA_LOG_ENDL; co_return false; } @@ -466,9 +465,8 @@ class coro_http_client { handle_result(data, ec, is_keep_alive); if (ec) { if (!stop_bench_) -#ifndef NDEBUG - std::cout << "do_bench_read error:" << ec.message() << "\n"; -#endif + CINATRA_LOG_ERROR << "do_bench_read error:" + << ec.message() CINATRA_LOG_ENDL; data.net_err = ec; data.status = 404; } @@ -484,9 +482,8 @@ class coro_http_client { if (ec) { if (!stop_bench_) -#ifndef NDEBUG - std::cout << "do_bench_read error:" << ec.message() << "\n"; -#endif + CINATRA_LOG_ERROR << "do_bench_read error:" + << ec.message() CINATRA_LOG_ENDL; data.net_err = ec; data.status = 404; close_socket(*socket_); @@ -566,22 +563,19 @@ class coro_http_client { bool add_file_part(std::string name, std::string filename) { if (form_data_.find(name) != form_data_.end()) { -#ifndef NDEBUG - std::cout << "name already exist\n"; -#endif + CINATRA_LOG_WARNING << "name already exist: " << name CINATRA_LOG_ENDL; return false; } std::error_code ec; bool r = std::filesystem::exists(filename, ec); if (!r || ec) { -#ifndef NDEBUG if (ec) { - std::cout << ec.message() << "\n"; + CINATRA_LOG_WARNING << ec.message() CINATRA_LOG_ENDL; } - std::cout << "file not exists, " - << std::filesystem::current_path().string() << std::endl; -#endif + CINATRA_LOG_WARNING << "file not exists, " + << std::filesystem::current_path().string() + CINATRA_LOG_ENDL; return false; } @@ -632,9 +626,7 @@ class coro_http_client { form_data_.clear(); }); if (form_data_.empty()) { -#ifndef NDEBUG - std::cout << "no multipart\n"; -#endif + CINATRA_LOG_WARNING << "no multipart" CINATRA_LOG_ENDL; co_return resp_data{{}, 404}; } @@ -675,9 +667,7 @@ class coro_http_client { #ifdef INJECT_FOR_HTTP_CLIENT_TEST inject_write_failed = ClientInjectAction::none; #endif -#ifndef NDEBUG - std::cout << ec.message() << "\n"; -#endif + CINATRA_LOG_DEBUG << ec.message() CINATRA_LOG_ENDL; co_return resp_data{ec, 404}; } @@ -710,9 +700,8 @@ class coro_http_client { async_simple::coro::Lazy async_upload_multipart( std::string uri, std::string name, std::string filename) { if (!add_file_part(std::move(name), std::move(filename))) { -#ifndef NDEBUG - std::cout << "open file failed or duplicate test names\n"; -#endif + CINATRA_LOG_WARNING + << "open file failed or duplicate test names" CINATRA_LOG_ENDL; co_return resp_data{{}, 404}; } co_return co_await async_upload_multipart(std::move(uri)); @@ -920,7 +909,7 @@ class coro_http_client { req_str_ = req_head_str; #endif #ifdef CORO_HTTP_PRINT_REQ_HEAD - std::cout << req_head_str << "\n"; + CINATRA_LOG_DEBUG << req_head_str CINATRA_LOG_ENDL; #endif auto future = start_timer(req_timeout_duration_, "request timer"); if (has_body) { @@ -954,9 +943,7 @@ class coro_http_client { auto ec = co_await coro_io::async_handshake( ssl_stream_, asio::ssl::stream_base::client); if (ec) { -#ifndef NDEBUG - std::cout << "handle failed " << ec.message() << "\n"; -#endif + CINATRA_LOG_ERROR << "handle failed " << ec.message() CINATRA_LOG_ENDL; } co_return ec; } @@ -1302,9 +1289,7 @@ class coro_http_client { data.status = 404; #ifdef BENCHMARK_TEST if (!stop_bench_) { -#ifndef NDEBUG - std::cout << ec.message() << "\n"; -#endif + CINATRA_LOG_DEBUG << ec.message() CINATRA_LOG_ENDL; } #endif } @@ -1350,9 +1335,7 @@ class coro_http_client { #ifdef INJECT_FOR_HTTP_CLIENT_TEST inject_chunk_valid = ClientInjectAction::none; #endif -#ifndef NDEBUG - std::cout << "bad chunked size\n"; -#endif + CINATRA_LOG_DEBUG << "bad chunked size" CINATRA_LOG_ENDL; ec = asio::error::make_error_code( asio::error::basic_errors::invalid_argument); break; @@ -1637,9 +1620,7 @@ class coro_http_client { promise.setValue(async_simple::Unit()); co_return false; } -#ifndef NDEBUG - std::cout << msg << " timeout\n"; -#endif + CINATRA_LOG_WARNING << msg << " timeout" CINATRA_LOG_ENDL; close_socket(*socket_); promise.setValue(async_simple::Unit()); co_return true; diff --git a/src/coro_http/examples/channel.cpp b/src/coro_http/examples/channel.cpp index 64f4e7ac24..3b230426bc 100644 --- a/src/coro_http/examples/channel.cpp +++ b/src/coro_http/examples/channel.cpp @@ -17,13 +17,13 @@ #include #include -#include #include #include #include #include #include #include +#include using namespace async_simple::coro; using namespace coro_http; @@ -32,7 +32,7 @@ using namespace coro_io; std::atomic qps, working_echo; Lazy test_async_channel(coro_io::channel &chan) { ++working_echo; - for (int i = 0; i < 10000; ++i) { + for (int i = 0; i < 100; ++i) { auto result = co_await chan.send_request( [&](coro_http_client &client, std::string_view host) -> Lazy { @@ -62,15 +62,16 @@ Lazy qps_watcher() { cnt = 0; } } - int main() { + easylog::init_log(easylog::Severity::TRACE); + std::vector hosts{"http://baidu.com", "http://www.baidu.com"}; auto chan = coro_io::channel::create( hosts, coro_io::channel::channel_config{ - .pool_config{.max_connection = 1000}}); + .pool_config{.max_connection = 100}}); - for (int i = 0, lim = std::thread::hardware_concurrency() * 10; i < lim; ++i) + for (int i = 0, lim = std::thread::hardware_concurrency(); i < lim; ++i) test_async_channel(chan).start([](auto &&) { }); syncAwait(qps_watcher());