Belle II Software light-2406-ragdoll
LogConnectionUDP Class Referencefinal

Log Connection to send the log message as JSON to a UDP server. More...

#include <LogConnectionUDP.h>

Inheritance diagram for LogConnectionUDP:
Collaboration diagram for LogConnectionUDP:

Public Member Functions

 LogConnectionUDP (const std::string &hostname, unsigned short port)
 Create a new UDP log connection with the hostname (as name or IP) and the port to connect to.
 
 ~LogConnectionUDP () override
 Close the socket on destruction.
 
bool sendMessage (const LogMessage &message) final
 Send the log message as JSON to the UDP server.
 
bool isConnected () final
 there is no way to check if a UDP connection is fine, so we just return True always
 
virtual void finalizeOnAbort ()
 Called just before the system exists after a fatal error.
 

Private Attributes

boost::asio::io_service m_ioservice
 asio service for handling the requests
 
boost::asio::ip::udp::socket m_socket
 the socket to the UDP server
 
boost::asio::ip::udp::endpoint m_remoteEndpoint
 the remote endpoint we send to
 

Detailed Description

Log Connection to send the log message as JSON to a UDP server.

Definition at line 21 of file LogConnectionUDP.h.

Constructor & Destructor Documentation

◆ LogConnectionUDP()

LogConnectionUDP ( const std::string &  hostname,
unsigned short  port 
)

Create a new UDP log connection with the hostname (as name or IP) and the port to connect to.

if the hostname can not be resolved, this will throw an exception so dereferencing the result is always safe

Definition at line 17 of file LogConnectionUDP.cc.

18{
19 ip::udp::resolver resolver(m_ioservice);
20 ip::udp::resolver::query udpquery(udp::v4(), hostname, std::to_string(port));
21
22 m_socket.open(ip::udp::v4());
24 m_remoteEndpoint = *resolver.resolve(udpquery);
25}
boost::asio::ip::udp::endpoint m_remoteEndpoint
the remote endpoint we send to
boost::asio::ip::udp::socket m_socket
the socket to the UDP server
boost::asio::io_service m_ioservice
asio service for handling the requests

◆ ~LogConnectionUDP()

~LogConnectionUDP ( )
override

Close the socket on destruction.

Definition at line 42 of file LogConnectionUDP.cc.

43{
44 m_socket.close();
45}

Member Function Documentation

◆ finalizeOnAbort()

virtual void finalizeOnAbort ( )
inlinevirtualinherited

Called just before the system exists after a fatal error.

Can be used to flush the output or close the file

Reimplemented in LogConnectionConsole, LogConnectionFilter, LogConnectionJSON, and LogConnectionTxtFile.

Definition at line 48 of file LogConnectionBase.h.

48{}

◆ isConnected()

bool isConnected ( )
finalvirtual

there is no way to check if a UDP connection is fine, so we just return True always

Implements LogConnectionBase.

Definition at line 27 of file LogConnectionUDP.cc.

28{
29 return true;
30}

◆ sendMessage()

bool sendMessage ( const LogMessage message)
finalvirtual

Send the log message as JSON to the UDP server.

Implements LogConnectionBase.

Definition at line 32 of file LogConnectionUDP.cc.

33{
34 const std::string& jsonMessage = message.toJSON(true);
35
36 boost::system::error_code err;
37 m_socket.send_to(buffer(jsonMessage, jsonMessage.size()), m_remoteEndpoint, 0, err);
38 const bool sent = err.value() != 0;
39 return sent;
40}

Member Data Documentation

◆ m_ioservice

boost::asio::io_service m_ioservice
private

asio service for handling the requests

Definition at line 33 of file LogConnectionUDP.h.

◆ m_remoteEndpoint

boost::asio::ip::udp::endpoint m_remoteEndpoint
private

the remote endpoint we send to

Definition at line 37 of file LogConnectionUDP.h.

◆ m_socket

boost::asio::ip::udp::socket m_socket
private

the socket to the UDP server

Definition at line 35 of file LogConnectionUDP.h.


The documentation for this class was generated from the following files: