72 :
public spdlog::sinks::sink
73 ,
public Nano::Observer
76 : socket{std::move(s)}
77 , sender{std::move(send)}
79 socket->socket.on_open.connect<&websocket_log_sink::open_fun>(
this);
85 while(logs.try_dequeue(m))
87 socket->socket.send_message(m);
93 socket->socket.on_open.disconnect<&websocket_log_sink::open_fun>(
this);
96 void make_message(
const spdlog::details::log_msg& msg)
100 ossia::json_writer writer{buffer};
102 writer.StartObject();
104 writer.Key(
"operation");
105 writer.String(
"log");
110 case spdlog::level::trace:
111 writer.String(
"trace");
113 case spdlog::level::debug:
114 writer.String(
"debug");
116 case spdlog::level::info:
117 writer.String(
"info");
119 case spdlog::level::warn:
120 writer.String(
"warn");
122 case spdlog::level::err:
123 writer.String(
"error");
125 case spdlog::level::critical:
126 writer.String(
"critical");
128 case spdlog::level::off:
129 writer.String(
"off");
133 writer.Key(
"sender");
134 writer.String(sender.data(), sender.size());
136 writer.Key(
"message");
137 writer.String(msg.payload.data(), msg.payload.size());
142 void send_message(
const spdlog::details::log_msg& msg)
145 socket->socket.send_message(buffer);
148 void log(
const spdlog::details::log_msg& msg)
override
150 if(!socket->socket.connected())
153 logs.enqueue(std::string{buffer.GetString(), buffer.GetSize()});
162 void flush()
override { }
164 void set_pattern(
const std::string& pattern)
override { }
165 void set_formatter(std::unique_ptr<spdlog::formatter> sink_formatter)
override { }
168 rapidjson::StringBuffer buffer;
169 std::shared_ptr<websocket_threaded_connection> socket;
172 moodycamel::ReaderWriterQueue<std::string> logs;
180 std::shared_ptr<websocket_threaded_connection> t, std::string s,
181 std::chrono::seconds dur)
186 thread = std::thread([
this] {
189 if(init && conn->socket.connected())
192 ossia::json_writer writer{buffer};
193 writer.StartObject();
195 writer.Key(
"operation");
196 writer.String(
"alive");
198 writer.Key(
"sender");
199 writer.String(sender.data(), sender.size());
203 conn->socket.send_message(buffer);
206 for(
int i = 0; i < 100; i++)
208 std::this_thread::sleep_for(
209 std::chrono::duration_cast<std::chrono::milliseconds>(interval) / 100.0);
216 t->socket.on_open.connect<&websocket_heartbeat::open_fun>(*this);
221 conn->socket.send_message(init_msg);
225 ~websocket_heartbeat()
227 conn->socket.on_open.disconnect<&websocket_heartbeat::open_fun>(*this);
229 if(thread.joinable())
234 send_init(
const ossia::hash_map<std::string, ossia::variant<std::string, int>>& map)
236 rapidjson::StringBuffer buffer;
237 ossia::json_writer writer{buffer};
238 writer.StartObject();
240 writer.Key(
"operation");
241 writer.String(
"initWatchdog");
243 writer.Key(
"sender");
244 writer.String(sender.data(), sender.size());
246 writer.Key(
"aliveTime");
247 writer.Int(interval.count());
251 ossia::json_writer& writer;
252 void operator()(
const std::string& s) { writer.String(s); }
254 void operator()(
int s) { writer.Int(s); }
257 for(
const auto& pair : map)
259 writer.Key(pair.first);
260 ossia::visit(sw, pair.second);
265 if(conn->socket.connected())
267 conn->socket.send_message(buffer);
272 init_msg = std::string(buffer.GetString(), buffer.GetSize());
277 rapidjson::StringBuffer buffer;
279 std::chrono::seconds interval;
281 std::string init_msg;
282 std::shared_ptr<websocket_threaded_connection> conn;
283 std::atomic_bool running{
true};
284 std::atomic_bool init{
false};