Go to the documentation of this file.
9 #ifndef EAGINE_LOGGING_ASIO_BACKEND_HPP
10 #define EAGINE_LOGGING_ASIO_BACKEND_HPP
12 #include "../config/platform.hpp"
18 EAGINE_DIAG_OFF(disabled-macro-expansion)
19 EAGINE_DIAG_OFF(covered-switch-default)
20 EAGINE_DIAG_OFF(
zero-as-null-pointer-constant)
21 EAGINE_DIAG_OFF(shorten-64-to-32)
22 EAGINE_DIAG_OFF(deprecated)
25 #include <asio/io_context.hpp>
26 #include <asio/ip/tcp.hpp>
28 #include <asio/local/stream_protocol.hpp>
30 #include <asio/streambuf.hpp>
31 #include <asio/write.hpp>
40 class asio_local_ostream_log_connection {
41 static auto _fix_addr(
string_view addr_str) noexcept {
42 return addr_str ? addr_str :
string_view{
"/tmp/eagine-xmllog"};
46 asio_local_ostream_log_connection(
string_view addr_str)
47 : _endpoint{
c_str(_fix_addr(addr_str))}
50 _socket.connect(_endpoint);
53 auto out() noexcept -> std::ostream& {
54 if(_socket.is_open()) {
63 if(_socket.is_open()) {
64 const auto done = asio::write(_socket, _buffer);
65 _buffer.consume(done);
72 asio::io_context _context{};
73 asio::local::stream_protocol::endpoint _endpoint{};
74 asio::local::stream_protocol::socket _socket;
75 asio::streambuf _buffer{};
80 class asio_tcpipv4_ostream_log_connection {
82 asio_tcpipv4_ostream_log_connection(
string_view addr_str)
83 : _addr{_fix_addr(addr_str)}
88 _resolver.resolve(std::get<0>(_addr), std::get<1>(_addr))) {
89 _socket.connect(endpt);
94 auto out() noexcept -> std::ostream& {
95 if(_socket.is_open()) {
104 if(_socket.is_open()) {
105 const auto done = asio::write(_socket, _buffer);
106 _buffer.consume(done);
114 -> std::tuple<std::string, std::string> {
117 hostname ? to_string(hostname) : std::string(
"localhost"),
118 port_str ? to_string(port_str) : std::string(
"34917")};
120 std::tuple<std::string, std::string> _addr;
122 asio::io_context _context{};
123 asio::ip::tcp::resolver _resolver;
124 asio::ip::tcp::socket _socket;
125 asio::streambuf _buffer{};
129 template <
typename Connection,
typename Lockable>
130 class asio_ostream_log_backend
132 ,
public ostream_log_backend<Lockable> {
134 using base = ostream_log_backend<Lockable>;
136 void flush() noexcept final {
137 Connection::_flush();
141 asio_ostream_log_backend(
144 : Connection(addr_str)
145 , ostream_log_backend<Lockable>(Connection::out(), min_severity) {}
148 : asio_ostream_log_backend(
string_view{}, min_severity) {}
150 asio_ostream_log_backend(asio_ostream_log_backend&&) =
delete;
151 asio_ostream_log_backend(
const asio_ostream_log_backend&) =
delete;
152 auto operator=(asio_ostream_log_backend&&) =
delete;
153 auto operator=(
const asio_ostream_log_backend&) =
delete;
155 ~asio_ostream_log_backend() noexcept
override {
161 template <
typename Lockable = std::mutex>
162 using asio_local_ostream_log_backend =
163 asio_ostream_log_backend<asio_local_ostream_log_connection, Lockable>;
164 #define EAGINE_HAS_ASIO_LOCAL_LOG_BACKEND 1
166 #define EAGINE_HAS_ASIO_LOCAL_LOG_BACKEND 0
167 #endif // EAGINE_POSIX
169 template <
typename Lockable = std::mutex>
170 using asio_tcpipv4_ostream_log_backend =
171 asio_ostream_log_backend<asio_tcpipv4_ostream_log_connection, Lockable>;
175 #endif // EAGINE_LOGGING_ASIO_BACKEND_HPP
basic_string_span< const char > string_view
Alias for const string views.
Definition: string_span.hpp:116
static constexpr auto c_str(memory::basic_span< C, P, S > s) -> std::enable_if_t< std::is_convertible_v< memory::basic_span< C, P, S >, basic_string_span< C, P, S >>, basic_c_str< C, P, S >>
Functions that construct a basic_c_str from a basic_string_span.
Definition: string_span.hpp:226
Common code is placed in this namespace.
Definition: eagine.hpp:21
static auto split_by_last(basic_span< T1, P1, S1 > spn, basic_span< T2, P2, S2 > what) -> std::tuple< basic_span< T1, P1, S1 >, basic_span< T1, P1, S1 >>
Splits a span by the last occurrence of what (before and after, what)
Definition: span_algo.hpp:479
static auto zero(basic_span< T, P, S > spn) -> std::enable_if_t< std::is_integral_v< T >||std::is_floating_point_v< T >, basic_span< T, P, S >>
Fills a span with zero value of type T.
Definition: span_algo.hpp:548
log_event_severity
Log event severity enumeration.
Definition: severity.hpp:18