websocket_logging.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <functional>
4 
5 #include <asio/ip/address.hpp>
6 #include <websocketpp/logger/levels.hpp>
7 
8 #include "common.hpp"
9 
10 namespace foxglove {
11 
12 using LogCallback = std::function<void(WebSocketLogLevel, char const*)>;
13 
14 inline std::string IPAddressToString(const asio::ip::address& addr) {
15  if (addr.is_v6()) {
16  return "[" + addr.to_string() + "]";
17  }
18  return addr.to_string();
19 }
20 
21 inline void NoOpLogCallback(WebSocketLogLevel, char const*) {}
22 
24 public:
25  using channel_type_hint = websocketpp::log::channel_type_hint;
26 
27  CallbackLogger(channel_type_hint::value hint = channel_type_hint::access)
28  : _staticChannels(0xffffffff)
29  , _dynamicChannels(0)
30  , _channelTypeHint(hint)
32 
33  CallbackLogger(websocketpp::log::level channels,
34  channel_type_hint::value hint = channel_type_hint::access)
35  : _staticChannels(channels)
36  , _dynamicChannels(0)
37  , _channelTypeHint(hint)
39 
40  void set_callback(LogCallback callback) {
41  _callback = callback;
42  }
43 
44  void set_channels(websocketpp::log::level channels) {
45  if (channels == 0) {
46  clear_channels(0xffffffff);
47  return;
48  }
49 
50  _dynamicChannels |= (channels & _staticChannels);
51  }
52 
53  void clear_channels(websocketpp::log::level channels) {
54  _dynamicChannels &= ~channels;
55  }
56 
57  void write(websocketpp::log::level channel, std::string const& msg) {
58  write(channel, msg.c_str());
59  }
60 
61  void write(websocketpp::log::level channel, char const* msg) {
62  if (!this->dynamic_test(channel)) {
63  return;
64  }
65 
66  if (_channelTypeHint == channel_type_hint::access) {
68  } else {
69  if (channel == websocketpp::log::elevel::devel) {
71  } else if (channel == websocketpp::log::elevel::library) {
73  } else if (channel == websocketpp::log::elevel::info) {
75  } else if (channel == websocketpp::log::elevel::warn) {
77  } else if (channel == websocketpp::log::elevel::rerror) {
79  } else if (channel == websocketpp::log::elevel::fatal) {
81  }
82  }
83  }
84 
85  constexpr bool static_test(websocketpp::log::level channel) const {
86  return ((channel & _staticChannels) != 0);
87  }
88 
89  bool dynamic_test(websocketpp::log::level channel) {
90  return ((channel & _dynamicChannels) != 0);
91  }
92 
93 private:
94  websocketpp::log::level const _staticChannels;
95  websocketpp::log::level _dynamicChannels;
96  channel_type_hint::value _channelTypeHint;
98 };
99 
100 } // namespace foxglove
constexpr bool static_test(websocketpp::log::level channel) const
void write(websocketpp::log::level channel, char const *msg)
void set_callback(LogCallback callback)
void NoOpLogCallback(WebSocketLogLevel, char const *)
void clear_channels(websocketpp::log::level channels)
void write(websocketpp::log::level channel, std::string const &msg)
void set_channels(websocketpp::log::level channels)
std::function< void(WebSocketLogLevel, char const *)> LogCallback
CallbackLogger(websocketpp::log::level channels, channel_type_hint::value hint=channel_type_hint::access)
CallbackLogger(channel_type_hint::value hint=channel_type_hint::access)
websocketpp::log::level const _staticChannels
websocketpp::log::channel_type_hint channel_type_hint
channel_type_hint::value _channelTypeHint
std::string IPAddressToString(const asio::ip::address &addr)
bool dynamic_test(websocketpp::log::level channel)
WebSocketLogLevel
Definition: common.hpp:41
websocketpp::log::level _dynamicChannels


foxglove_bridge
Author(s): Foxglove
autogenerated on Mon Jul 3 2023 02:12:22