commit 2f4289de81ec2f59955947bad61a6e9037183b63 from: Aleksey Ryndin date: Wed Aug 30 10:00:33 2023 UTC Print error to one string commit - 33be863885ab69ec32799c7d6844609fa6a5e039 commit + 2f4289de81ec2f59955947bad61a6e9037183b63 blob - 84d7f97f0e3b15bfa61f8da4c5e2419468355860 blob + f5aedf985eac8c5fc2e226eb082d9746ecff4e70 --- vostok/error.h +++ vostok/error.h @@ -24,7 +24,7 @@ class Print (public) explicit Print(int error=errno) : m_error{error} {} void operator() () const { - g_log << "Error code: " << std::dec << m_error << ". " << strerror(m_error) << std::endl; + g_log << "Error code (" << std::dec << m_error << "): " << strerror(m_error); } private: @@ -52,8 +52,9 @@ occurred( ) { print_action(); - g_log << " failed." << std::endl; + g_log << " failed. "; print_error(); + g_log << std::endl; } template blob - 3febc5c0529b541c35b077e06a481ff91dc8033a blob + 88f9a8d0e00e67af58036b1876e9f289344e7fcd --- vostok/transport.cc +++ vostok/transport.cc @@ -25,6 +25,21 @@ class ConfigPtr : public ConfigPtrBase (public) }; +class PrintIfError +{ +public: + explicit PrintIfError(czstring error) : m_error{error} {} + void operator() () const + { + if (m_error) + error::g_log << "Error: " << m_error; + } + +private: + czstring m_error; +}; + + bool read(NotNull ctx, std::vector &buff) { ssize_t ret{}; @@ -37,15 +52,7 @@ bool read(NotNull ctx, std::vector } if (ret == -1) { - const auto last_error = tls_error(ctx); - error::occurred( - "TLS read", - [last_error] - { - if (last_error) - error::g_log << "Error: " << last_error << std::endl; - } - ); + error::occurred("TLS read", PrintIfError{tls_error(ctx)}); return false; } buff.resize(ret); @@ -114,15 +121,7 @@ void create_server(NotNull cert_file, NotNul if (tls_configure(ctx.get(), cfg.get()) == -1) { - auto config_error = tls_config_error(cfg.get()); - error::occurred( - "Configure TLS context", - [config_error] - { - if (config_error) - error::g_log << "Error: " << config_error << std::endl; - } - ); + error::occurred("Configure TLS context", PrintIfError{tls_config_error(cfg.get())}); return; } @@ -209,15 +208,7 @@ bool send(NotNull ctx, Span } if (ret == -1) { - const auto last_error = tls_error(ctx); - error::occurred( - "TLS write", - [last_error] - { - if (last_error) - error::g_log << "Error: " << last_error << std::endl; - } - ); + error::occurred("TLS write", PrintIfError{tls_error(ctx)}); return false; } return true; blob - 1522a58db3d9e2729758ad19dda6d5cf73e2ba8f blob + c1151107c5b33b391e6681df5f6523245e2d71ed --- vostok/vostok.cc +++ vostok/vostok.cc @@ -58,15 +58,15 @@ void client_thread(const transport::AcceptedClient *ac switch (parse_result) { case url_too_short: - error::occurred("parse URL", []{error::g_log << meta::sz_url_too_short << "." << std::endl;}); + error::occurred("parse URL", []{error::g_log << meta::sz_url_too_short;}); transport::send_response(accepted_client->get_ctx(), gemini::STATUS_59_BAD_REQUEST, meta::url_too_short); return; case url_non_gemini: - error::occurred("parse URL", []{error::g_log << meta::sz_non_gemini << "." << std::endl;}); + error::occurred("parse URL", []{error::g_log << meta::sz_non_gemini;}); transport::send_response(accepted_client->get_ctx(), gemini::STATUS_53_PROXY_REQUEST_REFUSED, meta::non_gemini); return; case url_root_traverse: - error::occurred("parse URL", []{error::g_log << meta::sz_root_traverse << "." << std::endl;}); + error::occurred("parse URL", []{error::g_log << meta::sz_root_traverse;}); transport::send_response(accepted_client->get_ctx(), gemini::STATUS_50_PERMANENT_FAILURE, meta::root_traverse); return; @@ -149,7 +149,7 @@ bool server_loop(int server_socket, NotNull