Files
spark/src/lib/io/websocket/websocket_client_logger.h
T
John Colvin eee00cc407 diag: Integrate websocketpp with our logging system
* Unifies log message structure for improved readability
* Grants us runtime control over log levels via config
2025-03-05 20:48:14 +00:00

89 lines
2.2 KiB
C++

#pragma once
#include "lib/logging/Common.h"
#include "lib/logging/logging.h"
#include <websocketpp/concurrency/basic.hpp>
#include <websocketpp/logger/basic.hpp>
#include <websocketpp/logger/levels.hpp>
namespace websocketpp::log {
struct ElevelAdapter {
typedef elevel LevelT;
constexpr bts::log::Level operator()(level channel) {
switch (channel) {
case elevel::devel:
return bts::Diag;
case elevel::library:
case elevel::warn:
return bts::Warn;
case elevel::rerror:
return bts::Error;
case elevel::fatal:
return bts::Fatal;
default:
return bts::Info;
}
}
};
struct AlevelAdapter {
typedef alevel LevelT;
constexpr bts::log::Level operator()(level channel) {
switch (channel) {
case alevel::control:
case alevel::frame_header:
case alevel::frame_payload:
case alevel::message_header:
case alevel::message_payload:
case alevel::debug_handshake:
case alevel::debug_close:
case alevel::devel:
return bts::Diag;
case alevel::disconnect:
return bts::Warn;
case alevel::fail:
return bts::Error;
default:
return bts::Info;
}
}
};
template <typename LevelAdapter>
class WebsocketClientLogger
: public basic<concurrency::basic, typename LevelAdapter::LevelT> {
typedef basic<concurrency::basic, typename LevelAdapter::LevelT> base;
std::string name;
LevelAdapter level_adapter;
public:
WebsocketClientLogger(
level c, channel_type_hint::value h = channel_type_hint::access)
: base(c, h) {}
void set_name(std::string name) {
concurrency::basic::scoped_lock_type lock{this->m_lock};
this->name = std::move(name);
}
void write(level channel, const std::string &msg) {
concurrency::basic::scoped_lock_type lock{this->m_lock};
if (!this->dynamic_test(channel)) {
return;
}
LOG(Http, level_adapter(channel), "WebsocketClient-{} {}. {}", name,
std::string{LevelAdapter::LevelT::channel_name(channel)}, msg);
}
void write(level channel, const char *msg) {
write(channel, std::string{msg});
}
};
} // namespace websocketpp::log