diff --git a/include/ircd/client.h b/include/ircd/client.h index db9d6f3fc..a954059eb 100644 --- a/include/ircd/client.h +++ b/include/ircd/client.h @@ -17,6 +17,9 @@ namespace ircd const ipport &remote(const client &); const ipport &local(const client &); + + string_view loghead(const mutable_buffer &buf, const client &); + string_view loghead(const client &); } /// Remote party connecting to our daemon to make requests. @@ -51,7 +54,6 @@ struct ircd::client size_t content_consumed {0}; resource::request request; - string_view loghead() const; size_t write_all(const net::const_buffers &); size_t write_all(const const_buffer &); void close(const net::close_opts &, net::close_callback); diff --git a/include/ircd/net/acceptor.h b/include/ircd/net/acceptor.h index 9c6c28b8f..35f3c890a 100644 --- a/include/ircd/net/acceptor.h +++ b/include/ircd/net/acceptor.h @@ -16,6 +16,14 @@ // is part of the stack which can be included in your // definition file if you need low level access to this acceptor API. +namespace ircd::net +{ + #pragma GCC visibility push(hidden) + string_view loghead(const mutable_buffer &, const acceptor &); + string_view loghead(const acceptor &); + #pragma GCC visibility pop +} + /// Implementation to net::listener. See listener.h for additional interface. struct [[gnu::visibility("protected")]] ircd::net::acceptor diff --git a/include/ircd/net/acceptor_udp.h b/include/ircd/net/acceptor_udp.h index 621508d50..af271fdb5 100644 --- a/include/ircd/net/acceptor_udp.h +++ b/include/ircd/net/acceptor_udp.h @@ -16,6 +16,14 @@ // is part of the stack which can be included in your // definition file if you need low level access to this acceptor API. +namespace ircd::net +{ + #pragma GCC visibility push(hidden) + string_view loghead(const mutable_buffer &, const acceptor_udp &); + string_view loghead(const acceptor_udp &); + #pragma GCC visibility pop +} + struct [[gnu::visibility("protected")]] ircd::net::acceptor_udp { diff --git a/include/ircd/net/listener.h b/include/ircd/net/listener.h index 6feda375b..3a330f109 100644 --- a/include/ircd/net/listener.h +++ b/include/ircd/net/listener.h @@ -28,11 +28,6 @@ namespace ircd::net size_t handshaking_count(const acceptor &); size_t accepting_count(const acceptor &); - string_view loghead(const mutable_buffer &, const acceptor &); - string_view loghead(const acceptor &); - std::ostream &operator<<(std::ostream &s, const acceptor &); - std::ostream &operator<<(std::ostream &s, const listener &); - bool allow(acceptor &); bool start(acceptor &); bool stop(acceptor &); @@ -78,6 +73,4 @@ struct ircd::net::listener proffer = {}); ~listener() noexcept; - - friend std::ostream &operator<<(std::ostream &s, const listener &); }; diff --git a/include/ircd/net/listener_udp.h b/include/ircd/net/listener_udp.h index 6c722d083..3ead58569 100644 --- a/include/ircd/net/listener_udp.h +++ b/include/ircd/net/listener_udp.h @@ -15,11 +15,6 @@ namespace ircd::net { struct listener_udp; struct acceptor_udp; - - string_view loghead(const mutable_buffer &, const acceptor_udp &); - string_view loghead(const acceptor_udp &); - std::ostream &operator<<(std::ostream &s, const listener_udp &); - std::ostream &operator<<(std::ostream &s, const acceptor_udp &); } struct ircd::net::listener_udp @@ -46,8 +41,6 @@ struct ircd::net::listener_udp const std::string &options); ~listener_udp() noexcept; - - friend std::ostream &operator<<(std::ostream &s, const listener_udp &); }; struct ircd::net::listener_udp::datagram diff --git a/include/ircd/net/net.h b/include/ircd/net/net.h index f40ed8740..c6ad1dc6d 100644 --- a/include/ircd/net/net.h +++ b/include/ircd/net/net.h @@ -60,8 +60,6 @@ namespace ircd::net ipport remote_ipport(const socket &) noexcept; std::pair bytes(const socket &) noexcept; // std::pair calls(const socket &) noexcept; // - string_view loghead(const mutable_buffer &out, const socket &); - string_view loghead(const socket &); const_buffer peer_cert_der(const mutable_buffer &, const socket &); const_buffer peer_cert_der_sha256(const mutable_buffer &, const socket &); diff --git a/include/ircd/net/socket.h b/include/ircd/net/socket.h index ab3743e9b..0c640bf50 100644 --- a/include/ircd/net/socket.h +++ b/include/ircd/net/socket.h @@ -22,6 +22,9 @@ namespace ircd::net extern conf::item ssl_cipher_list; extern conf::item ssl_cipher_blacklist; extern asio::ssl::context sslv23_client; + + string_view loghead(const mutable_buffer &out, const socket &); + string_view loghead(const socket &); } /// Internal socket interface diff --git a/include/ircd/server/link.h b/include/ircd/server/link.h index 5a1fe74e6..12c61ab49 100644 --- a/include/ircd/server/link.h +++ b/include/ircd/server/link.h @@ -11,12 +11,6 @@ #pragma once #define HAVE_IRCD_SERVER_LINK_H -namespace ircd::server -{ - string_view loghead(const mutable_buffer &out, const link &); - string_view loghead(const link &); -} - /// A single connection to a remote peer. /// struct ircd::server::link diff --git a/include/ircd/server/request.h b/include/ircd/server/request.h index a7c2c7d38..fb856be73 100644 --- a/include/ircd/server/request.h +++ b/include/ircd/server/request.h @@ -24,12 +24,6 @@ namespace ircd::server // gets the unique tag number from request or 0 if no tag associated. uint64_t id(const request &) noexcept; - // convenience suite used to prefix log messages. - string_view loghead(const mutable_buffer &out, const request &); - string_view loghead(const request &); - string_view loghead(const mutable_buffer &out, const link &, const request &); - string_view loghead(const link &, const request &); - // control panel void submit(const hostport &, request &); bool cancel(request &); diff --git a/ircd/client.cc b/ircd/client.cc index 14e557829..b73c927db 100644 --- a/ircd/client.cc +++ b/ircd/client.cc @@ -155,6 +155,38 @@ noexcept // util // +ircd::string_view +ircd::loghead(const client &client) +{ + thread_local char buf[512]; + return loghead(buf, client); +} + +ircd::string_view +ircd::loghead(const mutable_buffer &buf, + const client &client) +{ + const string_view alpn + { + client.sock? + client.sock->alpn: + "h1"_sv + }; + + char rembuf[64], locbuf[64]; + return fmt::sprintf + { + buf, "socket:%lu local:%s remote:%s client:%lu %s %lu:%lu", + client.sock? net::id(*client.sock) : -1UL, + string(locbuf, local(client)), + string(rembuf, remote(client)), + client.id, + alpn, + client.ready_count, + client.request_count, + }; +} + const ircd::ipport & ircd::local(const client &client) { @@ -199,10 +231,11 @@ ircd::client::wait_all() { for(const auto &[remote, client] : client::map) { + assert(client); log::dwarning { log, "Waiting for client %s", - client->loghead(), + loghead(*client), }; assert(!client->sock || client->sock->fini); @@ -233,7 +266,8 @@ ircd::client::close_all() if(!client::map.empty()) log::debug { - log, "Closing %zu clients", client::map.size() + log, "Closing %zu clients", + client::map.size() }; auto it(begin(client::map)); @@ -247,7 +281,9 @@ ircd::client::close_all() { log::derror { - log, "Error disconnecting client @%p: %s", c.get(), e.what() + log, "Error disconnecting client @%p: %s", + c.get(), + e.what(), }; } } @@ -451,7 +487,7 @@ try log::debug { log, "%s enter", - client->loghead() + loghead(*client), }; #endif @@ -466,7 +502,7 @@ try log::debug { log, "%s leave %s", - client->loghead(), + loghead(*client), pretty(buf, timer.at(), true) }; #endif @@ -478,7 +514,7 @@ catch(const std::exception &e) log::error { log, "%s fault :%s", - client->loghead(), + loghead(*client), e.what() }; } @@ -496,7 +532,7 @@ ircd::handle_ec(client &client, log::dwarning { client::log, "%s refusing client request in runlevel %s", - client.loghead(), + loghead(client), reflect(run::level) }; @@ -533,7 +569,7 @@ try log::debug { client::log, "%s end of file", - client.loghead() + loghead(client), }; client.close(net::dc::SSL_NOTIFY, net::close_ignore); @@ -544,7 +580,7 @@ catch(const std::exception &e) log::error { client::log, "%s end of file :%s", - client.loghead(), + loghead(client), e.what() }; @@ -561,7 +597,7 @@ try log::dwarning { client::log, "%s short_read", - client.loghead() + loghead(client), }; client.close(net::dc::RST, net::close_ignore); @@ -572,7 +608,7 @@ catch(const std::exception &e) log::error { client::log, "%s short_read :%s", - client.loghead(), + loghead(client), e.what() }; @@ -591,7 +627,7 @@ try log::debug { client::log, "%s disconnecting after inactivity timeout", - client.loghead() + loghead(client), }; client.close(net::dc::SSL_NOTIFY, net::close_ignore); @@ -602,7 +638,7 @@ catch(const std::exception &e) log::derror { client::log, "%s timeout :%s", - client.loghead(), + loghead(client), e.what() }; @@ -619,7 +655,7 @@ ircd::handle_ec_default(client &client, log::derror { client::log, "%s :%s", - client.loghead(), + loghead(client), string(buf, ec) }; @@ -722,7 +758,7 @@ catch(const ctx::interrupted &e) log::warning { log, "%s request interrupted :%s", - loghead(), + loghead(*this), e.what() }; @@ -734,7 +770,7 @@ catch(const std::exception &e) log::critical { log, "%s :%s", - loghead(), + loghead(*this), e.what() }; @@ -786,7 +822,7 @@ try log::debug { resource::log, "%s HTTP %s `%s' content-length:%zu have:%zu", - loghead(), + loghead(*this), head.method, head.path, head.content_length, @@ -849,7 +885,7 @@ catch(const http::error &e) { log, log::level::DERROR, "%s HTTP %u %s :%s", - loghead(), + loghead(*this), uint(e.code), http::status(e.code), e.content @@ -913,7 +949,7 @@ catch(const http::error &e) { resource::log, log::level::DERROR, "%s HTTP %u `%s' %s :%s", - loghead(), + loghead(*this), uint(e.code), head.uri, http::status(e.code), @@ -956,7 +992,7 @@ catch(const std::exception &e) log::error { resource::log, "%s HTTP 500 Internal Error `%s' :%s", - loghead(), + loghead(*this), head.uri, e.what() }; @@ -992,7 +1028,7 @@ ircd::client::discard_unconsumed(const http::request::head &head) log::debug { log, "%s discarding %zu unconsumed of %zu bytes content...", - loghead(), + loghead(*this), unconsumed, head.content_length }; @@ -1039,35 +1075,3 @@ ircd::client::write_all(const net::const_buffers &bufs) return net::write_all(*sock, bufs); } - -/// Returns a string_view to a static (tls) buffer containing common -/// information used to prefix log calls for this client: i.e id, remote -/// address, etc. This is meant to be used as the first argument to all log -/// calls apropos this client and should not be held over a context switch -/// as there is only one static buffer. -ircd::string_view -ircd::client::loghead() -const -{ - thread_local char buf[512]; - - const string_view alpn - { - sock? - sock->alpn: - nullptr - }; - - char rembuf[64], locbuf[64]; - return fmt::sprintf - { - buf, "socket:%lu local:%s remote:%s client:%lu %s %lu:%lu", - sock? net::id(*sock) : -1UL, - string(locbuf, ircd::local(*this)), - string(rembuf, ircd::remote(*this)), - id, - alpn?: "h1"_sv, - ready_count, - request_count, - }; -} diff --git a/ircd/net_listener.cc b/ircd/net_listener.cc index 24c670852..4755a8d7a 100644 --- a/ircd/net_listener.cc +++ b/ircd/net_listener.cc @@ -49,20 +49,6 @@ ircd::net::allow(acceptor &a) return true; } -std::ostream & -ircd::net::operator<<(std::ostream &s, const acceptor &a) -{ - s << loghead(a); - return s; -} - -std::ostream & -ircd::net::operator<<(std::ostream &s, const listener &a) -{ - s << *a.acceptor; - return s; -} - ircd::string_view ircd::net::loghead(const acceptor &a) { diff --git a/ircd/net_listener_udp.cc b/ircd/net_listener_udp.cc index 0c15fe8cf..b44d05077 100644 --- a/ircd/net_listener_udp.cc +++ b/ircd/net_listener_udp.cc @@ -8,20 +8,6 @@ // copyright notice and this permission notice is present in all copies. The // full license for this software is available in the LICENSE file. -std::ostream & -ircd::net::operator<<(std::ostream &s, const listener_udp &a) -{ - s << *a.acceptor; - return s; -} - -std::ostream & -ircd::net::operator<<(std::ostream &s, const acceptor_udp &a) -{ - s << loghead(a); - return s; -} - ircd::string_view ircd::net::loghead(const acceptor_udp &a) { diff --git a/ircd/resource.cc b/ircd/resource.cc index a680a9707..9dd0e72f1 100644 --- a/ircd/resource.cc +++ b/ircd/resource.cc @@ -688,7 +688,7 @@ const log::derror { log, "%s Timed out in %s `%s'", - client.loghead(), + loghead(client), name, resource->path }; @@ -951,7 +951,7 @@ noexcept try log::derror { log, "%s HTTP response chunks:%u wrote:%zu flushed:%zu :stream interrupted...", - c->loghead(), + loghead(*c), count, wrote, flushed, @@ -991,7 +991,7 @@ ircd::resource::response::chunked::finish(const bool psh) log::debug { log, "%s HTTP --- `%s' in %s wrote:%zu flushed:%zu chunks:%u psh:%b", - c->loghead(), + loghead(*c), c->request.head.path, pretty(tmbuf, c->timer.at(), true), wrote, @@ -1371,7 +1371,7 @@ ircd::resource::response::response(client &client, { log, level, "%s HTTP %u `%s' %s in %s; %s content-length:%s head-length:%zu %s%s", - client.loghead(), + loghead(client), uint(code), client.request.head.path, http::status(code), diff --git a/ircd/server.cc b/ircd/server.cc index 594846f23..d4d04952b 100644 --- a/ircd/server.cc +++ b/ircd/server.cc @@ -23,6 +23,14 @@ namespace ircd::server template static size_t accumulate_tags(F&&); static string_view canonize(const hostport &); // TLS buffer + // convenience suite used to prefix log messages. + static string_view loghead(const mutable_buffer &out, const request &); + static string_view loghead(const request &); + static string_view loghead(const mutable_buffer &out, const link &, const request &); + static string_view loghead(const link &, const request &); + static string_view loghead(const mutable_buffer &out, const link &); + static string_view loghead(const link &); + // Internal control static decltype(ircd::server::peers)::iterator create(const net::hostport &, decltype(peers)::iterator &); diff --git a/matrix/resource.cc b/matrix/resource.cc index c75337fb2..dc0bc4df0 100644 --- a/matrix/resource.cc +++ b/matrix/resource.cc @@ -211,7 +211,7 @@ try log::debug { log, "%s %s %s %s `%s'", - client.loghead(), + loghead(client), ident, request.head.method, request.version?: "??"_sv, diff --git a/modules/client/rooms/context.cc b/modules/client/rooms/context.cc index 0f92c5b6c..97cf4485d 100644 --- a/modules/client/rooms/context.cc +++ b/modules/client/rooms/context.cc @@ -310,7 +310,7 @@ get__context(client &client, log::debug { context_log, "%s %s in %s before:%zu start:%s after:%zu end:%s state:%zu", - client.loghead(), + loghead(client), string_view{event_id}, string_view{room_id}, counts.before, diff --git a/modules/client/rooms/messages.cc b/modules/client/rooms/messages.cc index ed92e92d8..abedef901 100644 --- a/modules/client/rooms/messages.cc +++ b/modules/client/rooms/messages.cc @@ -201,7 +201,7 @@ get__messages(client &client, log::debug { messages_log, "%s in %s from:%s to:%s dir:%c limit:%zu start:%s end:%s hit:%zu miss:%zu post:%zu", - client.loghead(), + loghead(client), string_view{room_id}, string_view{page.from}, string_view{page.to}, diff --git a/modules/console.cc b/modules/console.cc index 9a0de569d..69f69c38e 100644 --- a/modules/console.cc +++ b/modules/console.cc @@ -17307,7 +17307,7 @@ console_cmd__synchron(opt &out, const string_view &line) { const auto *const &client(data_p->client); if(client) - out << client->loghead() << " | "; + out << loghead(*client) << " | "; out << m::sync::loghead(*data_p) << " | "; out << '\n';