Skip to content

Commit

Permalink
add log wrapper (qicosmos#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Aug 1, 2023
1 parent a4a2e16 commit 83218be
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 40 deletions.
74 changes: 74 additions & 0 deletions include/cinatra/cinatra_log_wrapper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include <iostream>
namespace cinatra {
struct null_logger_t {
template <typename T>
const null_logger_t& operator<<(T&&) const {
return *this;
}
};
struct cout_logger_t {
template <typename T>
const cout_logger_t& operator<<(T&& t) const {
std::cout << std::forward<T>(t);
return *this;
}
~cout_logger_t() { std::cout << std::endl; }
};
struct cerr_logger_t {
template <typename T>
const cerr_logger_t& operator<<(T&& t) const {
std::cerr << std::forward<T>(t);
return *this;
}
~cerr_logger_t() { std::cerr << std::endl; }
};

constexpr inline cinatra::null_logger_t NULL_LOGGER;

} // namespace cinatra

#ifdef CINATRA_LOG_ERROR
#else
#define CINATRA_LOG_ERROR \
cerr_logger_t {}
#endif

#ifdef CINATRA_LOG_WARNING
#else
#ifndef NDEBUG
#define CINATRA_LOG_WARNING \
cerr_logger_t {}
#else
#define CINATRA_LOG_WARNING NULL_LOGGER
#endif
#endif

#ifdef CINATRA_LOG_INFO
#else
#ifndef NDEBUG
#define CINATRA_LOG_INFO \
cout_logger_t {}
#else
#define CINATRA_LOG_INFO NULL_LOGGER
#endif
#endif

#ifdef CINATRA_LOG_DEBUG
#else
#ifndef NDEBUG
#define CINATRA_LOG_DEBUG \
cout_logger_t {}
#else
#define CINATRA_LOG_DEBUG NULL_LOGGER
#endif
#endif

#ifdef CINATRA_LOG_TRACE
#else
#ifndef NDEBUG
#define CINATRA_LOG_TRACE \
cout_logger_t {}
#else
#define CINATRA_LOG_TRACE NULL_LOGGER
#endif
#endif
57 changes: 17 additions & 40 deletions include/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 @@ -275,7 +276,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();
}
return ssl_init_ret_;
}
Expand Down Expand Up @@ -351,9 +352,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:";
co_return false;
}

Expand Down Expand Up @@ -471,9 +470,7 @@ 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();
data.net_err = ec;
data.status = 404;
}
Expand All @@ -489,9 +486,7 @@ 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();
data.net_err = ec;
data.status = 404;
close_socket(*socket_);
Expand Down Expand Up @@ -571,22 +566,18 @@ 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;
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();
}
std::cout << "file not exists, "
<< std::filesystem::current_path().string() << std::endl;
#endif
CINATRA_LOG_WARNING << "file not exists, "
<< std::filesystem::current_path().string();
return false;
}

Expand Down Expand Up @@ -637,9 +628,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";
co_return resp_data{{}, 404};
}

Expand Down Expand Up @@ -680,9 +669,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();
co_return resp_data{ec, 404};
}

Expand Down Expand Up @@ -715,9 +702,7 @@ 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";
co_return resp_data{{}, 404};
}
co_return co_await async_upload_multipart(std::move(uri));
Expand Down Expand Up @@ -925,7 +910,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;
#endif
auto future = start_timer(req_timeout_duration_, "request timer");
if (has_body) {
Expand Down Expand Up @@ -959,9 +944,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();
}
co_return ec;
}
Expand Down Expand Up @@ -1308,9 +1291,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();
}
#endif
}
Expand Down Expand Up @@ -1356,9 +1337,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";
ec = asio::error::make_error_code(
asio::error::basic_errors::invalid_argument);
break;
Expand Down Expand Up @@ -1650,9 +1629,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";
close_socket(*socket_);
promise.setValue(async_simple::Unit());
co_return true;
Expand Down

0 comments on commit 83218be

Please sign in to comment.