fix: use qlogging in eventsub lib (through a hoop) (#6161)
This commit is contained in:
@@ -170,11 +170,12 @@ void BM_ParseAndHandleMessages(benchmark::State &state)
|
||||
{
|
||||
auto messages = readMessages();
|
||||
|
||||
auto log = std::make_shared<NullLogger>();
|
||||
std::unique_ptr<Listener> listener = std::make_unique<NoopListener>();
|
||||
boost::asio::io_context ioc;
|
||||
boost::asio::ssl::context ssl(
|
||||
boost::asio::ssl::context::method::tls_client);
|
||||
auto sess = std::make_shared<Session>(ioc, ssl, std::move(listener));
|
||||
auto sess = std::make_shared<Session>(ioc, ssl, std::move(listener), log);
|
||||
|
||||
for (auto _ : state)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <string_view>
|
||||
|
||||
namespace chatterino::eventsub::lib {
|
||||
|
||||
class Logger
|
||||
{
|
||||
public:
|
||||
virtual ~Logger() = default;
|
||||
virtual void debug(std::string_view msg) = 0;
|
||||
virtual void warn(std::string_view msg) = 0;
|
||||
};
|
||||
|
||||
class NullLogger : public Logger
|
||||
{
|
||||
public:
|
||||
void debug(std::string_view msg) override
|
||||
{
|
||||
}
|
||||
|
||||
void warn(std::string_view msg) override
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace chatterino::eventsub::lib
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "twitch-eventsub-ws/logger.hpp"
|
||||
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/beast/core.hpp>
|
||||
#include <boost/beast/ssl.hpp>
|
||||
@@ -23,7 +25,8 @@ public:
|
||||
// Resolver and socket require an io_context
|
||||
explicit Session(boost::asio::io_context &ioc,
|
||||
boost::asio::ssl::context &ctx,
|
||||
std::unique_ptr<Listener> listener);
|
||||
std::unique_ptr<Listener> listener,
|
||||
std::shared_ptr<Logger> log_);
|
||||
|
||||
// Start the asynchronous operation
|
||||
void run(std::string _host, std::string _port, std::string _path,
|
||||
@@ -63,6 +66,7 @@ private:
|
||||
|
||||
void checkKeepalive();
|
||||
|
||||
std::shared_ptr<Logger> log;
|
||||
boost::asio::ip::tcp::resolver resolver;
|
||||
boost::beast::websocket::stream<
|
||||
boost::beast::ssl_stream<boost::beast::tcp_stream>>
|
||||
|
||||
Submodule
+1
Submodule lib/twitch-eventsub-ws/lib/fmt added at 123913715a
@@ -53,6 +53,7 @@ target_include_directories(${PROJECT_NAME}
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/../include"
|
||||
PRIVATE
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/../lib/date/include"
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/../lib/fmt/include"
|
||||
)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME}
|
||||
|
||||
@@ -15,10 +15,26 @@
|
||||
#include <boost/json.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
|
||||
#if __cpp_lib_format >= 201907L && !defined(__APPLE__)
|
||||
# include <format>
|
||||
#else
|
||||
# define FMT_HEADER_ONLY
|
||||
# include "fmt/format.h"
|
||||
#endif
|
||||
|
||||
namespace c2fmt {
|
||||
|
||||
#if __cpp_lib_format >= 201907L && !defined(__APPLE__)
|
||||
using std::format;
|
||||
#else
|
||||
using fmt::format;
|
||||
#endif
|
||||
|
||||
} // namespace c2fmt
|
||||
|
||||
namespace beast = boost::beast;
|
||||
namespace http = beast::http;
|
||||
namespace websocket = beast::websocket;
|
||||
@@ -230,8 +246,10 @@ namespace {
|
||||
|
||||
// Resolver and socket require an io_context
|
||||
Session::Session(boost::asio::io_context &ioc, boost::asio::ssl::context &ctx,
|
||||
std::unique_ptr<Listener> listener)
|
||||
: resolver(boost::asio::make_strand(ioc))
|
||||
std::unique_ptr<Listener> listener,
|
||||
std::shared_ptr<Logger> log_)
|
||||
: log(std::move(log_))
|
||||
, resolver(boost::asio::make_strand(ioc))
|
||||
, ws(boost::asio::make_strand(ioc), ctx)
|
||||
, listener(std::move(listener))
|
||||
{
|
||||
@@ -418,7 +436,8 @@ void Session::onClose(beast::error_code ec)
|
||||
|
||||
void Session::fail(beast::error_code ec, std::string_view op)
|
||||
{
|
||||
std::cerr << op << ": " << ec.message() << " (" << ec.location() << ")\n";
|
||||
this->log->warn(c2fmt::format("{}: {} ({})", op, ec.message(),
|
||||
ec.location().to_string()));
|
||||
if (!this->ws.is_open() && this->listener)
|
||||
{
|
||||
if (this->keepaliveTimer)
|
||||
@@ -505,7 +524,8 @@ boost::system::error_code Session::onSessionWelcome(
|
||||
this->keepaliveTimeout =
|
||||
std::chrono::seconds{payload.keepaliveTimeoutSeconds.value_or(60)} * 2;
|
||||
assert(!this->keepaliveTimer);
|
||||
std::cerr << "Keepalive: " << this->keepaliveTimeout.count() << 's';
|
||||
this->log->debug(
|
||||
c2fmt::format("Keepalive: {}s", this->keepaliveTimeout.count()));
|
||||
this->checkKeepalive();
|
||||
|
||||
return {};
|
||||
@@ -551,7 +571,7 @@ void Session::checkKeepalive()
|
||||
{
|
||||
if (!this->receivedMessage)
|
||||
{
|
||||
std::cerr << "Keepalive timeout, closing\n";
|
||||
this->log->debug("Keepalive timeout, closing");
|
||||
if (this->listener)
|
||||
{
|
||||
this->listener->onClose(std::move(this->listener), {});
|
||||
@@ -572,7 +592,8 @@ void Session::checkKeepalive()
|
||||
this->keepaliveTimer->async_wait([this](boost::system::error_code ec) {
|
||||
if (ec)
|
||||
{
|
||||
std::cerr << "Keepalive timer cancelled: " << ec.message() << '\n';
|
||||
this->log->warn(
|
||||
c2fmt::format("Keepalive timer cancelled: {}", ec.message()));
|
||||
return;
|
||||
}
|
||||
this->checkKeepalive();
|
||||
|
||||
@@ -153,11 +153,12 @@ TEST_P(TestHandleMessageP, Run)
|
||||
{
|
||||
auto buf = readToFlatBuffer(filePath(GetParam() + ".json"));
|
||||
|
||||
auto log = std::make_shared<NullLogger>();
|
||||
std::unique_ptr<Listener> listener = std::make_unique<NoOpListener>();
|
||||
boost::asio::io_context ioc;
|
||||
boost::asio::ssl::context ssl(
|
||||
boost::asio::ssl::context::method::tls_client);
|
||||
auto sess = std::make_shared<Session>(ioc, ssl, std::move(listener));
|
||||
auto sess = std::make_shared<Session>(ioc, ssl, std::move(listener), log);
|
||||
auto ec = sess->handleMessage(buf);
|
||||
ASSERT_FALSE(ec.failed())
|
||||
<< ec.what() << ec.message() << ec.location().to_string();
|
||||
|
||||
Reference in New Issue
Block a user