| #include <boost/leaf/handle_exception.hpp> | #include <boost/leaf/handle_exception.hpp> | ||||
| #include <fmt/ostream.h> | #include <fmt/ostream.h> | ||||
| #include <neo/event.hpp> | |||||
| #include <filesystem> | #include <filesystem> | ||||
| #include <iostream> | #include <iostream> | ||||
| int main_fn(std::string_view program_name, const std::vector<std::string>& argv) { | int main_fn(std::string_view program_name, const std::vector<std::string>& argv) { | ||||
| dds::log::init_logger(); | dds::log::init_logger(); | ||||
| auto log_subscr = neo::subscribe(&dds::log::ev_log::print); | |||||
| dds::install_signal_handlers(); | dds::install_signal_handlers(); | ||||
| dds::cli::options opts; | dds::cli::options opts; |
| .parse_tail = {}, | .parse_tail = {}, | ||||
| }; | }; | ||||
| dds_log(trace, | |||||
| dds_log(debug, | |||||
| " --> HTTP {} {}://{}:{}{}", | " --> HTTP {} {}://{}:{}{}", | ||||
| params.method, | params.method, | ||||
| origin.protocol, | origin.protocol, | ||||
| disconnect = true; | disconnect = true; | ||||
| } | } | ||||
| _peer_disconnected = disconnect; | _peer_disconnected = disconnect; | ||||
| dds_log(trace, " <-- HTTP {} {}", r.status, r.status_message); | |||||
| dds_log(debug, " <-- HTTP {} {}", r.status, r.status_message); | |||||
| return r; | return r; | ||||
| } | } | ||||
| }; | }; | ||||
| ret._pool = _impl; | ret._pool = _impl; | ||||
| if (iter == _impl->_clients.end()) { | if (iter == _impl->_clients.end()) { | ||||
| // Nothing for this origin yet | // Nothing for this origin yet | ||||
| dds_log(debug, "Opening new connection to {}://{}:{}", origin.protocol, origin.hostname, origin.port); | |||||
| auto ptr = std::make_shared<detail::http_client_impl>(origin); | auto ptr = std::make_shared<detail::http_client_impl>(origin); | ||||
| ptr->connect(); | ptr->connect(); | ||||
| ret._impl = ptr; | ret._impl = ptr; | ||||
| } else { | } else { | ||||
| dds_log(debug, | |||||
| "Reusing existing connection to {}://{}:{}", | |||||
| origin.protocol, | |||||
| origin.hostname, | |||||
| origin.port); | |||||
| ret._impl = iter->second; | ret._impl = iter->second; | ||||
| _impl->_clients.erase(iter); | _impl->_clients.erase(iter); | ||||
| } | } |
| #include "./log.hpp" | #include "./log.hpp" | ||||
| #include <neo/assert.hpp> | #include <neo/assert.hpp> | ||||
| #include <neo/event.hpp> | |||||
| #include <spdlog/spdlog.h> | #include <spdlog/spdlog.h> | ||||
| spdlog::set_pattern("[%^%-5l%$] %v"); | spdlog::set_pattern("[%^%-5l%$] %v"); | ||||
| } | } | ||||
| void dds::log::ev_log::print() const noexcept { log_print(level, message); } | |||||
| void dds::log::log_print(dds::log::level l, std::string_view msg) noexcept { | void dds::log::log_print(dds::log::level l, std::string_view msg) noexcept { | ||||
| static auto logger_inst = [] { | static auto logger_inst = [] { | ||||
| auto logger = spdlog::default_logger_raw(); | auto logger = spdlog::default_logger_raw(); | ||||
| neo_assert_always(invariant, false, "Invalid log level", msg, int(l)); | neo_assert_always(invariant, false, "Invalid log level", msg, int(l)); | ||||
| }(); | }(); | ||||
| logger_inst->log(lvl, "{}", msg); | |||||
| logger_inst->log(lvl, msg); | |||||
| } | |||||
| void dds::log::log_emit(dds::log::ev_log ev) noexcept { | |||||
| if (!neo::get_event_subscriber<ev_log>()) { | |||||
| thread_local bool did_warn = false; | |||||
| if (!did_warn) { | |||||
| log_print(level::warn, | |||||
| "The calling thread issued a log message, but there is no subscriber " | |||||
| "listening for it. The log message will be dropped. This is a bug!"); | |||||
| did_warn = true; | |||||
| } | |||||
| } | |||||
| neo::emit(ev); | |||||
| } | } |
| inline level current_log_level = level::info; | inline level current_log_level = level::info; | ||||
| struct ev_log { | |||||
| log::level level; | |||||
| std::string_view message; | |||||
| void print() const noexcept; | |||||
| }; | |||||
| void log_print(level l, std::string_view s) noexcept; | void log_print(level l, std::string_view s) noexcept; | ||||
| void log_emit(ev_log) noexcept; | |||||
| void init_logger() noexcept; | void init_logger() noexcept; | ||||
| void log(level l, std::string_view s, const Args&... args) noexcept { | void log(level l, std::string_view s, const Args&... args) noexcept { | ||||
| if (int(l) >= int(current_log_level)) { | if (int(l) >= int(current_log_level)) { | ||||
| auto message = fmt::format(s, args...); | auto message = fmt::format(s, args...); | ||||
| log_print(l, message); | |||||
| log_emit(ev_log{l, message}); | |||||
| } | } | ||||
| } | } | ||||
| #pragma once | #pragma once | ||||
| #include <dds/util/log.hpp> | |||||
| #include <neo/event.hpp> | |||||
| #include <algorithm> | #include <algorithm> | ||||
| #include <iterator> | #include <iterator> | ||||
| #include <mutex> | #include <mutex> | ||||
| std::vector<std::exception_ptr> exceptions; | std::vector<std::exception_ptr> exceptions; | ||||
| auto run_one = [&]() mutable { | auto run_one = [&]() mutable { | ||||
| auto log_subscr = neo::subscribe(&log::ev_log::print); | |||||
| while (true) { | while (true) { | ||||
| std::unique_lock lk{mut}; | std::unique_lock lk{mut}; | ||||
| if (!exceptions.empty()) { | if (!exceptions.empty()) { |