Skip to content

Commit

Permalink
[coro_http] [easylog] use easylog instead of std::cout
Browse files Browse the repository at this point in the history
  • Loading branch information
poor-circle committed Jul 20, 2023
1 parent dd666cf commit ff6da2c
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 52 deletions.
8 changes: 8 additions & 0 deletions include/ylt/coro_http/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
#ifdef YLT_ENABLE_SSL
#define CINATRA_ENABLE_SSL
#endif
#include <ylt/easylog.hpp>
#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 <cinatra/coro_http_client.hpp>

namespace coro_http {
Expand Down
11 changes: 6 additions & 5 deletions include/ylt/easylog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ inline void add_appender(std::function<void(std::string_view)> 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<Id>::instance().check_severity(severity)) { \
Expand All @@ -149,19 +150,19 @@ inline void add_appender(std::function<void(std::string_view)> 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<Id>(); \
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)
Expand Down
4 changes: 2 additions & 2 deletions include/ylt/easylog/record.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ enum { ERROR = 0 };
#endif
#endif

namespace easylog {

enum class Severity {
NONE = 0,
TRACE = 1,
Expand All @@ -60,8 +62,6 @@ enum class Severity {
CRITICAL = 6,
};

namespace easylog {

inline std::string_view severity_str(Severity severity) {
switch (severity) {
case Severity::TRACE:
Expand Down
56 changes: 56 additions & 0 deletions include/ylt/thirdparty/cinatra/cinatra_log_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace cinatra {
struct null_logger_t {
template <typename T>
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
61 changes: 21 additions & 40 deletions include/ylt/thirdparty/cinatra/coro_http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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_;
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -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_);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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};
}

Expand Down Expand Up @@ -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};
}

Expand Down Expand Up @@ -710,9 +700,8 @@ class coro_http_client {
async_simple::coro::Lazy<resp_data> 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));
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 6 additions & 5 deletions src/coro_http/examples/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
#include <async_simple/coro/Lazy.h>
#include <async_simple/coro/SyncAwait.h>

#include <cinatra/uri.hpp>
#include <iostream>
#include <thread>
#include <ylt/coro_http/coro_http_client.hpp>
#include <ylt/coro_io/channel.hpp>
#include <ylt/coro_io/client_pool.hpp>
#include <ylt/coro_io/coro_io.hpp>
#include <ylt/easylog.hpp>

using namespace async_simple::coro;
using namespace coro_http;
Expand All @@ -32,7 +32,7 @@ using namespace coro_io;
std::atomic<int> qps, working_echo;
Lazy<void> test_async_channel(coro_io::channel<coro_http_client> &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<coro_http::resp_data> {
Expand Down Expand Up @@ -62,15 +62,16 @@ Lazy<void> qps_watcher() {
cnt = 0;
}
}

int main() {
easylog::init_log(easylog::Severity::TRACE);

std::vector<std::string_view> hosts{"http://baidu.com",
"http://www.baidu.com"};
auto chan = coro_io::channel<coro_http_client>::create(
hosts, coro_io::channel<coro_http_client>::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());
Expand Down

0 comments on commit ff6da2c

Please sign in to comment.